Sha256: 3de927387032f29c50190ddc46749513a7b6f20293ff8b245927736c8a8bfd08

Contents?: true

Size: 1.47 KB

Versions: 15

Compression:

Stored size: 1.47 KB

Contents

require 'funkr/adt/adt'
require 'funkr/categories'

module Funkr
  module Types
    class Failable < ADT
      
      include Funkr::Categories
      
      adt :ok, :failed
      
      class << self
        alias unit ok
        alias pure ok
      end
      
      include Functor
      
      def map(&block)
        case self.const
        when :ok then
          Failable.ok(yield(*self.data))
        else self
        end
      end
      
      include Applicative
      
      def apply(to)
        self.match do |f_on|
          f_on.ok do |f|
            to.match do |t_on|
              t_on.ok {|t| Failable.ok(f.call(t)) }
              t_on.failed { to }
            end
          end
          f_on.failed { self }
        end
      end
      
      
      include Alternative
      
      def or_else(&block)
        case self.const
        when :ok then self
        else yield
        end
      end
      
      
      include Monoid
      extend Monoid::ClassMethods
      
      def mplus(m_y)
        self.match do |x_on|
          x_on.failed { m_y }
          x_on.ok do |x|
            m_y.match do |y_on|
              y_on.failed { self }
              y_on.ok {|y| Failable.ok(x.mplus(y))}
            end
          end
        end
      end
      
      
      include Monad
      extend Monad::ClassMethods
      
      def bind(&block)
        case self.const
        when :ok then yield(*self.data)
        else self
        end
      end
      
      
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
funkr-0.0.17 lib/funkr/types/failable.rb
funkr-0.0.16 lib/funkr/types/failable.rb
funkr-0.0.15 lib/funkr/types/failable.rb
funkr-0.0.14 lib/funkr/types/failable.rb
funkr-0.0.12 lib/funkr/types/failable.rb
funkr-0.0.11 lib/funkr/types/failable.rb
funkr-0.0.10 lib/funkr/types/failable.rb
funkr-0.0.9 lib/funkr/types/failable.rb
funkr-0.0.8 lib/funkr/types/failable.rb
funkr-0.0.7 lib/funkr/types/failable.rb
funkr-0.0.6 lib/funkr/types/failable.rb
funkr-0.0.5 lib/funkr/types/failable.rb
funkr-0.0.4 lib/funkr/types/failable.rb
funkr-0.0.3 lib/funkr/types/failable.rb
funkr-0.0.2 lib/funkr/types/failable.rb