-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds Calculator.make(fn) call #2660
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@quri/squiggle-lang": patch | ||
--- | ||
|
||
Added Calculator.make(fn) shorthand |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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; | ||
|
@@ -33,6 +34,7 @@ export abstract class BaseLambda { | |
abstract parameterString(): string; | ||
abstract parameterCounts(): number[]; | ||
abstract parameterCountString(): string; | ||
abstract toCalculator(): Calculator; | ||
|
||
callFrom( | ||
args: Value[], | ||
|
@@ -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. | ||
|
@@ -193,6 +207,20 @@ 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", | ||
})), | ||
sampleCount: 10000, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, why? Shouldn't this be inherited from environment, like in user-defined lambdas? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted this to be a bit higher than normal, because these are probably fairly small (just one function). That said, as I think about it, maybe it could be a very expensive function. Can change. |
||
autorun, | ||
}; | ||
} | ||
} | ||
|
||
export type Lambda = UserDefinedLambda | BuiltinLambda; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a reasonable heuristic, but it could cause some frustration if the longest signature is not the most natural one.
Brainstorming other, more general approaches:
Calculator.make(fn) -> Calculator[]
(return all possible versions)Calculator.make(fn.definitions[i]) -> Calculator
, wherefn.definitions[i]
returns a specialized function with a single signature (but this is awkward because it means thatfn.definitions[3][0][0][0][0][0]
should be allowed)Calculator.make(fn, 3) -> Calculator
(same asCalculator.make(fn)[3]
in (1) above)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the ideas, but I don't think I'm a fan of those.
One nice thing is that this issue should only come into play for builtin functions - users functions only have one definition.
My guess is that we should leave for now. However, here are some options for later:
Maybe, if there are multiple definitions, then if you try
Calculator.make()
without any definitions, it shows you an error, but gives you a list of definitions as strings. You then can plug them in as,Calculator.make(defString)
.We output a Dict, where keys have to do with the definitions somehow.
We output a small dropdown menu of definitions, after which the correct form will appear.