Class: Sbuilder::Fp::Sequence
- Inherits:
-
Object
- Object
- Sbuilder::Fp::Sequence
- Includes:
- Compositable
- Defined in:
- lib/fp/sequence.rb
Instance Attribute Summary
Attributes included from Compositable
Class Method Summary collapse
Instance Method Summary collapse
-
#and_then(proc = nil, *args, &block) ⇒ Object
Composition operator.
Methods included from Compositable
#Error, #Result, #_proc, #error, included, #initialize, #isError, #isSuccess, #lwrap, #method_missing, #or_else, #pipe, #to_s
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Sbuilder::Fp::Compositable
Class Method Details
Instance Method Details
#and_then(proc = nil, *args, &block) ⇒ Object
Composition operator. Can bind (with *args) to an instance method (given as a :symbol), proc, or block.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fp/sequence.rb', line 10 def and_then( proc=nil, *args, &block ) proc = _proc( proc, *args, &block ) # toggle for any error inError = false # ---------- # Iterate sequence using lamba blockAsProc = ->(v) do begin mapped_v = proc.arity == 0 ? proc.call( &block ) : proc.call( v, &block ) # Guarantee that sequenced results are wrapped within # 'Compositable'. Use Fixnum check to avoid error "can't # define singleton" for 'immadiate values (i.e. Fixnums) mapped_v = Result(mapped_v) if mapped_v.is_a?( Fixnum ) || !mapped_v.singleton_class.include?( Compositable) inError = inError || mapped_v.isError rescue FpException => fpError inError = true mapped_v = Error(fpError) end mapped_v end ret = value.map &blockAsProc ret = inError ? Error( ret ) : Result( ret ) ret end |