simple-squiggle

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

forEach.transform.js (2579B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createForEachTransform = void 0;
      7 
      8 var _is = require("../../utils/is.js");
      9 
     10 var _function = require("../../utils/function.js");
     11 
     12 var _array = require("../../utils/array.js");
     13 
     14 var _factory = require("../../utils/factory.js");
     15 
     16 var _compileInlineExpression = require("./utils/compileInlineExpression.js");
     17 
     18 var name = 'forEach';
     19 var dependencies = ['typed'];
     20 var createForEachTransform = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     21   var typed = _ref.typed;
     22 
     23   /**
     24    * Attach a transform function to math.forEach
     25    * Adds a property transform containing the transform function.
     26    *
     27    * This transform creates a one-based index instead of a zero-based index
     28    */
     29   function forEachTransform(args, math, scope) {
     30     var x, callback;
     31 
     32     if (args[0]) {
     33       x = args[0].compile().evaluate(scope);
     34     }
     35 
     36     if (args[1]) {
     37       if ((0, _is.isSymbolNode)(args[1]) || (0, _is.isFunctionAssignmentNode)(args[1])) {
     38         // a function pointer, like forEach([3, -2, 5], myTestFunction)
     39         callback = args[1].compile().evaluate(scope);
     40       } else {
     41         // an expression like forEach([3, -2, 5], x > 0 ? callback1(x) : callback2(x) )
     42         callback = (0, _compileInlineExpression.compileInlineExpression)(args[1], math, scope);
     43       }
     44     }
     45 
     46     return _forEach(x, callback);
     47   }
     48 
     49   forEachTransform.rawArgs = true; // one-based version of forEach
     50 
     51   var _forEach = typed('forEach', {
     52     'Array | Matrix, function': function ArrayMatrixFunction(array, callback) {
     53       // figure out what number of arguments the callback function expects
     54       var args = (0, _function.maxArgumentCount)(callback);
     55 
     56       var recurse = function recurse(value, index) {
     57         if (Array.isArray(value)) {
     58           (0, _array.forEach)(value, function (child, i) {
     59             // we create a copy of the index array and append the new index value
     60             recurse(child, index.concat(i + 1)); // one based index, hence i+1
     61           });
     62         } else {
     63           // invoke the callback function with the right number of arguments
     64           if (args === 1) {
     65             callback(value);
     66           } else if (args === 2) {
     67             callback(value, index);
     68           } else {
     69             // 3 or -1
     70             callback(value, index, array);
     71           }
     72         }
     73       };
     74 
     75       recurse(array.valueOf(), []); // pass Array
     76     }
     77   });
     78 
     79   return forEachTransform;
     80 }, {
     81   isTransformFunction: true
     82 });
     83 exports.createForEachTransform = createForEachTransform;