MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / captureClosures

Function captureClosures

src/compute-engine/function-utils.ts:374–424  ·  view source on GitHub ↗

* Recursively walk `expr` and rebind any Function literals so their body * scopes close over `closureParent`. This handles Functions nested inside * List, Tuple, Pair, or any other compound expression. * * Example: given `List(Function(x+n), Function(x+2*n))` where n=5 lives * in `closureParent

(
  ce: ComputeEngine,
  expr: Expression,
  closureParent: Scope
)

Source from the content-addressed store, hash-verified

372 * From an expression, return a predicate function, which can be used to filter.
373 */
374export function predicate(
375 _expr: Expression
376): (...args: Expression[]) => boolean {
377 // Expression-backed predicates are not implemented; the conservative
378 // fallback rejects every candidate.
379 return () => false;
380}
381
382/**
383 * From an expression, create an ordering function, which can be used to sort.
384 */
385export function order(
386 _expr: Expression
387): (a: Expression, b: Expression) => -1 | 0 | 1 {
388 // Expression-backed comparators are not implemented. Use the canonical
389 // expression order as the fallback.
390 return (a: Expression, b: Expression) => {
391 const c = cmp(a, b);
392 if (c === '=') return 0;
393 if (c === '<' || c === '<=') return -1;
394 return 1;
395 };
396}
397
398/**
399 * Given an expression, rewrite it to a symbol or canonical Function form.
400 *
401 * - symbol (no change):
402 * "Sin"
403 * -> "Sin"
404 *
405 * - built-in function:
406 * ["BuiltinFunction", "Sin"]
407 * -> "Sin"
408 *
409 * - parenthesized expression:
410 * ["Delimiter", ["Add", "_", 1], "'()'"]
411 * -> ["Function", ["Block", ["Add", "_", 1]], "_"]
412 *
413 * - explicit parameters (adding a block to serve as a scope for the arguments):
414 * ["Function", ["Add", "x", 1], "x"]
415 * -> ["Function", ["Block", ["Add", "x", 1]], "x"]
416 *
417 *
418 * - single anonymous parameters:
419 * ["Add", "_", 1]
420 * -> ["Function", ["Block", ["Add", "_", 1]], "_"]
421 *
422 * - multiple anonymous parameters:
423 * ["Add", "_1", "_2"]
424 * -> ["Function", ["Block", ["Add", "_1", "_2"]], "_1", "_2"]
425 *
426 *
427 */

Callers 1

invokeFunction · 0.85

Calls 7

isFunctionFunction · 0.90
isSymbolFunction · 0.90
mapMethod · 0.65
sliceMethod · 0.65
hasMethod · 0.65
setMethod · 0.65
_fnMethod · 0.65

Tested by

no test coverage detected