Module: Core
- Included in:
- F
- Defined in:
- lib/fubby/core.rb
Instance Method Summary collapse
-
#compose ⇒ Object
TODO: figure out how to notate many args.
-
#curry ⇒ Object
- curry
-
(a -> b) -> (a -> b).
-
#flip ⇒ Object
- flip
-
(a -> b) -> (a -> b).
-
#identity ⇒ Object
- identity
-
a -> a.
-
#reduce ⇒ Object
- reduce
-
a, (b -> a), [b] -> a.
Instance Method Details
#compose ⇒ Object
TODO: figure out how to notate many args. [f] isn’t correct here.
- compose
- f
-
-> (a -> b)
44 45 46 47 48 49 50 51 52 |
# File 'lib/fubby/core.rb', line 44 def compose ->(*f) { _compose = ->(x, *f) { f.length == 1 ? f[0].(x) : f[0].(_compose.(x, *f[1..-1])) } ->(x) { _compose.(x, *f) } } end |
#curry ⇒ Object
- curry
-
(a -> b) -> (a -> b)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/fubby/core.rb', line 8 def curry _curry = ->(f, *given_args) { ->(*remaining_args) { total_args = given_args + remaining_args total_args.length == f.arity ? f.(*total_args) : _curry.(f, *total_args) } } ->(f) { ->(*x) { f.arity == x.length ? f.(*x) : _curry.(f, *x) } } end |
#flip ⇒ Object
- flip
-
(a -> b) -> (a -> b)
55 56 57 58 59 60 61 |
# File 'lib/fubby/core.rb', line 55 def flip ->(f) { ->(*args) { f.(*reverse.(args)) } } end |
#identity ⇒ Object
- identity
-
a -> a
3 4 5 |
# File 'lib/fubby/core.rb', line 3 def identity ->(x) { x } end |
#reduce ⇒ Object
- reduce
-
a, (b -> a), [b] -> a
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/fubby/core.rb', line 25 def reduce curry.(->(memo, f, array) { if memo == nil case array.length when 0 then nil when 1 then array[0] else reduce.(f.(array[0], array[1]), f, array[2..-1]) end else case array.length when 0 then memo else reduce.(f.(memo, array[0]), f, array[1..-1]) end end }) end |