Sha256: 62ff404e503e2b05c831d0dc2240f674d698b99ee7fd76733a82968698f683d0

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 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| Maybe.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| Maybe.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| Maybe.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
        self.match do |on|
          on.just {|v| v }
          on.nothing { nil }
        end
      end
      
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
funkr-0.0.10 lib/funkr/types/maybe.rb
funkr-0.0.9 lib/funkr/types/maybe.rb
funkr-0.0.8 lib/funkr/types/maybe.rb
funkr-0.0.7 lib/funkr/types/maybe.rb
funkr-0.0.6 lib/funkr/types/maybe.rb