Skip to content

Commit

Permalink
Doc update for v0.1.4 (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
robsimmons authored Nov 28, 2024
1 parent a09052d commit 6286561
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
30 changes: 22 additions & 8 deletions docs/src/content/docs/docs/api/dusa.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const dusa = new Dusa(`
dusa.solution; // null
```

[Explore this example on val.town](htthttps://www.val.town/v/robsimmons/solution_getter_no)
[Explore this example on val.town](https://www.val.town/v/robsimmons/solution_getter_no)

If there are multiple solutions, the `solution` getter will pick one solution,
and will always return that one.
Expand All @@ -76,24 +76,38 @@ dusa.solution; // raises DusaError

[Explore this example on val.town](https://www.val.town/v/robsimmons/solution_getter_maybe)

### Getting all solutions
### Getting all solutions with `solve()`

To enumerate the solutions to a program, you can use the Javascript iterator
notation. The iterator works in an arbitrary order: this program will either
print `[["one"]]` and then `[["two"]]` or else it will print `[["two"]]` and
then `[["one"]]`.
The `solve()` function returns a standard
[JavaScript iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator)
that will, upon successive calls to `next()`, return each solution for the
Dusa program. The iterator works in an arbitrary order: this program will
either print `"one"` and then `"two"` or else it will print `"two"` and then
`"one"`.

```javascript
const dusa = new Dusa(`name is { "one", "two" }.`);
const iterator = dusa.solve();
console.log(iterator.next().value?.get('name')); // "one" or "two"
console.log(iterator.next().value?.get('name')); // "two" or "one"
console.log(iterator.next().value?.get('name')); // undefined
```
[Explore this example on val.town](https://www.val.town/v/robsimmons/solutions_with_next)
Dusa classes themselves are also `Iterable` — they implement the
`[Symbol.iterator]` method and so can be used in `for..of` loops:
```javascript
const dusa = new Dusa(`name is { "one", "two" }.`);
for (const solution of dusa) {
console.log([...solution.lookup('name')]);
console.log(solution.get('name'));
}
```
[Explore this example on val.town](https://www.val.town/v/robsimmons/solutions_enumerate)
Each time you invoke the iterator `dusa` getter is accessed, search is re-run,
Each time you invoke the iterator `dusa` is accessed, search is re-run,
potentially returning solutions in a different order.
## Modifying a Dusa instance
Expand Down
4 changes: 2 additions & 2 deletions docs/src/content/docs/docs/api/dusasolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ This will print the following in some order:
Path from 2 to 4
Path from 3 to 4

Given the first argument `'path'` and the second argument `2n`, `lookup` will
Given the first argument `'path'` and the second argument `2`, `lookup` will
return an iterator over all the second arguments `B` such that there is a fact
`path 2 B`.

Expand All @@ -74,7 +74,7 @@ const dusa = new Dusa(`
edge 3 4.
path X Y :- edge X Y.
path X Z :- edge X Y, path Y Z.`);
for (const [b] of dusa.solution.lookup('path', 2n)) {
for (const [b] of dusa.solution.lookup('path', 2)) {
console.log(`Path from 2 to ${b}`);
}
```
Expand Down

0 comments on commit 6286561

Please sign in to comment.