<commit_before>const _ = require('lodash')

module.exports = {
  /**
   * @returns a flatten array of all cells in the grid (cell is object with props x, y, value).
   */
  cells: lift(_.flatMap, _.map, (value, x, y) => ({value, x, y})),
  /**
   * Runs a function on each cell. Function takes params value, x, y, grid, row.
   */
  each: lift(_.each, _.each, (value, x, y, grid, row, f) => f(value, x, y, grid, row)),
  /**
   * Maps values of the cells. Function takes same params as `each`.
   */
  map: lift(_.map, _.map, (value, x, y, grid, row, f) => f(value, x, y, grid, row)),
  /**
   * Matrix transposition from maths. Inverts columns and rows. https://en.wikipedia.org/wiki/Transpose
   */
  transpose
}

function lift (fgrid, frow, fvalue) {
  return (grid, ...args) => {
    return fgrid(grid, (row, y) => {
      return frow(row, (value, x) => {
        return fvalue(value, x, y, grid, row, ...args)
      })
    })<commit_msg>docs(grid-utils): jsdoc with types<commit_after>const _ = require('lodash')

/**
 * @typedef {Array.<Array>} Grid
 */

/**
 * @typedef {Object} Cell
 * @property value
 * @property {number} x
 * @property {number} y
 */

/**
 * @callback GridIteratee
 * @param value
 * @param {number} x
 * @param {number} y
 * @param {Grid} grid
 * @param {Array} row
 */

module.exports = {

  /**
   * @param {Grid} grid
   * @returns {Array.<Cell>}
   */
  cells: lift(_.flatMap, _.map, (value, x, y) => ({value, x, y})),

  /**
   * Runs a function on each cell.
   * @param {Grid} grid
   * @param {GridIteratee} f
   * @returns {undefined}
   */
  each: lift(_.each, _.each, (value, x, y, grid, row, f) => f(value, x, y, grid, row)),

  /**
   * Maps values of the cells.
   * @param {Grid} grid
   * @param {GridIteratee} f
   * @returns {Grid}
   */
  map: lift(_.map, _.map, (value, x, y, grid, row, f) => f(value, x, y, grid, row)),

  /**
   * Matrix transposition from maths. Inverts columns and rows. https://en.wikipedia.org/wiki/Transpose
   * @param {Grid} grid
   * @returns {Grid}
   */
  transpose

}

function lift (fgrid, frow, fvalue) {
  return (grid, ...args) => {
    return fgrid(grid, (row, y) => {
      return frow(row, (value, x) => {
        return fvalue(value, x, y, grid, row, ...args)
      })
    })
{'fraction_non_alphanumeric': 0.1900972590627763, 'fraction_numerical': 0.0, 'mean_word_length': 2.715927750410509, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6364894', 'n_tokens_mistral': 840, 'n_tokens_neox': 827, 'n_words': 259}