Sha256: 6fe0c7387bbb2bca7c0323e2f57db3f9982c113da68a3e063618e14d3198fc76

Contents?: true

Size: 593 Bytes

Versions: 11

Compression:

Stored size: 593 Bytes

Contents

# Adapted from http://stackoverflow.com/questions/2709361/monad-equivalent-in-ruby
class LazyIdentity # :nodoc:
  def initialize(lam = nil, &blk)
    @lazy = lam || blk
    @lazy.is_a?(Proc) || raise(ArgumentError, "not a Proc")
    @lazy.arity.zero? || raise(ArgumentError, "arity must be 0, was #{@lazy.arity}")
  end

  attr_reader :lazy

  def force
    @lazy[]
  end

  def self.unit(lam = nil, &blk)
    LazyIdentity.new(lam || blk)
  end

  def bind(lam = nil, &blk)
    f = lam || blk
    f[@lazy]
  end

  def ==(other)
    other.is_a?(LazyIdentity) && other.force == force
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rumonade-0.4.4 lib/rumonade/lazy_identity.rb
rumonade-0.4.3 lib/rumonade/lazy_identity.rb
rumonade-0.4.2 lib/rumonade/lazy_identity.rb
rumonade-0.4.1 lib/rumonade/lazy_identity.rb
rumonade-0.4.0 lib/rumonade/lazy_identity.rb
rumonade-0.3.0 lib/rumonade/lazy_identity.rb
rumonade-0.2.2 lib/rumonade/lazy_identity.rb
rumonade-0.2.1 lib/rumonade/lazy_identity.rb
rumonade-0.2.0 lib/rumonade/lazy_identity.rb
rumonade-0.1.2 lib/rumonade/lazy_identity.rb
rumonade-0.1.1 lib/rumonade/lazy_identity.rb