urbit/base/pub/doc/hoon/runes/br/brcn.md
2015-04-29 18:48:45 -04:00

3.9 KiB

barcen, |%, %brcn

Build Core

|% is a natural rune that produces a core. |% takes a list of arms . The list must be closed with a --.

The product of |% is similar to an object with named properties containing either functions or data. A |% accepts both dry or %elm and wet or %ash arms. For more about variance, see the glossary.

See also

barcab, |_, %brcb barfas, |/, %brfs

Produces

Twig: [%brcn p=(map term foot)]

Sample

p is a map with ++term keys and ++foot values, which are called arms.

Tall form

|%  
++  p.n.q
  q.n.q
+-  p.n.l.q
  q.n.l.q
--

Wide form

None

Irregular form

None

Examples

/~zod/try=> 
=a  |%
    ++  n  100
    ++  g  |=  b=@
            (add b n)
    --
new var %a
/~zod/try=> 
(g.a 1)
101

Here we create a core with two arms n, a constant and g, a simple funciton. g adds our constant n to whatever is passed to it.

/~zod/try=> 
=a  |%
    ++  l  |%
           ++  r  100
           ++  s  4
           --
    ++  g  |=  b=@
            (div (add b r:l) s:l)
    --
changed %a
/~zod/try=> 
(g.a 4)
26

Extending our previous example a bit, we nest a core inside our arm l and make our gate g a bit more complicated. g now computes the sum of its argument and the arm r inside l, and divides that by s inside l.

++  yo
      |%  ++  cet  36.524                 ::  (add 24 (mul 100 365))
          ++  day  86.400                 ::  (mul 24 hor)
          ++  era  146.097                ::  (add 1 (mul 4 cet))
          ++  hor  3.600                  ::  (mul 60 mit)
          ++  jes  106.751.991.084.417    ::  (mul 730.692.561 era)
          ++  mit  60
          ++  moh  `(list ,@ud)`[31 28 31 30 31 30 31 31 30 31 30 31 ~]
          ++  moy  `(list ,@ud)`[31 29 31 30 31 30 31 31 30 31 30 31 ~]
          ++  qad  126.144.001            ::  (add 1 (mul 4 yer))
          ++  yer  31.536.000             ::  (mul 365 day)
      --

++yo, found in hoon.hoon, uses |% to create a core whose arms contain constant data for calculating time. As the following examples shows, |% is also used to encapsulate arms that perform calculations.

    ++  si                                                  ::  signed integer
      |%
      ++  abs  |=(a=@s (add (end 0 1 a) (rsh 0 1 a)))
      ++  dif  |=([a=@s b=@s] (sum a (new !(syn b) (abs b))))
      ++  dul  |=([a=@s b=@] =+(c=(old a) ?:(-.c (mod +.c b) (sub b +.c))))
      ++  fra  |=  [a=@s b=@s]
               (new =(0 (mix (syn a) (syn b))) (div (abs a) (abs b)))
      ++  new  |=([a=? b=@] `@s`?:(a (mul 2 b) ?:(=(0 b) 0 +((mul 2 (dec b))))))
      ++  old  |=(a=@s [(syn a) (abs a)])
      ++  pro  |=  [a=@s b=@s]
               (new =(0 (mix (syn a) (syn b))) (mul (abs a) (abs b)))
      ++  rem  |=([a=@s b=@s] (dif a (pro b (fra a b))))
      ++  sum  |=  [a=@s b=@s]
               ~|  %si-sum
               =+  [c=(old a) d=(old b)]
               ?:  -.c
                 ?:  -.d
                   (new & (add +.c +.d))
                 ?:  (gte +.c +.d)
                   (new & (sub +.c +.d))
                 (new | (sub +.d +.c))
               ?:  -.d
                 ?:  (gte +.c +.d)
                   (new | (sub +.c +.d))
                 (new & (sub +.d +.c))
               (new | (add +.c +.d))
      ++  sun  |=(a=@u (mul 2 a))
      ++  syn  |=(a=@s =(0 (end 0 1 a)))
      --

++si, found in hoon.hoon, uses |% to create a core whose arms contain gates used to calculate with signed integers, @s. In this case our core is made up entirely of gates.