Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bin/test-doc-examples
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ IMAGE = "exercism/factor-test-runner"

DEFAULT_USING = (
"arrays ascii assocs combinators continuations fry hashtables "
"io io.streams.string kernel literals math math.bitwise "
"math.functions math.order math.parser math.primes math.statistics "
"namespaces prettyprint quotations sequences sorting splitting "
"strings vectors"
"io io.streams.string kernel literals locals math math.bitwise "
"math.constants math.functions math.order math.parser math.primes "
"math.statistics namespaces prettyprint quotations sequences "
"sorting splitting strings typed vectors"
)

SENTINEL = "###BLOCK-END###"
Expand Down
3 changes: 2 additions & 1 deletion concepts/deques/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ can implement them.
| `pop-back` | `( deque -- elt )` — remove from the back |
| `peek-front` | `( deque -- elt )` — read the front without removing |
| `peek-back` | `( deque -- elt )` — read the back without removing |
| `deque-empty?` | `( deque -- ? )` — also written `empty?` (sequence protocol) |
| `deque-empty?` | `( deque -- ? )` — is the deque empty? |
| `dlist-length` | `( dlist -- n )` — element count |

All push/pop operations mutate the deque in place — passing a
deque to a routine and then reading from it again is the normal
Expand Down
2 changes: 1 addition & 1 deletion concepts/sequences/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ with the others.
| `find-last` | `( seq quot -- i/f elt/f )` |
| `produce` | `( pred quot -- seq )` |

Arrays are immutable; the `prefix`/`suffix`/`append` operations all
Arrays are immutable; the `prefix`/`suffix`/`append`/`prepend` operations all
return new sequences without modifying the original. Vectors are
mutable — `push` and `pop` work in place — but `clone` is the right
starting point if you want a fresh copy.
Expand Down
2 changes: 2 additions & 0 deletions exercises/concept/annalyns-infiltration/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ tuck ( x y -- y x y )
3dup ( x y z -- x y z x y z )
4dup ( w x y z -- w x y z w x y z )
2drop ( x y -- )
3drop ( x y z -- )
4drop ( w x y z -- )
2nip ( x y z -- z )
2swap ( x y z w -- z w x y )
```
Expand Down
53 changes: 41 additions & 12 deletions exercises/concept/bering-bearings/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,34 @@ on the *class* of one of its arguments. Concrete behaviour comes
from *methods*, defined with `M:`.

```factor
USING: kernel math ;

GENERIC: area ( shape -- n )

TUPLE: square side ;
TUPLE: circle radius ;

M: square area side>> dup * ;
M: circle area radius>> dup * pi * ;
M: square area
side>> dup * ;
M: circle area
radius>> dup * pi * ;
```

When `area` is called, Factor inspects the top of the stack,
looks up the most-specific method whose class matches, and runs
it. With locals you can use `M::`:

```factor
USING: locals ;
TUPLE: rectangle width height ;

M:: rectangle area ( r -- n )
r width>> r height>> * ;

M:: square area ( s -- n )
s side>> :> a a a * ;
T{ rectangle { width 3 } { height 4 } } area . ! => 12
```

Generics let you add new representations later without changing
the calling code. Anywhere `area` was called, the new method is
picked up automatically.
the calling code: `area` already worked for `square` and `circle`,
and the `rectangle` method is picked up automatically wherever
`area` is called.

## When a generic is the right choice

Expand All @@ -52,15 +55,42 @@ tuple can opt into the protocol's default methods by declaring
itself an instance:

```factor
TUPLE: my-shape ... ;
INSTANCE: my-shape shape ! my-shape is now a `shape`
MIXIN: shape
M: shape area ! a default area for any shape
drop 0 ;

TUPLE: my-shape ;
INSTANCE: my-shape shape ! my-shape is now a `shape`

T{ my-shape } area . ! => 0
```

After `INSTANCE:`, every default method `M: shape …` automatically
dispatches on `my-shape`, and any code that asks for a `shape`
accepts a `my-shape`. `M:` is for adding methods to a generic;
`INSTANCE:` is for joining a class to a mixin.

## `TYPED::` — checked inputs for a plain word

`GENERIC:`/`M:` choose a method by the class of an argument.
Sometimes you don't want dispatch — just *one* word that insists
its inputs are of a given class. `TYPED::` (in the `typed`
vocabulary) is `::` with a class written after each input name:

```factor
TYPED:: triple ( n: integer -- m )
n 3 * ;

14 triple . ! => 42
```

`n: integer` reads like the locals you already use, with the
class spelled after the colon. There's no dispatch — one
definition, one behaviour — but each argument is now checked
against its class: a mismatch like `"x" triple` is rejected
(*expected input value of type integer but got string*) rather
than silently flowing through.

## Math functions and constants

This exercise also leans on a few words from `math.constants`
Expand All @@ -73,7 +103,6 @@ and `math.functions` that you'll meet for the first time here:
arbitrary base, use `^`.)

```factor
USING: math.constants math.functions ;
pi 2 / sin ! → 1.0
0 cos ! → 1.0
1 e^ ! → 2.718281828459045
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ The exercise needs five small string words from the
- `1string ( char -- str )` — wrap a single character as a string.
- `>upper ( str -- upper )` — uppercase a string.
- `append ( s1 s2 -- s )` — concatenate two strings.
- `prepend ( s1 s2 -- s )` — concatenate `s2` then `s1` (`append` with
its inputs in the other order).
- `glue ( s1 s2 sep -- s )` — concatenate two strings with a separator
between them.
- `surround ( seq pre post -- new )` — wrap a string with a prefix and
Expand Down
2 changes: 2 additions & 0 deletions exercises/concept/joiners-journey/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ tuck ( x y -- y x y )
3dup ( x y z -- x y z x y z )
4dup ( w x y z -- w x y z w x y z )
2drop ( x y -- )
3drop ( x y z -- )
4drop ( w x y z -- )
2nip ( x y z -- z )
2swap ( x y z w -- z w x y )
```
Expand Down
1 change: 1 addition & 0 deletions exercises/concept/pirates-path/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ push-back ( elt deque -- )
pop-front ( deque -- elt )
pop-back ( deque -- elt )
deque-empty? ( deque -- ? )
dlist-length ( dlist -- n )
clear-deque ( deque -- )
```

Expand Down