Skip to content

Commit

Permalink
[the-art] quadratic, squarer and sqrter
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jun 15, 2024
1 parent 8d30393 commit c4fcc4a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[the-art] quadratic, squarer and sqrter

[the-art] 实现能够处理 interval 的 propagator
[the-art] use a scheduler
[the-art] `merge` as generic lattice operations
Expand Down
21 changes: 21 additions & 0 deletions src/the-art/toys/quadratic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from "node:assert"
import test from "node:test"
import { addContent } from "../cell/addContent.js"
import { content } from "../cell/index.js"
import { quadratic } from "./quadratic.js"

test("quadratic", () => {
{
const [r, s] = quadratic()
addContent(r, 2)

assert.deepStrictEqual(content(s), 4)
}

{
const [r, s] = quadratic()
addContent(s, 4)

assert.deepStrictEqual(content(r), 2)
}
})
10 changes: 10 additions & 0 deletions src/the-art/toys/quadratic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { definePrimitive } from "../propagator/definePrimitive.js"
import { definePropagator } from "../propagator/definePropagator.js"

export const squarer = definePrimitive(2, (x) => x * x)
export const sqrter = definePrimitive(2, (x) => Math.sqrt(x))

export const quadratic = definePropagator(2, (r, s) => {
squarer(r, s)
sqrter(s, r)
})

0 comments on commit c4fcc4a

Please sign in to comment.