Skip to content

Commit

Permalink
Merge pull request #2660 from quantified-uncertainty/fnmake
Browse files Browse the repository at this point in the history
Adds Calculator.make(fn) call
  • Loading branch information
OAGr committed Dec 6, 2023
2 parents 4eccbf9 + 0963778 commit 2dbf0de
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-snakes-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@quri/squiggle-lang": patch
---

Added Calculator.make(fn) shorthand
28 changes: 17 additions & 11 deletions packages/squiggle-lang/src/fr/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ import {
frCalculator,
} from "../library/registry/frTypes.js";
import { FnFactory } from "../library/registry/helpers.js";
import { vCalculator } from "../value/index.js";
import { Calculator, Value, vCalculator } from "../value/index.js";

Check warning on line 14 in packages/squiggle-lang/src/fr/calculator.ts

View workflow job for this annotation

GitHub Actions / Build, test, lint

'Value' is defined but never used

const maker = new FnFactory({
nameSpace: "Calculator",
requiresNamespace: true,
});

const validateCalculator = (calc: Calculator): Calculator => {
const _calc = vCalculator(calc);
const error = _calc.getError();
if (error) {
throw error;
} else {
return _calc.value;
}
};

export const library = [
maker.make({
name: "make",
Expand All @@ -36,22 +46,18 @@ export const library = [
),
],
frCalculator,
([{ fn, title, description, inputs, autorun, sampleCount }]) => {
const calc = vCalculator({
([{ fn, title, description, inputs, autorun, sampleCount }]) =>
validateCalculator({
fn,
title: title || undefined,
description: description || undefined,
inputs: inputs || [],
autorun: autorun === null ? true : autorun,
sampleCount: sampleCount || undefined,
});
const error = calc.getError(); // this is awkward, we have to construct Calculator value only to call this method
if (error) {
throw error;
} else {
return calc.value;
}
}
})
),
makeDefinition([frLambda], frCalculator, ([fn]) =>
validateCalculator(fn.toCalculator())
),
],
}),
Expand Down
29 changes: 28 additions & 1 deletion packages/squiggle-lang/src/reducer/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ASTNode } from "../ast/parse.js";
import * as IError from "../errors/IError.js";
import { REArityError, REOther } from "../errors/messages.js";
import { Expression } from "../expression/index.js";
import { VDomain, Value } from "../value/index.js";
import { Calculator, VDomain, Value } from "../value/index.js";
import * as Context from "./context.js";
import { ReducerContext } from "./context.js";
import { Stack } from "./stack.js";
Expand All @@ -16,6 +16,7 @@ import {
import uniq from "lodash/uniq.js";
import { sort } from "../utility/E_A_Floats.js";
import { FRType } from "../library/registry/frTypes.js";
import maxBy from "lodash/maxBy.js";

export type UserDefinedLambdaParameter = {
name: string;
Expand All @@ -33,6 +34,7 @@ export abstract class BaseLambda {
abstract parameterString(): string;
abstract parameterCounts(): number[];
abstract parameterCountString(): string;
abstract toCalculator(): Calculator;

callFrom(
args: Value[],
Expand Down Expand Up @@ -136,6 +138,18 @@ export class UserDefinedLambda extends BaseLambda {
parameterCountString() {
return this.parameters.length.toString();
}

toCalculator(): Calculator {
const only0Params = this.parameters.length === 0;
return {
fn: this,
inputs: this._getParameterNames().map((name) => ({
name: name,
type: "text",
})),
autorun: only0Params,
};
}
}

// Stdlib functions (everything in FunctionRegistry) are instances of this class.
Expand Down Expand Up @@ -197,6 +211,19 @@ export class BuiltinLambda extends BaseLambda {
}
throw new REOther(showNameMatchDefinitions());
}

toCalculator(): Calculator {
const longestSignature = maxBy(this.signatures(), (s) => s.length) || [];
const autorun = longestSignature.length !== 0;
return {
fn: this,
inputs: longestSignature.map((sig, i) => ({
name: `Input ${i + 1}`,
type: sig.getName() === "Bool" ? "checkbox" : "text",
})),
autorun,
};
}
}

export type Lambda = UserDefinedLambda | BuiltinLambda;
1 change: 1 addition & 0 deletions packages/website/src/pages/docs/Api/Calculator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Calculator.make: ({
autorun?: boolean = true,
sampleCount?: number,
}) => calculator
Calculator.make(fn: ...arguments => any) => calculator
```

``Calculator.make`` takes in a function, a description, and a list of fields. The function should take in the same number of arguments as the number of fields, and the arguments should be of the same type as the default value of the field.
Expand Down

3 comments on commit 2dbf0de

@vercel
Copy link

@vercel vercel bot commented on 2dbf0de Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2dbf0de Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2dbf0de Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.