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
#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
52
53
54
|
# File 'lib/deterministic/option.rb', line 52
def none?
is_a? None
end
|
#some? ⇒ Boolean
48
49
50
|
# File 'lib/deterministic/option.rb', line 48
def some?
is_a? Some
end
|