module Arrows
Constants
- Die
- Evil
- Good
- ID
- VERSION
Public Class Methods
arrow_like?(x)
click to toggle source
# 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)
click to toggle source
# File lib/arrows.rb, line 37 def compose(f,g) Arrows::Proc.new { |args| g[f[args]] } end
concurrent(f,g)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/arrows.rb, line 21 def evil_fork(g) fork ID, g end
fanout(f,g)
click to toggle source
# File lib/arrows.rb, line 34 def fanout(f,g) Arrows::Proc.new { |args| [f[args], g[args]] } end
feedback(merge, split)
click to toggle source
# 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)
click to toggle source
# File lib/arrows.rb, line 40 def fmap(xs, f) Arrows::Proc.new { |args| xs[args].map { |x| f[x] } } end
fork(f,g)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/arrows.rb, line 18 def good_fork(f) fork f, ID end
lift(x=nil) { |args| ... }
click to toggle source
# 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) { |args| ... }
click to toggle source
# 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)
click to toggle source
# File lib/arrows.rb, line 71 def proc_like?(x) x.respond_to?(:call) && x.respond_to?(:arity) end
wrap_proc(f)
click to toggle source
# File lib/arrows.rb, line 74 def wrap_proc(f) Arrows::Proc.new do |args| f[args] end end