Skip to content

Commit

Permalink
propagators/boolean.test -- inverter
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 30, 2024
1 parent 2d19fc9 commit e1b4849
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 4 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

> https://github.com/cicada-lang/propagator/issues/4
propagators/boolean.test -- inverter

propagators/conditional -- conditional & conditionalWriter
propagators/conditional.test

Expand Down
6 changes: 5 additions & 1 deletion src/generics/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { defineGeneric, defineHandler } from "../generic/index.js"
import { isBoolean } from "../utils/isBoolean.js"

export const not = defineGeneric()
export const not = defineGeneric({
default: (...args) => {
console.log("hi", args)
},
})
defineHandler(not, [isBoolean], (x) => !x)

export const and = defineGeneric()
Expand Down
1 change: 1 addition & 0 deletions src/generics/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./arithmetic.js"
export * from "./boolean.js"
export * from "./quadratic.js"
1 change: 1 addition & 0 deletions src/merge/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./merge-belief-system.js"
export * from "./merge-belief.js"
export * from "./merge-boolean.js"
export * from "./merge-interval.js"
export * from "./merge-merge-conflict.js"
export * from "./merge-nothing.js"
Expand Down
8 changes: 8 additions & 0 deletions src/merge/merge-boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineHandler } from "../generic/index.js"
import { theMergeConflict } from "../merge-conflict/index.js"
import { isBoolean } from "../utils/isBoolean.js"
import { merge } from "./merge.js"

defineHandler(merge, [isBoolean, isBoolean], (content, increment) =>
increment === content ? content : theMergeConflict,
)
14 changes: 14 additions & 0 deletions src/propagators/boolean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import assert from "node:assert"
import test from "node:test"
import { patch } from "../cell/index.js"
import { run } from "../scheduler/index.js"
import { inverter } from "./boolean.js"

test("propagators / boolean", async () => {
const [x, y] = inverter()
patch(x, true)

await run()

assert(y.content === false)
})
2 changes: 1 addition & 1 deletion src/propagators/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { not } from "../generics/boolean.js"
import { not } from "../generics/index.js"
import { definePrimitive } from "../propagator/index.js"

export const inverter = definePrimitive(2, not)

0 comments on commit e1b4849

Please sign in to comment.