Module: Arrows
- Defined in:
- lib/arrows.rb,
lib/arrows/version.rb
Defined Under Namespace
Constant Summary collapse
- ID =
lift { |x| x }
- Good =
lift { |x| good x }
- Evil =
lift { |x| evil x }
- Die =
lift { |x| throw x }
- VERSION =
"0.1.1"
Class Method Summary collapse
- .arrow_like?(x) ⇒ Boolean
- .compose(f, g) ⇒ Object
- .concurrent(f, g) ⇒ Object
- .evil(x = nil) ⇒ Object
- .evil_fork(g) ⇒ Object
- .fanout(f, g) ⇒ Object
- .feedback(merge, split) ⇒ Object
- .fmap(xs, f) ⇒ Object
- .fork(f, g) ⇒ Object
- .good(x = nil) ⇒ Object
- .good_fork(f) ⇒ Object
- .lift(x = nil) ⇒ Object
- .polarize(x = nil) ⇒ Object
- .proc_like?(x) ⇒ Boolean
- .wrap_proc(f) ⇒ Object
Class Method Details
.arrow_like?(x) ⇒ Boolean
62 63 64 65 66 67 68 69 70 |
# File 'lib/arrows.rb', line 62 def arrow_like?(x) proc_like?(x) && x.arity == 1 && x.respond_to?(:>=) && x.respond_to?(:>>) && x.respond_to?(:^) && x.respond_to?(:/) && x.respond_to?(:%) end |
.compose(f, g) ⇒ Object
37 38 39 |
# File 'lib/arrows.rb', line 37 def compose(f,g) Arrows::Proc.new { |args| g[f[args]] } end |
.concurrent(f, g) ⇒ Object
29 30 31 32 33 |
# File 'lib/arrows.rb', line 29 def concurrent(f,g) Arrows::Proc.new do |args| [f[args.first], g[args.last]] end end |
.evil(x = nil) ⇒ Object
47 48 49 50 |
# File 'lib/arrows.rb', line 47 def evil(x=nil) return x if x.respond_to?(:good?) && x.respond_to?(:payload) Arrows::Either.new false, x end |
.evil_fork(g) ⇒ Object
21 22 23 |
# File 'lib/arrows.rb', line 21 def evil_fork(g) fork ID, g end |
.fanout(f, g) ⇒ Object
34 35 36 |
# File 'lib/arrows.rb', line 34 def fanout(f,g) Arrows::Proc.new { |args| [f[args], g[args]] } end |
.feedback(merge, split) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/arrows.rb', line 7 def feedback(merge, split) Arrows::Proc.new do |args| single = merge.call [args] either = split.call single while not either.good? single = merge.call either.payload either = split.call single end either.payload end end |
.fmap(xs, f) ⇒ Object
40 41 42 |
# File 'lib/arrows.rb', line 40 def fmap(xs, f) Arrows::Proc.new { |args| xs[args].map { |x| f[x] } } end |
.fork(f, g) ⇒ Object
24 25 26 27 28 |
# File 'lib/arrows.rb', line 24 def fork(f,g) Arrows::Proc.new do |either| either.good? ? f[either.payload] : g[either.payload] end end |
.good(x = nil) ⇒ Object
43 44 45 46 |
# File 'lib/arrows.rb', line 43 def good(x=nil) return x if x.respond_to?(:good?) && x.respond_to?(:payload) Arrows::Either.new true, x end |
.good_fork(f) ⇒ Object
18 19 20 |
# File 'lib/arrows.rb', line 18 def good_fork(f) fork f, ID end |
.lift(x = nil) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/arrows.rb', line 51 def lift(x=nil) return Arrows::Proc.new { |args| yield args } if block_given? return x if arrow_like? x return wrap_proc x if proc_like? x Arrows::Proc.new { |args| x } end |
.polarize(x = nil) ⇒ Object
57 58 59 60 61 |
# File 'lib/arrows.rb', line 57 def polarize(x=nil) return lift { |args| yield(args) ? good(args) : evil(args) } if block_given? return lift { |args| x.call(args) ? good(args) : evil(args) } if proc_like? x lift { |args| x ? good(args) : evil(args) } end |
.proc_like?(x) ⇒ Boolean
71 72 73 |
# File 'lib/arrows.rb', line 71 def proc_like?(x) x.respond_to?(:call) && x.respond_to?(:arity) end |