Method: Collins::Option#flat_map

Defined in:
lib/collins/option.rb

#flat_map(&block) ⇒ Option<Object>

Same as map, but flatten the results

This is useful when operating on an object that will return an ‘Option`.

Examples:

Option(15).flat_map {|i| Option(i).filter{|i2| i2 > 0}} == Some(15)

Returns:

  • (Option<Object>)

    Optional value

See Also:



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/collins/option.rb', line 128

def flat_map &block
  if empty? then
    None.new
  else
    res = block.call(get)
    if res.is_a?(Some) then
      res
    else
      Some.new(res)
    end
  end
end