Class: EasyMonads::Monadic

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/easy_monads/monadic.rb

Direct Known Subclasses

Option::Option

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unit(*args) ⇒ Object



7
8
9
# File 'lib/easy_monads/monadic.rb', line 7

def unit(*args)
  new(*args)
end

Instance Method Details

#<=>(other_monad) ⇒ Object



39
40
41
42
# File 'lib/easy_monads/monadic.rb', line 39

def <=>(other_monad)
  return RuntimeError.new("Can only compare with other Monadic objects") unless other_monad.is_a? EasyMonads::Monadic
  data <=> other_monad.data
end

#==(other_monad) ⇒ Object



34
35
36
37
# File 'lib/easy_monads/monadic.rb', line 34

def ==(other_monad)
  return false unless other_monad.is_a? EasyMonads::Monadic
  data == other_monad.data
end

#bind(&func) ⇒ Object

Raises:

  • (RuntimeError)


16
17
18
19
20
# File 'lib/easy_monads/monadic.rb', line 16

def bind(&func)
  result = func.call(data) if defined? @data
  raise RuntimeError.new("Result of .bind must be a Monadic but was #{result.class.name}") unless result.is_a? Monadic
  result
end

#bind_unit(&func) ⇒ Object



22
23
24
# File 'lib/easy_monads/monadic.rb', line 22

def bind_unit(&func)
  self.bind { |*args| self.unit(func.call(*args)) }
end

#dataObject



26
27
28
# File 'lib/easy_monads/monadic.rb', line 26

def data
  @data
end

#each {|data| ... } ⇒ Object

Yields:



30
31
32
# File 'lib/easy_monads/monadic.rb', line 30

def each
  yield data
end

#unit(to_wrap) ⇒ Object



12
13
14
# File 'lib/easy_monads/monadic.rb', line 12

def unit(to_wrap)
  self.class.unit(to_wrap)
end