Sha256: c6758f11ac83553ad9d5e25af874bd01aa095dabe60ab38b9059625fb40c30a3
Contents?: true
Size: 1.85 KB
Versions: 3
Compression:
Stored size: 1.85 KB
Contents
require 'funkr/adt/adt' require 'funkr/categories' module Funkr module Types class Maybe < ADT include Funkr::Categories adt :just, :nothing ### Categories include Functor def map(&block) self.match do |on| on.just {|v| self.class.just(yield(v))} on.nothing { self } end end include Applicative extend Applicative::ClassMethods def apply(to) self.match do |f_on| f_on.just do |f| to.match do |t_on| t_on.just {|t| self.class.unit(f.call(t)) } t_on.nothing { to } end end f_on.nothing { self } end end include Alternative def or_else(&block) self.match do |on| on.just {|v| self} on.nothing { yield } end end include Monoid extend Monoid::ClassMethods def mplus(m_y) self.match do |x_on| x_on.nothing { m_y } x_on.just do |x| m_y.match do |y_on| y_on.nothing { self } y_on.just {|y| self.class.just(x.mplus(y))} end end end end include Monad extend Monad::ClassMethods def bind(&block) self.match do |on| on.just {|v| yield(v)} on.nothing {self} end end class << self alias unit just alias pure just alias mzero nothing end def self.box(value) if value.nil? then self.nothing else self.just(value) end end def unbox(default=nil) self.match do |on| on.just {|v| v } on.nothing { default } end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
funkr-0.0.20 | lib/funkr/types/maybe.rb |
funkr-0.0.19 | lib/funkr/types/maybe.rb |
funkr-0.0.18 | lib/funkr/types/maybe.rb |