Class: Monad::Maybe::List

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/monad/maybe/json.rb,
lib/monad/maybe/list.rb

Instance Method Summary collapse

Constructor Details

#initialize(enum) ⇒ List

Returns a new instance of List.



6
7
8
# File 'lib/monad/maybe/list.rb', line 6

def initialize(enum)
  @enum = enum.map { |v| v.maybe? ? v : v.to_maybe }
end

Instance Method Details

#<<(obj) ⇒ Object



18
19
20
21
# File 'lib/monad/maybe/list.rb', line 18

def <<(obj)
  @enum << obj if obj.just?
  self
end

#eachObject



31
32
33
34
35
# File 'lib/monad/maybe/list.rb', line 31

def each
  @enum.each do |x|
    yield(x)
  end
end

#inspectObject



10
11
12
# File 'lib/monad/maybe/list.rb', line 10

def inspect
  "#{to_a}"
end

#mapObject Also known as: maybe_map



37
38
39
40
41
42
43
# File 'lib/monad/maybe/list.rb', line 37

def map
  e = []
  each do |x|
    e << yield(x)
  end
  List.new(e)
end

#maybe?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/monad/maybe/list.rb', line 14

def maybe?
  true
end

#rejectObject



55
56
57
# File 'lib/monad/maybe/list.rb', line 55

def reject
  select { |x| !yield(x) }
end

#selectObject



46
47
48
49
50
51
52
53
# File 'lib/monad/maybe/list.rb', line 46

def select
  e = []
  each do |x|
    is_true = yield(x)
    e << x if is_true  
  end
  List.new(e)
end

#select_justObject



59
60
61
# File 'lib/monad/maybe/list.rb', line 59

def select_just
  select { |x| x.just? }
end

#to_aObject



23
24
25
# File 'lib/monad/maybe/list.rb', line 23

def to_a
  @enum.to_a
end

#to_json(*args) ⇒ Object



21
22
23
# File 'lib/monad/maybe/json.rb', line 21

def to_json(*args)
  to_a.to_json(*args)
end

#to_maybeObject



27
28
29
# File 'lib/monad/maybe/list.rb', line 27

def to_maybe
  first.to_maybe
end

#unwrap_map(default, &blk) ⇒ Object



63
64
65
# File 'lib/monad/maybe/list.rb', line 63

def unwrap_map(default, &blk)
  to_a.map { |x| blk ? blk.call(x.unwrap(default)) : x.unwrap(default) }
end

#value_map(&blk) ⇒ Object



67
68
69
# File 'lib/monad/maybe/list.rb', line 67

def value_map(&blk)
  to_a.map { |x| blk ? blk.call(x.value) : x.value }
end