Module: Arrows

Defined in:
lib/arrows.rb,
lib/arrows/version.rb

Defined Under Namespace

Classes: Either, Proc

Constant Summary collapse

ID =
lift -> (x) { x }
VERSION =
"0.0.7"

Class Method Summary collapse

Class Method Details

.arrow_like?(x) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/arrows.rb', line 39

def arrow_like?(x)
   proc_like?(x) && x.arity == 1 && x.respond_to?(:>=) && x.respond_to?(:>>)
end

.compose(f, g) ⇒ Object



20
21
22
# File 'lib/arrows.rb', line 20

def compose(f,g)
  Arrows::Proc.new { |args| g[f[args]] }
end

.concurrent(f, g) ⇒ Object



12
13
14
15
16
# File 'lib/arrows.rb', line 12

def concurrent(f,g)
  Arrows::Proc.new do |args| 
    [f[args.first], g[args.last]]
  end
end

.evil(x) ⇒ Object



30
31
32
33
# File 'lib/arrows.rb', line 30

def evil(x)
  return x if x.respond_to?(:good?) && x.respond_to?(:payload)
  Arrows::Either.new false, x
end

.fanout(f, g) ⇒ Object



17
18
19
# File 'lib/arrows.rb', line 17

def fanout(f,g)
  Arrows::Proc.new { |args| [f[args], g[args]] }
end

.fmap(xs, f) ⇒ Object



23
24
25
# File 'lib/arrows.rb', line 23

def fmap(xs, f)
  Arrows::Proc.new { |args| xs[args].map { |x| f[x] }  }
end

.fork(f, g) ⇒ Object



7
8
9
10
11
# File 'lib/arrows.rb', line 7

def fork(f,g)
  Arrows::Proc.new do |either|
    either.good? ? f[either.payload] : g[either.payload]
  end
end

.good(x) ⇒ Object



26
27
28
29
# File 'lib/arrows.rb', line 26

def good(x)
  return x if x.respond_to?(:good?) && x.respond_to?(:payload)
  Arrows::Either.new true, x
end

.lift(x) ⇒ Object



34
35
36
37
38
# File 'lib/arrows.rb', line 34

def lift(x)
  return x if arrow_like? x
  return wrap_proc x if proc_like? x
  Arrows::Proc.new { |args| x }
end

.proc_like?(x) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/arrows.rb', line 42

def proc_like?(x)
  x.respond_to?(:call) && x.respond_to?(:arity)
end

.wrap_proc(f) ⇒ Object



45
46
47
48
49
# File 'lib/arrows.rb', line 45

def wrap_proc(f)
  Arrows::Proc.new do |args|
    f[args]
  end
end