class Mon::Monad::List

Public Class Methods

[](*list) click to toggle source

Create a list. Eg: List[1, 2, 3]

# File lib/monads/list.rb, line 26
def self.[](*list)
  List.new(list)
end
valid?(o) click to toggle source
# File lib/monads/list.rb, line 82
def self::valid?(o)
  o.is_a?(List)
end

Protected Class Methods

new(list) click to toggle source
# File lib/monads/list.rb, line 21
def initialize(list)
  @list = list
end

Public Instance Methods

==(o) click to toggle source
# File lib/monads/list.rb, line 74
def ==(o)
  eql?(o)
end
_() click to toggle source

Alias for unwrap

# File lib/monads/list.rb, line 42
def _
  to_a
end
bind(&fun) click to toggle source

Apply fun to all elements of the list (ala map): List[1, 2, 3].map { |i| i + 5 } # ==> List[6, 7, 8]

# File lib/monads/list.rb, line 32
def bind &fun
  List.send(:new, @list.map { |i| fun.call(i) }.map { |i| (i.is_a? List) ? i.to_a : i}.flatten(1))
end
eql?(other) click to toggle source
# File lib/monads/list.rb, line 64
def eql?(other)
  if other.is_a? List
    @list == other.unwrap
  elsif other.is_a? Array
    @list == other
  else
    false
  end
end
equal?(o) click to toggle source
# File lib/monads/list.rb, line 78
def equal?(o)
  eql?(o)
end
to_a() click to toggle source

Return the array wrapped by the list

# File lib/monads/list.rb, line 56
def to_a
  @list
end
to_s() click to toggle source
# File lib/monads/list.rb, line 50
def to_s
  (@list.length > 3) ? (tail = "...") : tail = "" 
  "List[#{ @list.take(3).join(", ") }#{ tail }]"
end

Protected Instance Methods

_canBind?(name) click to toggle source
# File lib/monads/list.rb, line 46
def _canBind? name
  @list.all? { |i| i.respond_to?(name) }
end
unwrap() click to toggle source

Return the array wrapped by the List

# File lib/monads/list.rb, line 37
def unwrap
  to_a
end