Class: Array

Inherits:
Object show all
Extended by:
Applicative::ClassMethods, Monad::ClassMethods, Monoid::ClassMethods
Includes:
Alternative, Applicative, Funkr::Categories, Monad, Monoid
Defined in:
lib/funkr/extensions/array.rb

Overview

Extends array (as list) capabilities

Direct Known Subclasses

Funkr::Types::SimpleRecord

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.box(value) ⇒ Object



63
64
65
66
# File 'lib/funkr/extensions/array.rb', line 63

def self.box(value)
  if value.nil? then self.mzero
  else self.unit(value) end
end

.mzeroObject



11
# File 'lib/funkr/extensions/array.rb', line 11

def mzero; self.new(); end

.unit(e) ⇒ Object Also known as: pure



9
# File 'lib/funkr/extensions/array.rb', line 9

def unit(e); self.new([e]); end

Instance Method Details

#apply(to) ⇒ Object



27
28
29
30
31
# File 'lib/funkr/extensions/array.rb', line 27

def apply(to)
  map do |f|
    to.map{ |t| f.call(t)}
  end.flatten(1)
end

#bind(&block) ⇒ Object

Array is also a monad

1,2].bind{|x| [3,4].bind{|y| x + y}} # => [4,5,5,6


59
60
61
# File 'lib/funkr/extensions/array.rb', line 59

def bind(&block)
  self.map(&block).flatten(1)
end

#mplus(m_y) ⇒ Object

5].mplus() => [5,6


48
49
50
# File 'lib/funkr/extensions/array.rb', line 48

def mplus(m_y)
  self + m_y
end

#or_else(&block) ⇒ Object

].or_else[5] => [5


37
38
39
40
# File 'lib/funkr/extensions/array.rb', line 37

def or_else(&block)
  if empty? then yield
  else self end
end

#unboxObject



68
69
70
71
# File 'lib/funkr/extensions/array.rb', line 68

def unbox()
  if self.empty? then nil
  else self.first end
end