class Monad::Maybe::List
Public Class Methods
new(enum)
click to toggle source
TODO: add support for lazy evaluation
# File lib/monad/maybe/list.rb, line 8 def initialize(enum) @enum = enum.map { |v| v.maybe? ? v : v.to_maybe } end
Public Instance Methods
<<(obj)
click to toggle source
# File lib/monad/maybe/list.rb, line 20 def <<(obj) @enum << obj if obj.just? self end
each() { |x| ... }
click to toggle source
# File lib/monad/maybe/list.rb, line 33 def each @enum.each do |x| yield(x) end end
inspect()
click to toggle source
# File lib/monad/maybe/list.rb, line 12 def inspect "maybe_list(#{@enum.map(&:inspect).join(', ')})" end
map() { |x| ... }
click to toggle source
# File lib/monad/maybe/list.rb, line 39 def map e = [] each do |x| e << yield(x) end List.new(e) end
Also aliased as: maybe_map
maybe?()
click to toggle source
# File lib/monad/maybe/list.rb, line 16 def maybe? true end
reject() { |x| ... }
click to toggle source
# File lib/monad/maybe/list.rb, line 57 def reject select { |x| !yield(x) } end
select() { |x| ... }
click to toggle source
# File lib/monad/maybe/list.rb, line 48 def select e = [] each do |x| is_true = yield(x) e << x if is_true end List.new(e) end
select_just()
click to toggle source
# File lib/monad/maybe/list.rb, line 61 def select_just select { |x| x.just? } end
to_a()
click to toggle source
# File lib/monad/maybe/list.rb, line 25 def to_a @enum.to_a end
to_json(*args)
click to toggle source
# File lib/monad/maybe/json.rb, line 21 def to_json(*args) to_a.to_json(*args) end
to_maybe()
click to toggle source
# File lib/monad/maybe/list.rb, line 29 def to_maybe first.to_maybe end
unwrap_map(default, &blk)
click to toggle source
# File lib/monad/maybe/list.rb, line 65 def unwrap_map(default, &blk) to_a.map { |x| blk ? blk.call(x.unwrap(default)) : x.unwrap(default) } end
value_map(&blk)
click to toggle source
# File lib/monad/maybe/list.rb, line 69 def value_map(&blk) to_a.map { |x| blk ? blk.call(x.value) : x.value } end