Class: Fr::Either

Inherits:
Object show all
Extended by:
Monad, Monoid
Defined in:
lib/fr/either.rb

Direct Known Subclasses

Left, Right

Defined Under Namespace

Classes: Left, Right

Class Method Summary collapse

Methods included from Monoid

filter, guard, sum

Methods included from Monad

applyM, composeM, filterM, foldM, forM, join, liftM, mapM, sequence, unlessM, whenM, zipM

Methods included from Functor

#void

Class Method Details

.bind(x, &f) ⇒ Object



13
14
15
# File 'lib/fr/monad/either.rb', line 13

def bind(x, &f)
  x.fold(lambda{|l| x }, lambda{|r| f.call(r) })
end

.lefts(es) ⇒ Object

Either a b

-> [a]



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

def lefts(es)
  es.inject([]) do |ls,e|
    e.fold(lambda{|l| ls.push(l) }, lambda{|_| ls })
  end
end

.partition(es) ⇒ Object

Either a b

-> [a] -> [b]



21
22
23
24
25
26
# File 'lib/fr/either.rb', line 21

def partition(es)
  es.inject([[],[]]) do |(ls,rs),e|
    e.fold(lambda{|l| [ls.push(l), rs] },
           lambda{|r| [ls, rs.push(r)] })
  end
end

.plus(a, b) ⇒ Object



9
10
11
# File 'lib/fr/monoid/either.rb', line 9

def plus(a, b)
  (a.left?) ? b : a
end

.rights(es) ⇒ Object

Either a b

-> [b]



14
15
16
17
18
# File 'lib/fr/either.rb', line 14

def rights(es)
  es.inject([]) do |rs,e|
    e.fold(lambda{|_| rs }, lambda{|r| rs.push(r) })
  end
end

.unit(value) ⇒ Object

Combinators



9
10
11
# File 'lib/fr/monad/either.rb', line 9

def unit(value)
  Either::Right.new(value)
end

.zeroObject



5
6
7
# File 'lib/fr/monoid/either.rb', line 5

def zero
  Either::Left.new(nil)
end