Skip to content

Commit

Permalink
Added list for auto-generation
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Jan 2, 2024
1 parent 070b7cb commit 74b5fe7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 325 deletions.
106 changes: 71 additions & 35 deletions packages/squiggle-lang/src/fr/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ export const library = [
examples: [
`List.make(2, 3)`,
`List.make(2, {|| 3})`,
`List.make(2, {|f| f+1})`,
`List.make(2, {|index| index+1})`,
],
displaySection: "Constructors",
description: `Creates an array of length \`count\`, with each element being \`value\`. If \`value\` is a function, it will be called \`count\` times, with the index as the argument.`,
definitions: [
makeAssertDefinition(
[frNumber, frLambdaNand([0, 1])],
Expand Down Expand Up @@ -196,6 +198,7 @@ export const library = [
name: "upTo",
output: "Array",
examples: [`List.upTo(1,4)`],
displaySection: "Constructors",
definitions: [
makeDefinition(
[frNamed("low", frNumber), frNamed("high", frNumber)],
Expand All @@ -216,6 +219,7 @@ export const library = [
requiresNamespace: true,
output: "Number",
examples: [`List.length([1,4,5])`],
displaySection: "Queries",
definitions: [
makeDefinition([frArray(frAny())], frNumber, ([values]) => values.length),
],
Expand All @@ -224,6 +228,7 @@ export const library = [
name: "first",
requiresNamespace: true,
examples: [`List.first([1,4,5])`],
displaySection: "Queries",
definitions: [
makeDefinition(
[frArray(frAny({ genericName: "A" }))],
Expand All @@ -239,6 +244,7 @@ export const library = [
name: "last",
requiresNamespace: true,
examples: [`List.last([1,4,5])`],
displaySection: "Queries",
definitions: [
makeDefinition(
[frArray(frAny({ genericName: "A" }))],
Expand All @@ -255,6 +261,7 @@ export const library = [
output: "Array",
requiresNamespace: false,
examples: [`List.reverse([1,4,5]) // [5,4,1]`],
displaySection: "Modifications",
definitions: [
makeDefinition(
[frArray(frAny({ genericName: "A" }))],
Expand All @@ -263,42 +270,12 @@ export const library = [
),
],
}),
maker.make({
name: "map",
output: "Array",
requiresNamespace: false,
examples: [
"List.map([1,4,5], {|x| x+1})",
"List.map([1,4,5], {|x,i| x+i+1})",
],
definitions: [
makeAssertDefinition(
[frNumber, frLambdaNand([1, 2])],
"Call with either 1 or 2 arguments, not both."
),
makeDefinition(
[
frArray(frAny({ genericName: "A" })),
frLambdaTyped(
[
frAny({ genericName: "A" }),
frNamed("index", frOptional(frNumber)),
],
frAny({ genericName: "B" })
),
],
frArray(frAny({ genericName: "B" })),
([array, lambda], context) => {
const usedOptional = chooseLambdaParamLength([1, 2], lambda) === 2;
return _map(array, lambda, context, usedOptional ? true : false);
}
),
],
}),

maker.make({
name: "concat",
requiresNamespace: true,
examples: [`List.concat([1,2,3], [4, 5, 6])`],
displaySection: "Modifications",
definitions: [
makeDefinition(
[
Expand All @@ -314,6 +291,7 @@ export const library = [
name: "sortBy",
requiresNamespace: true,
examples: [`List.sortBy([{a:3}, {a:1}], {|f| f.a})`],
displaySection: "Modifications",
definitions: [
makeDefinition(
[
Expand All @@ -333,6 +311,7 @@ export const library = [
name: "minBy",
requiresNamespace: true,
examples: [`List.minBy([{a:3}, {a:1}], {|f| f.a})`],
displaySection: "Queries",
definitions: [
makeDefinition(
[
Expand All @@ -358,6 +337,7 @@ export const library = [
name: "maxBy",
requiresNamespace: true,
examples: [`List.maxBy([{a:3}, {a:1}], {|f| f.a})`],
displaySection: "Queries",
definitions: [
makeDefinition(
[
Expand All @@ -383,6 +363,7 @@ export const library = [
name: "append",
requiresNamespace: true,
examples: [`List.append([1,4],5)`],
displaySection: "Modifications",
definitions: [
makeDefinition(
[frArray(frAny({ genericName: "A" })), frAny({ genericName: "A" })],
Expand All @@ -397,6 +378,7 @@ export const library = [
"Returns a copy of the list, between the selected ``start`` and ``end``, end not included. Directly uses the [Javascript implementation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) underneath.",
requiresNamespace: true,
examples: [`List.slice([1,2,5,10],1,3)`],
displaySection: "Filtering",
definitions: [
makeDefinition(
[
Expand All @@ -423,6 +405,7 @@ export const library = [
"Filters the list for unique elements. Works on select Squiggle types.",
requiresNamespace: true,
examples: [`List.uniq([1,2,3,"hi",false,"hi"])`],
displaySection: "Filtering",
definitions: [
makeDefinition(
[frArray(frAny({ genericName: "A" }))],
Expand All @@ -437,6 +420,7 @@ export const library = [
"Filters the list for unique elements. Works on select Squiggle types.",
requiresNamespace: true,
examples: [`List.uniqBy([[1,5], [3,5], [5,7]], {|x| x[1]})`],
displaySection: "Filtering",
definitions: [
makeDefinition(
[
Expand All @@ -452,9 +436,43 @@ export const library = [
),
],
}),
maker.make({
name: "map",
output: "Array",
requiresNamespace: false,
displaySection: "Functional Transformations",
examples: [
"List.map([1,4,5], {|x| x+1})",
"List.map([1,4,5], {|x,i| x+i+1})",
],
definitions: [
makeAssertDefinition(
[frNumber, frLambdaNand([1, 2])],
"Call with either 1 or 2 arguments, not both."
),
makeDefinition(
[
frArray(frAny({ genericName: "A" })),
frLambdaTyped(
[
frAny({ genericName: "A" }),
frNamed("index", frOptional(frNumber)),
],
frAny({ genericName: "B" })
),
],
frArray(frAny({ genericName: "B" })),
([array, lambda], context) => {
const usedOptional = chooseLambdaParamLength([1, 2], lambda) === 2;
return _map(array, lambda, context, usedOptional ? true : false);
}
),
],
}),
maker.make({
name: "reduce",
requiresNamespace: false,
displaySection: "Functional Transformations",
description:
"Applies `f` to each element of `arr`. The function `f` has two main paramaters, an accumulator and the next value from the array. It can also accept an optional third `index` parameter.",
examples: [`List.reduce([1,4,5], 2, {|acc, el| acc+el})`],
Expand Down Expand Up @@ -496,7 +514,9 @@ export const library = [
maker.make({
name: "reduceReverse",
requiresNamespace: false,
displaySection: "Functional Transformations",
examples: [`List.reduceReverse([1,4,5], 2, {|acc, el| acc-el})`],
description: `Works like \`reduce\`, but the function is applied to each item from the last back to the first.`,
definitions: [
makeDefinition(
[
Expand Down Expand Up @@ -527,8 +547,15 @@ export const library = [
// Returns the last value that fits the condition.
// If even initial value doesn't fit the condition, it will be returned anyway;
// So the result isn't guaranteed to fit the condition.
`List.reduceWhile([1,4,5], 0, {|acc, curr| acc + curr }, {|acc| acc < 5})`,
`// Adds first two elements, returns \`11\`.
List.reduceWhile([5, 6, 7], 0, {|acc, curr| acc + curr}, {|acc| acc < 15})
`,
`// Adds first two elements, returns \`{ x: 11 }\`.
List.reduceWhile([5, 6, 7], { x: 0 }, {|acc, curr| { x: acc.x + curr }}, {|acc| acc.x < 15})`,
],
description: `Works like \`reduce\`, but stops when the condition is no longer met. This is useful, in part, for simulating processes that need to stop based on the process state.
`,
displaySection: "Functional Transformations",
definitions: [
makeDefinition(
[
Expand Down Expand Up @@ -559,6 +586,7 @@ export const library = [
name: "filter",
requiresNamespace: false,
examples: [`List.filter([1,4,5], {|x| x>3})`],
displaySection: "Filtering",
definitions: [
makeDefinition(
[
Expand All @@ -575,6 +603,7 @@ export const library = [
name: "every",
requiresNamespace: true,
examples: [`List.every([1,4,5], {|el| el>3 })`],
displaySection: "Queries",
definitions: [
makeDefinition(
[
Expand All @@ -591,6 +620,7 @@ export const library = [
name: "some",
requiresNamespace: true,
examples: [`List.some([1,4,5], {|el| el>3 })`],
displaySection: "Queries",
definitions: [
makeDefinition(
[
Expand All @@ -608,6 +638,7 @@ export const library = [
description: "Returns an error if there is no value found",
requiresNamespace: true,
examples: [`List.find([1,4,5], {|el| el>3 })`],
displaySection: "Queries",
definitions: [
makeDefinition(
[
Expand All @@ -630,6 +661,7 @@ export const library = [
description: "Returns `-1` if there is no value found",
requiresNamespace: true,
examples: [`List.findIndex([1,4,5], {|el| el>3 })`],
displaySection: "Queries",
definitions: [
makeDefinition(
[
Expand All @@ -645,7 +677,7 @@ export const library = [
maker.make({
name: "join",
requiresNamespace: true,
examples: [`List.join(["a", "b", "c"], ",")`],
examples: [`List.join(["a", "b", "c"], ",") // "a,b,c"`],
definitions: [
makeDefinition(
[frArray(frString), frNamed("separator", frOptional(frString))],
Expand All @@ -659,6 +691,7 @@ export const library = [
name: "flatten",
requiresNamespace: true,
examples: [`List.flatten([[1,2], [3,4]])`],
displaySection: "Modifications",
definitions: [
makeDefinition([frArray(frAny())], frArray(frAny()), ([arr]) =>
arr.reduce(
Expand All @@ -673,6 +706,7 @@ export const library = [
name: "shuffle",
requiresNamespace: true,
examples: [`List.shuffle([1,3,4,20])`],
displaySection: "Modifications",
definitions: [
makeDefinition(
[frArray(frAny({ genericName: "A" }))],
Expand All @@ -685,6 +719,7 @@ export const library = [
name: "zip",
requiresNamespace: true,
examples: [`List.zip([1,3,4,20], [2,4,5,6])`],
displaySection: "Modifications",
definitions: [
makeDefinition(
[
Expand All @@ -707,6 +742,7 @@ export const library = [
name: "unzip",
requiresNamespace: true,
examples: [`List.unzip([[1,2], [2,3], [4,5]])`],
displaySection: "Modifications",
definitions: [
makeDefinition(
[
Expand Down
1 change: 1 addition & 0 deletions packages/website/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
/src/pages/docs/Api/PointSet.mdx
/src/pages/docs/Api/Sym.mdx
/src/pages/docs/Api/Scale.mdx
/src/pages/docs/Api/List.mdx
/src/pages/docs/Ecosystem/LLMPrompt.md
/public/llms/documentationBundle.txt
20 changes: 20 additions & 0 deletions packages/website/scripts/generateModulePages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ One complication is that it's possible to represent invalid probability distribu
Squiggle uses D3 for the tick formats. You can read about d3 tick formats [here](https://github.com/d3/d3-format).`,
},
{
name: "List",
description:
"Lists are a simple data structure that can hold any type of value. They are similar to arrays in Javascript or lists in Python.",
sections: [
{ name: "Constructors" },
{ name: "Modifications" },
{ name: "Filtering" },
{ name: "Queries" },
{ name: "Functional Transformations" },
],
imports: `import { FnDocumentationFromName } from "@quri/squiggle-components";`,
intro: `Lists are a simple data structure that can hold any type of value. They are similar to arrays in Javascript or lists in Python.
\`\`\`squiggle
myList = [1, 2, 3, normal(5,2), "hello"]
\`\`\`
Lists are immutable, meaning that they cannot be modified. Instead, all list functions return a new list.`,
},
];

function toMarkdownDefinitions(definitions) {
Expand Down
Loading

0 comments on commit 74b5fe7

Please sign in to comment.