on 492ae2f help sign in
86 lines 2.6 kB history raw
1---
2sort: 7
3---
5# `:door`, `|_`, "buccab", `{$door p/moss q/(map term twig)}`
7Door: core with sample (input values).
9Produces: a core whose sample is `p` and arms are `q`.
11Regular form: *1-fixed*, then *battery*
13Similar to `|%`, but also takes input data (aka sample)
14available within the entire core. Cores of this type are called doors.
15Moreover, `|_` only accepts dry or `%elm` arms--in other words,
16type checking on these arms is performed on the input before computation.
18Examples:
20 /~zod/try=> =mol
21 |_ a/@ud
22 ++ succ +(a)
23 ++ prev (dec a)
24 --
25 /~zod/try=> ~(succ mol 1)
26 2
27 /~zod/try=> ~(succ mol ~(succ mol ~(prev mol 5)))
28 6
30In this example we create a door `mol` that operates on a `@ud`,
31`a`. We add two arms to our door, `++succ` and `++prev`, and invoke them
32with the irregular form of `%~`. Doors are commonly invoked with
33`%~`, irregular form `~(arm door sample)`, which replaces the door's
34sample and calls the specified arm.
36 /~zod/try=> =kom
37 |_ a/(list)
38 ++ hed -.a
39 ++ tal +.a
40 --
41 new var %kom
42 /~zod/try=> =kot
43 |/ a=(list)
44 +- hed -.a
45 +- tal +.a
46 --
47 new var %kot
48 /~zod/try=> ~(tal kom "abc")
49 t=~[98 99]
50 /~zod/try=> ~(tal kot "abc")
51 t="bc"
52 /~zod/try=> ~(tal kot [1 2 3 ~])
53 [2 3 ~]
54 /~zod/try=> ~(tal kom [1 2 3 ~])
55 t=~[2 3]
57Here we're demonstrating the difference between `|_` and `|/`. We create
58a nearly identical door using both runes, each with an arm that produces
59the tail of the sample `a`. You can see that wet functions use the
60sample as a mold to produce well-typed output.
62 ++ ne
63 |_ tig=@
64 ++ d (add tig '0')
65 ++ x ?:((gte tig 10) (add tig 87) d)
66 ++ v ?:((gte tig 10) (add tig 87) d)
67 ++ w ?:(=(tig 63) '~' ?:(=(tig 62) '-' ?:((gte tig 36) (add tig 29) x)))
68 --
69 ::
71`++ne` is used to print a single digit in base 10, 16, 32, or 64 and is
72a part of the hoon standard library. You can find it in `hoon.hoon`.
73`|_` is very commonly used throughout our standard library for groups of
74arms who all take the same sample.
76 ~zod/try=> ~(x ne 12)
77 99
78 ~zod/try=> `@t`~(x ne 12)
79 'c'
80 ~zod/try=> `@ux`12
81 0xc
83Here we put `++ne` to work a bit. Our first call renders 12 in base 16.
84Since `99` is within the ASCII character range, we can cast it to a
85`@t` and get `'c'`. Conveniently, casting `12` to a `@ux`
86results in `0xc`.