on master help sign in
adds docs for new gates ++spin and ++spun
commit2638fec81832289c78612f2f111d7c596276acc9 authorJoseph Bryan <joemfb@gmail.com> dateThu 7 Jul 2016, 12:51 (10 years ago) parent96d6fea
1 file changed+126
docs/hoon/library/2b.md+126
docs/hoon/library/2b.md+126
@@ -881,6 +881,132 @@ Examples
881881
882882
883883
884+***
885+### `++spin`
886+
887+Gate to list, with state
888+
889+Accepts a `++list` `a`, a gate `b`, and some state `c`. Produces a list with
890+the gate applied to each element of the original list. `b` is called with
891+a tuple -- the head is an element of `a` and the tail is the state `c`, and
892+should produce a tuple of the transformed element and the (potentially
893+modified) state `c`.
894+
895+Accepts
896+-------
897+
898+`a` is a `++list`.
899+
900+`b` is a gate.
901+
902+`c` is a noun.
903+
904+Produces
905+--------
906+
907+A list.
908+
909+Source
910+------
911+
912+ ++ spin
913+ |* {a/(list) b/_|=({* *} [** +<+]) c/*}
914+ :: ?< ?=($-([_?<(?=($~ a) i.a) _c] [* _c]) b)
915+ |-
916+ ?~ a
917+ ~
918+ =+ v=(b i.a c)
919+ [i=-.v t=$(a t.a, c +.v)]
920+
921+
922+Examples
923+--------
924+
925+A trivial example -- does nothing with the state:
926+
927+ > %^ spin (limo ~[4 5 6])
928+ |=({n/@ a/@} [n a])
929+ 0
930+ ~[4 5 6]
931+
932+Create a list of pairs, `p` as the index and `q` as the list element:
933+
934+ > %^ spin (limo ~[4 5 6])
935+ |=({n/@ a/@} [`(pair)`[a n] +(a)])
936+ 0
937+ ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
938+
939+Create 10 random numbers less than `10`:
940+
941+ > %^ spin (reap 10 0)
942+ |=({n/@ rng/_og} (rads:rng 10))
943+ ~(. og eny)
944+ ~[5 5 9 4 1 7 9 9 9 6]
945+
946+*Notes*:
947+
948+`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
949+with entropy (`eny`). The head of the product is the random number, the tail
950+is the continuation of the RNG.
951+
952+
953+
954+***
955+### `++spun`
956+
957+Gate to list, with state
958+
959+Accepts a `++list` `a` and a gate `b`. `c` is internal state, initially
960+derived by *bunting* the tail of the sample of gate `b`, instead of
961+being passed in explicitly as in `++spin`. Produces a list with the
962+gate applied to each element of the original list. `b` is called with a tuple
963+-- the head is an element of `a` and the tail is the state `c`, and should
964+produce a tuple of the transformed element and the (potentially modified)
965+state `c`.
966+
967+Accepts
968+-------
969+
970+`a` is a `++list`.
971+
972+`b` is a gate.
973+
974+Produces
975+--------
976+
977+A list.
978+
979+Source
980+------
981+
982+ ++ spun
983+ |* {a/(list) b/_|=({* *} [** +<+])}
984+ =| c/_+<+.b
985+ |-
986+ ?~ a
987+ ~
988+ =+ v=(b i.a c)
989+ [i=-.v t=$(a t.a, c +.v)]
990+
991+
992+Examples
993+--------
994+
995+Create a list of pairs, `p` as the index and `q` as the list element:
996+
997+ > %+ spun (limo ~[4 5 6])
998+ |=({n/@ a/@} [`(pair)`[a n] +(a)])
999+ ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1000+
1001+Join two lists into a list of pairs, `p` from `l` and `q` from the sample:
1002+
1003+ > =l (limo ~[7 8 9])
1004+ > %+ spun (limo ~[4 5 6])
1005+ |=({n/@ a/@} [`(pair)`[(snag a l) n] +(a)])
1006+ ~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1007+
1008+
1009+
8841010***
8851011### `++weld`
8861012