Module: Option

Includes:
Contracts::Core
Defined in:
lib/trither/option.rb

Defined Under Namespace

Modules: None Classes: Some

Constant Summary collapse

C =
Contracts
NoneType =
->(x) { x == None }
OptionType =
C::Or[NoneType, Some]
Func1toOption =
C::Func[C::Any => OptionType]

Class Method Summary collapse

Class Method Details

.all(array_of_options) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/trither/option.rb', line 27

def self.all(array_of_options)
  array_of_options.reduce(Some.new([])) do |memo, option|
    memo.flat_map do |array|
      option.map { |value| array + [value] }
    end
  end
end

.flatten(array_of_options) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/trither/option.rb', line 36

def self.flatten(array_of_options)
  array_of_options.reduce([]) do |array, option|
    if option.empty?
      array
    else
      array + [option.get_or_else { raise StandardError }]
    end
  end
end

.make(value) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/trither/option.rb', line 18

def self.make(value)
  if value.nil?
    None
  else
    Some.new(value)
  end
end