Class: MonadOxide::Some

Inherits:
Option
  • Object
show all
Defined in:
lib/some.rb

Overview

‘Some’ represents a present value in an ‘Option’. For most operations, ‘Some’ will perform some operation.

Instance Method Summary collapse

Methods inherited from Option

#match

Constructor Details

#initialize(data) ⇒ Some

Constructs a ‘Some’ with the data provided.



16
17
18
# File 'lib/some.rb', line 16

def initialize(data)
  @data = data
end

Instance Method Details

#and_then(f = nil) { ... } ⇒ Some<B> | None

Invokes ‘f’ or the block with the data and returns the Option returned from that. The return type is enforced.

Yields:

  • Will yield a block that takes an A and returns a Option<B>. Same as ‘f’ parameter.



30
31
32
33
34
35
36
37
# File 'lib/some.rb', line 30

def and_then(f=nil, &block)
  option = (f || block).call(@data)
  if !option.kind_of?(Option)
    raise OptionReturnExpectedError.new(option)
  else
    option
  end
end

#inspect_none(f = nil) { ... } ⇒ Some

Falls through. @see Option#inspect_none for how this is handled in either Option case, and @see None#inspect_none for how this is handled in the None case.

Yields:

  • An ignored block.



59
60
61
# File 'lib/some.rb', line 59

def inspect_none(f=nil, &block)
  self
end

#inspect_some(f = nil) { ... } ⇒ Option<A>

Applies ‘f’ or the block over the data and returns the same ‘Some’. No changes are applied. This is ideal for logging.

Yields:

  • Will yield a block that takes an A the return is ignored. Same as ‘f’ parameter.



47
48
49
50
# File 'lib/some.rb', line 47

def inspect_some(f=nil, &block)
  (f || block).call(@data)
  self
end

#map(f = nil) { ... } ⇒ Option<B>

Applies ‘f’ or the block over the data and returns a new ‘Some’ with the returned value.

Yields:

  • Will yield a block that takes an A and returns a B. Same as ‘f’ parameter.



72
73
74
# File 'lib/some.rb', line 72

def map(f=nil, &block)
  self.class.new((f || block).call(@data))
end

#map_none(f = nil) { ... } ⇒ Option<A>

This is a no-op for Some. @see None#map_none.

Yields:

  • A dummy block. Not used.



81
82
83
# File 'lib/some.rb', line 81

def map_none(f=nil, &block)
  self
end

#none?Boolean

Identifies that this is not a ‘None’. For counterparts:



92
93
94
# File 'lib/some.rb', line 92

def none?()
  false
end

#or_else(f = nil) { ... } ⇒ Option<A>

The None equivalent to Some#and_then. This is a no-op for Some. @see None#or_else.

Yields:

  • A dummy block. Not used.



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

def or_else(f=nil, &block)
  self
end

#some?Boolean

Identifies that this is a ‘Some’. For counterparts:



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

def some?()
  true
end

#unwrapA

Dangerously access the ‘Some’ data. If this is a ‘None’, an exception will be raised. It is recommended to use this for tests only.



121
122
123
# File 'lib/some.rb', line 121

def unwrap()
  @data
end

#unwrap_nonenil

Dangerously access the ‘None’ data. If this is a ‘Some’, an exception will be raised. It is recommended to use this for tests only.

Raises:



129
130
131
132
133
134
135
136
# File 'lib/some.rb', line 129

def unwrap_none()
  raise UnwrapError.new(
    "      \#{self.class} with \#{@data.inspect()} could not be unwrapped as a\n      None.\n    EOE\n  )\nend\n"