Module: Philiprehberger::Maybe

Defined in:
lib/philiprehberger/maybe.rb,
lib/philiprehberger/maybe/version.rb

Defined Under Namespace

Classes: Error, None, Some

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.all?(*maybes) ⇒ Boolean

Check if all arguments are Some

Parameters:

  • maybes (Array<Some, None>)

    the Maybe values to check

Returns:

  • (Boolean)

    true if all are Some



26
27
28
# File 'lib/philiprehberger/maybe.rb', line 26

def self.all?(*maybes)
  maybes.all?(&:some?)
end

.first_some(*maybes) ⇒ Some, None

Return the first Some from the arguments, or None if all are None

Parameters:

  • maybes (Array<Some, None>)

    the Maybe values to search

Returns:

  • (Some, None)

    the first Some, or None



34
35
36
# File 'lib/philiprehberger/maybe.rb', line 34

def self.first_some(*maybes)
  maybes.find(&:some?) || None.instance
end

.wrap(value) ⇒ Some, None

Wrap a value in a Maybe container

Parameters:

  • value (Object)

    the value to wrap

Returns:

  • (Some, None)

    Some if value is non-nil, None otherwise



14
15
16
17
18
19
20
# File 'lib/philiprehberger/maybe.rb', line 14

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