Class: Sbuilder::Fp::Sequence

Inherits:
Object
  • Object
show all
Includes:
Compositable
Defined in:
lib/fp/sequence.rb

Instance Attribute Summary

Attributes included from Compositable

#value

Class Method Summary collapse

Instance Method Summary collapse

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

.from_array(array) ⇒ Object



46
47
48
49
# File 'lib/fp/sequence.rb', line 46

def self.from_array( array )
  array = [ array ] unless array.is_a?( Array )
  Sequence.new( array.lazy )
end

.wrap(wrapped) ⇒ Object



42
43
44
# File 'lib/fp/sequence.rb', line 42

def self.wrap( wrapped )
  Sequence.new( wrapped )        
end

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