Class: Deterministic::Option
Overview
Abstract parent of Some and None
Defined Under Namespace
Modules: PatternMatching
Classes: None, Some
Class Method Summary
collapse
Instance Method Summary
collapse
#match
Methods included from Monad
#==, #bind, #fmap, #initialize, #inspect, #join, #to_s, #value
Class Method Details
.any?(expr) ⇒ Boolean
30
31
32
|
# File 'lib/deterministic/option.rb', line 30
def any?(expr)
to_option(expr) { expr.nil? || not(expr.respond_to?(:empty?)) || expr.empty? }
end
|
.some?(expr) ⇒ Boolean
26
27
28
|
# File 'lib/deterministic/option.rb', line 26
def some?(expr)
to_option(expr) { expr.nil? }
end
|
.to_option(expr, &predicate) ⇒ Object
34
35
36
|
# File 'lib/deterministic/option.rb', line 34
def to_option(expr, &predicate)
predicate.call(expr) ? None.new : Some.new(expr)
end
|
38
39
40
|
# File 'lib/deterministic/option.rb', line 38
def try!
yield rescue None.new
end
|
Instance Method Details
Add the inner values of two Some
54
55
56
57
58
59
60
61
62
|
# File 'lib/deterministic/option.rb', line 54
def +(other)
return other if none?
fmap { |v|
other.match {
some { v + other.value }
none { self }
}
}
end
|
#map(proc = nil, &block) ⇒ Object
43
44
45
46
|
# File 'lib/deterministic/option.rb', line 43
def map(proc=nil, &block)
return self if none?
bind(proc || block)
end
|
#none? ⇒ Boolean
68
69
70
|
# File 'lib/deterministic/option.rb', line 68
def none?
is_a? None
end
|
#some? ⇒ Boolean
64
65
66
|
# File 'lib/deterministic/option.rb', line 64
def some?
is_a? Some
end
|
#value_or(default) ⇒ Object
72
73
74
75
|
# File 'lib/deterministic/option.rb', line 72
def value_or(default)
return default if none?
return value
end
|
#value_to_a ⇒ Object
Convert the inner value to an Array
49
50
51
|
# File 'lib/deterministic/option.rb', line 49
def value_to_a
map { self.class.new(Array(value)) }
end
|