Skip to content

Commit

Permalink
feat(formula): support lambda in function register
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Dec 13, 2024
1 parent 0509114 commit fb42cc2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/engine-formula/src/engine/ast-node/function-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ export class FunctionNode extends BaseAstNode {
return (variant as ArrayValueObject).toValue();
}

if (variant.isLambda()) {
return variant;
}

return variant.getValue();
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import type { BaseAstNode } from '../ast-node/base-ast-node';
import type { LambdaParameterNode } from '../ast-node/lambda-parameter-node';
import type { Interpreter } from '../interpreter/interpreter';
import type { BaseReferenceObject, FunctionVariantType } from '../reference-object/base-reference-object';
import type { PrimitiveValueType } from './primitive-object';
import { ErrorType } from '../../basics/error-type';
import { DEFAULT_TOKEN_TYPE_LAMBDA_RUNTIME_PARAMETER } from '../../basics/token-type';
import { AsyncObject } from '../reference-object/base-reference-object';
import { generateExecuteAstNodeData } from '../utils/ast-node-tool';
import { ValueObjectFactory } from './array-value-object';
import { BaseValueObject, ErrorValueObject } from './base-value-object';

function getRootLexerHasValueNode(node: Nullable<BaseAstNode>): Nullable<BaseAstNode> {
Expand Down Expand Up @@ -110,6 +112,16 @@ export class LambdaValueObjectObject extends BaseValueObject {
return value;
}

/**
* Execute custom lambda function, handle basic types
* @param variants
*/
executeCustom(...variants: PrimitiveValueType[]) {
// Create base value object from primitive value, then execute
const baseValueObjects = variants.map((variant) => ValueObjectFactory.create(variant));
return this.execute(...baseValueObjects);
}

private _setLambdaNodeValue(node: Nullable<BaseAstNode>) {
if (!node) {
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-formula/src/functions/base-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class BaseFunction {
}

calculateCustom(
...arg: Array<PrimitiveValueType | PrimitiveValueType[][]>
...arg: Array<PrimitiveValueType | PrimitiveValueType[][] | BaseValueObject>
): PrimitiveValueType | PrimitiveValueType[][] {
return null;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/facade/src/apis/__tests__/facade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ describe('Test FUniver', () => {
[function (...variants) {
let sum = 0;

const last = variants[variants.length - 1];
// @ts-ignore
if (last.isLambda()) {
variants.pop();

const variantsList = variants.map((variant) => Array.isArray(variant) ? variant[0][0] : variant);
// @ts-ignore
sum += last.executeCustom(...variantsList).getValue();
}

for (const variant of variants) {
sum += Number(variant) || 0;
}
Expand Down

0 comments on commit fb42cc2

Please sign in to comment.