Sha256: 01a6557059e5e4aba0267cae7eacbf2b7fef9760762b2f0a8a4f740f82ce9f68
Contents?: true
Size: 1.72 KB
Versions: 1
Compression:
Stored size: 1.72 KB
Contents
module Dry module Monads class Maybe def self.lift(value) if value.nil? None.instance else Some.new(value) end end def ==(other) other.is_a?(Maybe) && value == other.value end def none? is_a?(None) end def some? is_a?(Some) end def to_maybe self end class Some < Maybe attr_reader :value def initialize(value) raise ArgumentError, 'nil cannot be some' if value.nil? @value = value end def bind(proc = nil) if proc proc.call(value) else yield(value) end end def fmap(proc = nil, &block) self.class.lift(bind(&(proc || block))) end def or(_val = nil) self end def to_s "Some(#{value.inspect})" end alias inspect to_s end class None < Maybe @instance = new singleton_class.send(:attr_reader, :instance) def value nil end def bind(_proc = nil) self end def fmap(_proc = nil) self end def or(val = nil) if block_given? yield(value) else val end end def to_s 'None' end alias inspect to_s end module Mixin Maybe = Maybe Some = Some None = None def Maybe(value) Maybe.lift(value) end def Some(value) Some.new(value) end def None None.instance end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-monads-0.0.2 | lib/dry/monads/maybe.rb |