Skip to content

Instantly share code, notes, and snippets.

@refaktor
Last active February 14, 2022 16:33
Show Gist options
  • Save refaktor/9a97ca5c024800a5d73cc4546935d022 to your computer and use it in GitHub Desktop.
Save refaktor/9a97ca5c024800a5d73cc4546935d022 to your computer and use it in GitHub Desktop.
HOFs test
;
; # Rye HOFs demo
;
numbers: { 1 2 3 4 }
;
; map
;
numbers |map { + 100 } |prn ; add 100 to all numbers
numbers |map { .factor-of 2 } |prn ; turn to 1 if even and 0 otherwise
numbers |map { :x , x * x } |prn ; run to it's squares
numbers |map ?inc |prn ; return all incremented by 1
;
; filter & seek
;
even: factor-of _ 2 ; experimental partial application
numbers |filter { > 2 } ; return values greater than 3
numbers |filter ?even ; return just even values
numbers |seek { :x all { x > 2 even x } } ; find first greater than 2 and even
;
; reduce & fold
;
numbers |reduce 'acc { * acc } ; factorial of 4
numbers |reduce 'acc { .str + acc } ; reverse to string
numbers |fold 'acc 0 { .even .either { inc acc } { acc } } ; count even numbers
numbers |fold 'acc { } { :x , acc + eval { x x } } ; double values
:doubles |print ; save to doubles
;
; partition
;
doubles |partition { , } |print ; partition same together
doubles |partition { > 2 } |print ; partition same together
;
; group
;
numbers |group { .even .str } |print
{ { 100 "Jim" } { 30 "Matt" } { 70 "Zoe" } } :players
|group { .first > 50 |either { "pass" } { "fail" } }
|print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment