Class: Mon::Monad::Final

Inherits:
Lazy show all
Defined in:
lib/monads/lazy.rb

Overview

Final represents a finalized lazy value (i.e. a value with no pending ops).

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Lazy

eventually, valid?

Methods included from ChainableMonad

#coerce, #method_missing, #respond_to?

Constructor Details

#initialize(obj) ⇒ Final

Returns a new instance of Final.



66
67
68
# File 'lib/monads/lazy.rb', line 66

def initialize(obj)
  @obj = obj
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mon::Monad::ChainableMonad

Class Method Details

.[](obj = nil) ⇒ Object

You probably want Lazy



71
72
73
# File 'lib/monads/lazy.rb', line 71

def self::[](obj = nil)
  Final.new(obj)
end

Instance Method Details

#==(o) ⇒ Object



120
121
122
# File 'lib/monads/lazy.rb', line 120

def == o
  eql? o
end

#_Object

Alias for #unwrap



99
100
101
# File 'lib/monads/lazy.rb', line 99

def _
  unwrap
end

#_canBind?(name) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/monads/lazy.rb', line 85

def _canBind?(name)
  @obj.respond_to? name
end

#_finalized?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/monads/lazy.rb', line 89

def _finalized?
  true
end

#bind(&fun) ⇒ Object

Add an operation to be performed on the wrapped value, only if necessary



76
77
78
# File 'lib/monads/lazy.rb', line 76

def bind(&fun)
  Pending::eventually(fun, [@obj])
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
# File 'lib/monads/lazy.rb', line 107

def eql? o
  # Time to collapse
  if o.is_a? Lazy
    self.unwrap == o.unwrap
  else
    self.unwrap == o
  end
end

#equals?(o) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/monads/lazy.rb', line 116

def equals? o
  eql? o
end

#finalizeObject

Perform any pending lazy operations



81
82
83
# File 'lib/monads/lazy.rb', line 81

def finalize
  self
end

#to_sObject



103
104
105
# File 'lib/monads/lazy.rb', line 103

def to_s
  "Final[#{ @obj }]"
end

#unwrapObject

Perform any pending lazy operations and unwrap the result



94
95
96
# File 'lib/monads/lazy.rb', line 94

def unwrap
  @obj
end