Skip to content

Commit

Permalink
extract constantCell
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 30, 2024
1 parent 84384e8 commit 1721ae3
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

> https://github.com/cicada-lang/propagator/issues/4
extract constantCell
global map about about nogood

`propagators/binaryAmb`
Expand Down
6 changes: 3 additions & 3 deletions src/examples/barometer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Cell } from "../cell/index.js"
import { Interval } from "../interval/index.js"
import { definePropagator } from "../propagator/index.js"
import { constant, product, quadratic } from "../propagators/index.js"
import { constantCell, product, quadratic } from "../propagators/index.js"

// h = (1 / 2) * g * t * t

export const fallDuration = definePropagator(2, (t, h) => {
const g = constant(Interval(9.789, 9.832))()
product(constant(1 / 2)(), product(g, quadratic(t)), h)
const g = constantCell(Interval(9.789, 9.832))
product(constantCell(1 / 2), product(g, quadratic(t)), h)
})

export const similarTriangles = definePropagator(4, (sa, ha, sb, hb) => {
Expand Down
12 changes: 6 additions & 6 deletions src/examples/celsius.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Cell } from "../cell/index.js"
import { definePropagator } from "../propagator/index.js"
import {
constant,
constantCell,
divider,
multiplier,
product,
Expand All @@ -13,20 +13,20 @@ import {

export const fahrenheitToCelsius = definePropagator(2, (f, c) => {
multiplier(
subtractor(f, constant(32)()),
divider(constant(5)(), constant(9)()),
subtractor(f, constantCell(32)),
divider(constantCell(5), constantCell(9)),
c,
)
})

export const fahrenheitCelsius = definePropagator(2, (f, c) => {
const a = Cell()
sum(a, constant(32)(), f)
sum(a, constantCell(32), f)
const b = Cell()
product(b, constant(9)(), constant(5)())
product(b, constantCell(9), constantCell(5))
product(a, b, c)
})

export const celsiusKelvin = definePropagator(2, (c, k) => {
sum(c, constant(273.15)(), k)
sum(c, constantCell(273.15), k)
})
4 changes: 2 additions & 2 deletions src/examples/heron.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { definePropagator } from "../propagator/index.js"
import { adder, constant, divider } from "../propagators/index.js"
import { adder, constantCell, divider } from "../propagators/index.js"

// h = (g + x/g) / 2

export const heronStep = definePropagator(3, (x, g, h) => {
divider(adder(g, divider(x, g)), constant(2)(), h)
divider(adder(g, divider(x, g)), constantCell(2), h)
})
4 changes: 2 additions & 2 deletions src/propagators/constant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import assert from "node:assert"
import test from "node:test"
import { nothing } from "../nothing/Nothing.js"
import { run } from "../scheduler/index.js"
import { constant } from "./constant.js"
import { constantCell } from "./constant.js"

test("propagators / constant", async () => {
const x = constant(1)()
const x = constantCell(1)
assert(x.content === nothing)
await run()
assert(x.content === 1)
Expand Down
4 changes: 4 additions & 0 deletions src/propagators/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ import { definePrimitive } from "../propagator/index.js"
export function constant<T>(content: T) {
return definePrimitive(1, () => content)
}

export function constantCell<T>(content: T) {
return constant(content)()
}

0 comments on commit 1721ae3

Please sign in to comment.