Class: Ronad::One

Inherits:
Many show all
Defined in:
lib/ronad/one.rb

Overview

A One is similar to a Many but does not flatten the results if #and_then produces another list.

urls = %w[
  example.com?p1=a&p2=b
  example.com?z1=5&y1=9
]

one(urls)
  .split('?')
  .last # a Many would have flatten the #split and #last would be called on a String
  .split('&')
  .last
  .split('=')
  .first
  .value #=> ['p2', 'y1']

A One can be used for providing a familiar interface to a function that does not expect the flattening behavior of Many.

Instance Method Summary collapse

Methods inherited from Many

#initialize, #monad_value

Methods inherited from Monad

#continue, #initialize, #monad_value, #value, #~@

Constructor Details

This class inherits a constructor from Ronad::Many

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ronad::Monad

Instance Method Details

#and_then(&b) ⇒ Object



22
23
24
# File 'lib/ronad/one.rb', line 22

def and_then &b
  from_value @value.reject(&:nil?).map(&b)
end