simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

count.js (940B)


      1 import { factory } from '../../utils/factory.js';
      2 var name = 'count';
      3 var dependencies = ['typed', 'size', 'prod'];
      4 export var createCount = /* #__PURE__ */factory(name, dependencies, _ref => {
      5   var {
      6     typed,
      7     size,
      8     prod
      9   } = _ref;
     10 
     11   /**
     12    * Count the number of elements of a matrix, array or string.
     13    *
     14    * Syntax:
     15    *
     16    *     math.count(x)
     17    *
     18    * Examples:
     19    *
     20    *     math.count('hello world')        // returns 11
     21    *     const A = [[1, 2, 3], [4, 5, 6]]
     22    *     math.count(A)                    // returns 6
     23    *     math.count(math.range(1,6))      // returns 5
     24    *
     25    * See also:
     26    *
     27    *     size
     28    *
     29    * @param {string | Array | Matrix} x  A matrix or string
     30    * @return {number} An integer with the elements in `x`.
     31    */
     32   return typed(name, {
     33     string: function string(x) {
     34       return x.length;
     35     },
     36     'Matrix | Array': function MatrixArray(x) {
     37       return prod(size(x));
     38     }
     39   });
     40 });