class Dry::Monads::Lazy
Lazy
is a twin of Task
which is always executed on the current thread. The underlying mechanism provided by concurrent-ruby ensures the given computation is evaluated not more than once (compare with the built-in lazy assignement ||= which does not guarantee this).
Private Class Methods
new(promise = nil, &block)
click to toggle source
@private
Calls superclass method
# File lib/dry/monads/lazy.rb, line 16 def new(promise = nil, &block) if promise super(promise) else super(Concurrent::Promise.new(executor: :immediate, &block)) end end
Private Instance Methods
evaluated?()
click to toggle source
@return [Boolean]
# File lib/dry/monads/lazy.rb, line 45 def evaluated? @promise.complete? end
force()
click to toggle source
Forces the computation. Note that if the computation thrown an error it won’t be re-raised as opposed to value!/force!.
@return [Lazy]
# File lib/dry/monads/lazy.rb, line 39 def force @promise.execute self end
to_s()
click to toggle source
@return [String]
# File lib/dry/monads/lazy.rb, line 53 def to_s state = case promise.state when :fulfilled value!.inspect when :rejected "!#{promise.reason.inspect}" else "?" end "Lazy(#{state})" end
Also aliased as: inspect
value!()
click to toggle source
Forces the compution and returns its value.
@return [Object]
# File lib/dry/monads/lazy.rb, line 30 def value! @promise.execute.value! end
Also aliased as: force!