Skip to content

Commit 7da9189

Browse files
pirates-path example uses until (#218)
1 parent 317d8d5 commit 7da9189

7 files changed

Lines changed: 33 additions & 17 deletions

File tree

concepts/deques/about.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ A breadth-first search is the canonical FIFO use:
4444
<dlist> :> frontier
4545
start visited adjoin
4646
start frontier push-back
47-
[ frontier deque-empty? not ] [
47+
[ frontier deque-empty? ] [
4848
frontier pop-front graph at [
4949
dup visited in? [ drop ] [
5050
[ visited adjoin ] [ frontier push-back ] bi
5151
] if
5252
] each
53-
] while
53+
] until
5454
visited ;
5555
```
5656

exercises/concept/factory-failsafe/.docs/introduction.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,19 @@ A no-slot error is just `ERROR: name ;` — calling `name` throws an
6262
empty instance. Tuples defined with `ERROR:` carry a class predicate
6363
`name?` you can use to test whether a caught error was of that type.
6464

65+
## Reading slots back out
66+
67+
A caught error is just a tuple, so each `ERROR:` slot comes with the
68+
same auto-generated `slot>>` reader as any other tuple.
69+
70+
```factor
71+
! DOCTEST: SKIP
72+
{
73+
T{ not-found { path "/a" } }
74+
T{ not-found { path "/b" } }
75+
}
76+
[ path>> ] map . ! => { "/a" "/b" }
77+
```
78+
6579
[kernel]: https://docs.factorcode.org/content/vocab-kernel.html
6680
[continuations]: https://docs.factorcode.org/content/vocab-continuations.html

exercises/concept/factory-failsafe/.meta/design.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ handling with `recover`, and defining error classes with `ERROR:`.
2727

2828
- `tuples` — taught in `role-playing-game`.
2929
- `conditionals` — taught in `cars-assemble`.
30+
- `higher-order-sequences` — taught in `boutique-bookkeeping`.
31+
- `combinators` — taught in `joiners-journey`.

exercises/concept/pirates-path/.docs/hints.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010

1111
## 1. Drain the tide queue
1212

13-
- Start with `<dlist>`, `[ push-back ] each` to fill it, then a
14-
`while` loop that pops the front until the deque is empty,
15-
accumulating into a `V{ } clone` and converting to an array
16-
at the end with `>array`.
13+
- Start with `<dlist>`, `[ push-back ] each` to fill it, then
14+
drain it with an `until` loop (`[ deque-empty? ]` predicate):
15+
pop the front into a `V{ } clone`, converting to an array at
16+
the end with `>array`.
1717

1818
## 2. Coves reachable
1919

2020
- Pattern: `HS{ } clone` for `visited`, `<dlist>` for
2121
`frontier`, mark `start` visited and push it onto the
22-
frontier, then loop while the frontier isn't empty: pop the
22+
frontier, then loop until the frontier is empty: pop the
2323
front, look up its neighbours in the chart with `at`, and for
2424
each unseen neighbour adjoin to `visited` and push-back onto
2525
the frontier.
@@ -29,8 +29,8 @@
2929
- Same shape as task 2, but the deque carries `{ cove distance }`
3030
pairs. When you pop a pair whose cove equals the goal, record
3131
the distance as the answer and stop. Use a mutable local
32-
(`f :> answer!`) and gate the loop on
33-
`frontier deque-empty? not answer not and`.
32+
(`f :> answer!`) and gate the `until` loop on
33+
`frontier deque-empty? answer or`.
3434

3535
## 4. Gold count
3636

exercises/concept/pirates-path/.docs/introduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ candidates from the front, and dedup with a hash-set:
5050
USING: deques dlists hash-sets sets ;
5151
5252
! ... visited adjoin and frontier push-back ...
53-
[ frontier deque-empty? not ] [
53+
[ frontier deque-empty? ] [
5454
frontier pop-front ! ... look up neighbours, push unseen ones ...
55-
] while
55+
] until
5656
```
5757

5858
Older candidates are visited before newer ones — so the search

exercises/concept/pirates-path/.meta/design.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ neighbour function" pattern from the ground up.
5151
up neighbours.
5252
- `locals` — taught in `lasagna-luminary`. `::` and `:>` keep
5353
the multi-state BFS body readable.
54-
- `while` — taught in `mixed-juices`. The BFS loop.
54+
- `until` — taught in `mixed-juices`. The BFS loop.
5555

5656
## Tasks ramp
5757

exercises/concept/pirates-path/.meta/exemplar.factor

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ IN: pirates-path
66
<dlist> :> q
77
items [ q push-back ] each
88
V{ } clone
9-
[ q deque-empty? not ] [ q pop-front over push ] while
9+
[ q deque-empty? ] [ q pop-front over push ] until
1010
>array ;
1111

1212
:: coves-reachable ( start chart -- coves )
1313
HS{ } clone :> visited
1414
<dlist> :> frontier
1515
start visited adjoin
1616
start frontier push-back
17-
[ frontier deque-empty? not ] [
17+
[ frontier deque-empty? ] [
1818
frontier pop-front chart at [
1919
dup visited in? [ drop ] [
2020
[ visited adjoin ] [ frontier push-back ] bi
2121
] if
2222
] each
23-
] while
23+
] until
2424
visited ;
2525

2626
:: hop-count ( start goal chart -- n/f )
@@ -29,7 +29,7 @@ IN: pirates-path
2929
f :> answer!
3030
start visited adjoin
3131
{ start 0 } frontier push-back
32-
[ frontier deque-empty? not answer not and ] [
32+
[ frontier deque-empty? answer or ] [
3333
frontier pop-front first2 :> ( cove dist )
3434
cove goal = [ dist answer! ] [
3535
cove chart at [
@@ -39,7 +39,7 @@ IN: pirates-path
3939
] if
4040
] each
4141
] if
42-
] while
42+
] until
4343
answer ;
4444

4545
CONSTANT: gold-distribution H{

0 commit comments

Comments
 (0)