Sha256: a0c214362b9c10f1ad2983fb96f82b33dd7baebdd8764f9ab5fc0a6527d7c064

Contents?: true

Size: 902 Bytes

Versions: 11

Compression:

Stored size: 902 Bytes

Contents

require 'funkr/categories'

class Array
  
  include Funkr::Categories
  
  class << self
    def unit(e); self.new([e]); end
    alias pure unit
    def mzero; self.new(); end
  end
  
  ### Categories
  
  # array est déjà un functor via map
  # include Functor 
  
  
  include Applicative
  extend Applicative::ClassMethods
  
  def apply(to)
    map do |f|
      to.map{ |t| f.call(t)}
    end.flatten(1)
  end
  
  include Alternative
  
  def or_else(&block)
    if empty? then yield
    else self end
  end
  
  
  include Monoid
  extend Monoid::ClassMethods
  
  def mplus(m_y)
    self + m_y
  end
  
  
  include Monad
  extend Monad::ClassMethods
  
  def bind(&block)
    self.map(&block).flatten(1)
  end
  
  def self.box(value)
    if value.nil? then self.mzero
    else self.unit(value) end
  end
  
  def unbox()
    if self.empty? then nil
    else self.first end
  end
  
  
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
funkr-0.0.21 lib/funkr/extensions/array.rb
funkr-0.0.20 lib/funkr/extensions/array.rb
funkr-0.0.19 lib/funkr/extensions/array.rb
funkr-0.0.18 lib/funkr/extensions/array.rb
funkr-0.0.17 lib/funkr/extensions/array.rb
funkr-0.0.16 lib/funkr/extensions/array.rb
funkr-0.0.15 lib/funkr/extensions/array.rb
funkr-0.0.14 lib/funkr/extensions/array.rb
funkr-0.0.12 lib/funkr/extensions/array.rb
funkr-0.0.11 lib/funkr/extensions/array.rb
funkr-0.0.10 lib/funkr/extensions/array.rb