Class: Some

Inherits:
Option show all
Includes:
LambdaBlock
Defined in:
lib/totally_lazy/option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Option

#flatten, #is?, #is_defined?, none, option, some

Constructor Details

#initialize(value) ⇒ Some

Returns a new instance of Some.



64
65
66
# File 'lib/totally_lazy/option.rb', line 64

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



62
63
64
# File 'lib/totally_lazy/option.rb', line 62

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object



134
135
136
# File 'lib/totally_lazy/option.rb', line 134

def <=>(other)
  @value <=> other.value
end

#contains?(value) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/totally_lazy/option.rb', line 68

def contains?(value)
  @value == value
end

#enumeratorObject



127
128
129
130
131
132
# File 'lib/totally_lazy/option.rb', line 127

def enumerator
  Enumerator.new { |y|
    y << @value
    raise StopIteration.new
  }
end

#exists?(fn_pred = nil, &block_pred) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/totally_lazy/option.rb', line 72

def exists?(fn_pred=nil, &block_pred)
  assert_funcs(fn_pred, block_given?)
  block_given? ? block_pred.call(@value) : fn_pred.(@value)
end

#flat_map(fn = nil, &block) ⇒ Object

function should return an option



82
83
84
85
# File 'lib/totally_lazy/option.rb', line 82

def flat_map(fn=nil, &block) # function should return an option
  assert_funcs(fn, block_given?)
  block_given? ? block.call(@value) : fn.(@value)
end

#fold(seed, fn = nil, &block) ⇒ Object Also known as: fold_left



87
88
89
90
# File 'lib/totally_lazy/option.rb', line 87

def fold(seed, fn=nil, &block)
  assert_funcs(fn, block_given?)
  block_given? ? block.call(seed, @value) : fn.(seed, @value)
end

#getObject



102
103
104
# File 'lib/totally_lazy/option.rb', line 102

def get
  @value
end

#get_or_else(value_or_fn = nil, &block) ⇒ Object Also known as: or_else



106
107
108
109
# File 'lib/totally_lazy/option.rb', line 106

def get_or_else(value_or_fn=nil, &block)
  assert_funcs(value_or_fn, block_given?)
  get
end

#get_or_nilObject



113
114
115
# File 'lib/totally_lazy/option.rb', line 113

def get_or_nil
  get
end

#get_or_raise(error) ⇒ Object Also known as: get_or_throw



117
118
119
# File 'lib/totally_lazy/option.rb', line 117

def get_or_raise(error)
  get
end

#is_empty?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/totally_lazy/option.rb', line 94

def is_empty?
  false
end

#map(fn = nil, &block) ⇒ Object



77
78
79
80
# File 'lib/totally_lazy/option.rb', line 77

def map(fn=nil, &block)
  assert_funcs(fn, block_given?)
  option(block_given? ? block.call(@value) : fn.(@value))
end

#sizeObject



98
99
100
# File 'lib/totally_lazy/option.rb', line 98

def size
  1
end

#to_either(left) ⇒ Object



123
124
125
# File 'lib/totally_lazy/option.rb', line 123

def to_either(left)
  right(value)
end

#to_sObject



138
139
140
# File 'lib/totally_lazy/option.rb', line 138

def to_s
  "some(#{value})"
end