Skip to content

Commit

Permalink
simple assertSameContext
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 6, 2024
1 parent d4115b4 commit bc0441b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# lattice

`lattice/conceptBelow` & `lattice/conceptAbove`
`lattice/conceptBelow`
`lattice/conceptAbove`

`LatticeLayout`
`layoutLattice(context)`

Expand Down
22 changes: 14 additions & 8 deletions src/concept/assertSameContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ import type { Concept } from "./Concept.js"

export function assertSameContext(
concepts: Array<Concept>,
options: {
who: string
message: string
} & Record<string, any>,
options: { who: string },
): void {
if (concepts.length < 2) return

const { who, message } = options
const { who } = options

const [first, ...rest] = concepts
const firstContext = first.context
const [firstConcept, ...rest] = concepts
const firstContext = firstConcept.context
for (const concept of rest) {
if (firstContext !== concept.context) {
log(options)
const message = "I expect the contexts of x and y to be the same."

log({
who,
message,
concepts,
first: firstConcept,
offendingConcept: concept,
})

throw new Error(`[assertSameContext by ${who}] ${message}`)
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/concept/conceptJoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import type { Concept } from "./Concept.js"
import { conceptFromAttributes } from "./conceptFromAttributes.js"

export function conceptJoin(x: Concept, y: Concept): Concept {
assertSameContext([x, y], {
who: "conceptJoin",
message: "I expect the contexts of x and y to be the same.",
})
assertSameContext([x, y], { who: "conceptJoin" })

return conceptFromAttributes(x.context, setIntersection(x.intent, y.intent))
}
5 changes: 1 addition & 4 deletions src/concept/conceptMeet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import type { Concept } from "./Concept.js"
import { conceptFromEntities } from "./conceptFromEntities.js"

export function conceptMeet(x: Concept, y: Concept): Concept {
assertSameContext([x, y], {
who: "conceptMeet",
message: "I expect the contexts of x and y to be the same.",
})
assertSameContext([x, y], { who: "conceptMeet" })

return conceptFromEntities(x.context, setIntersection(x.extent, y.extent))
}
2 changes: 2 additions & 0 deletions src/lattice/conceptBelow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { assertSameContext } from "../concept/assertSameContext.js"
import type { Concept } from "../concept/Concept.js"

export function conceptBelow(x: Concept, y: Concept): boolean {
assertSameContext([x, y], { who: "conceptBelow" })
return false
}

0 comments on commit bc0441b

Please sign in to comment.