Last active
June 15, 2016 03:03
-
-
Save joemfb/320c7df23f0354ac6c2e72a15b0282a2 to your computer and use it in GitHub Desktop.
Hoon set exercises (from http://urbit.org/docs/hoon/exercises/data-structures/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- %say |= * :- %noun | |
:: | |
:::: sets (http://urbit.org/docs/hoon/exercises/data-structures/) | |
:: | |
=+ s=(silt `(list @t)`['urbit' 'arvo' 'hoon' 'arvo' ~]) | |
~& 'original set' | |
~& s | |
:: | |
:::: #1 | |
:: | |
=+ l=(silt `(list @t)`['c' 'hoon' 'javascript' 'python' ~]) | |
~& 'new set' | |
~& l | |
:: | |
:::: #2 | |
:: | |
=+ ^= u | |
(~(uni in s) l) | |
=+ ^= m | |
(~(del in u) 'javascript') | |
=+ ^= m | |
(~(put in m) 'coffeescript') | |
~& 'mutated union' | |
~& m | |
:: | |
:::: #3 | |
:: | |
=+ ^= head-char | |
|=(a/@ `@`(snag 0 (trip a))) | |
=+ ^= comp | |
|=({a/@ b/@} (gth (head-char b) (head-char a))) | |
~& 'sorted' | |
~& (sort (~(tap in m)) comp) | |
:: | |
:::: #4 | |
:: | |
=< | |
=+ ^= cart1 | |
(cartesian-product1 s l) | |
=+ ^= cart2 | |
(cartesian-product2 s l) | |
~& 'cartesian product' | |
~& cart1 | |
~& cart2 | |
~ | |
:: | |
|% | |
++ cartesian-product1 | |
|= {a/(set @t) b/(set @t)} | |
=+ r=*(set {@t @t}) | |
|- ?~ a r | |
=+ b=b :: shadow b for inner loop walkthru | |
|- ?~ b | |
=+ ^= r | |
^$(a l.a, r r) | |
^$(a r.a, r r) | |
=+ ^= r | |
(~(put in r) [n.a n.b]) | |
=+ ^= r | |
$(b l.b, r r) | |
$(b r.b, r r) | |
++ cartesian-product2 | |
|= {s/(set @t) l/(set @t)} | |
=+ ^= lists | |
^- (list (list {@t @t})) | |
%+ turn (~(tap in s)) | |
|= a/@t | |
%+ turn (~(tap in l)) | |
|= b/@t [a b] | |
%- silt ^- (list {@t @t}) | |
%+ roll lists | |
|= {l/(list {@t @t}) r/(list {@t @t})} | |
(weld l r) | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment