-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[the-art]
definePropagator
-- handles currying
- Loading branch information
Showing
4 changed files
with
51 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,30 @@ | ||
import type { Cell } from "../cell/index.js" | ||
import type { PropagatorDefinition } from "./PropagatorDefinition.js" | ||
import { repeatApply } from "../../utils/repeatApply.js" | ||
import { createCell, type Cell } from "../cell/index.js" | ||
import type { PropagatorDefinitionWithFixedArity } from "./PropagatorDefinition.js" | ||
|
||
export function definePropagator( | ||
arity: number, | ||
export function definePropagator<A extends number>( | ||
arity: A, | ||
fn: (...args: Array<Cell<unknown>>) => void, | ||
): PropagatorDefinition { | ||
const definiton = (...args: Array<Cell<unknown>>) => { | ||
fn(...args) | ||
): PropagatorDefinitionWithFixedArity<A> { | ||
const definition = (...args: Array<Cell<unknown>>) => { | ||
if (args.length === arity) { | ||
fn(...args) | ||
} else if (args.length === arity - 1) { | ||
const output = createCell() | ||
fn(...args, output) | ||
return output | ||
} else if (args.length < arity - 1) { | ||
const outputs = repeatApply(arity - args.length, () => createCell(), []) | ||
fn(...args, ...outputs) | ||
return outputs | ||
} else { | ||
throw new Error( | ||
`[definePropagator] number of arguments ${args.length} exceed arity plus one: ${arity + 1}`, | ||
) | ||
} | ||
} | ||
|
||
definiton.arity = arity | ||
definition.arity = arity | ||
|
||
return definiton | ||
return definition as unknown as PropagatorDefinitionWithFixedArity<A> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { addPropagator, broadcast, type Cell } from "../cell/index.js" | ||
import type { Propagator } from "./Propagator.js" | ||
|
||
export function watch( | ||
cells: Array<Cell<unknown>>, | ||
propagator: Propagator, | ||
): void { | ||
for (const cell of cells) { | ||
addPropagator(cell, propagator) | ||
} | ||
|
||
broadcast([propagator]) | ||
} |