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

Function foldAssociativeOperator

src/math-json/utils.ts:415–429  ·  view source on GitHub ↗
(
  op: string,
  lhs: MathJsonExpression,
  rhs: MathJsonExpression
)

Source from the content-addressed store, hash-verified

413
414/**
415 * Assuming that op is an associative operator, fold lhs or rhs
416 * if either are the same operator.
417 */
418export function foldAssociativeOperator(
419 op: string,
420 lhs: MathJsonExpression,
421 rhs: MathJsonExpression
422): MathJsonExpression {
423 const lhsName = operator(lhs);
424 const rhsName = operator(rhs);
425
426 if (lhsName === op && rhsName === op)
427 return [op, ...operands(lhs), ...operands(rhs)];
428
429 if (lhsName === op) return [op, ...operands(lhs), rhs];
430 if (rhsName === op) return [op, lhs, ...operands(rhs)];
431 return [op, lhs, rhs];
432}

Callers 2

makeParseHandlerFunction · 0.90

Calls 2

operandsFunction · 0.85
operatorFunction · 0.70

Tested by

no test coverage detected