Class: System::Foldl

Inherits:
Object show all
Defined in:
lib/raskell/folds.rb

Constant Summary collapse

@@foldl =
->(f,u) { Foldl.new([f,u])}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*monoids) ⇒ Foldl

must be [fn, unit] pair, a monoid



12
13
14
15
# File 'lib/raskell/folds.rb', line 12

def initialize(*monoids) # must be [fn, unit] pair, a monoid
    @monoids = monoids
    self
end

Class Method Details

.foldlObject



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

def self.foldl
  @@foldl
end

Instance Method Details

#*(lamb) ⇒ Object



21
22
23
# File 'lib/raskell/folds.rb', line 21

def *(lamb)
  ->(x) { self.( lamb.( x ) ) }
end

#+(foldl) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/raskell/folds.rb', line 37

def +(foldl)
  if foldl.kind_of?(Foldl)
    Foldl.new(*(self.monoids + foldl.monoids))
  else
    raise "Cannot add two non-folds together"
  end
end

#<<(val) ⇒ Object



29
30
31
# File 'lib/raskell/folds.rb', line 29

def <<(val)
  self.(val.())
end

#>>(lamb) ⇒ Object



33
34
35
# File 'lib/raskell/folds.rb', line 33

def >>(lamb)
  lamb.(self)
end

#call(stream) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/raskell/folds.rb', line 45

def call(stream)
  if stream.respond_to?(:to_stream)
     if @monoids.length > 1
       fn = ->(acc, el) { F.zip_with.(F.apply_fn).(@monoids.map(&:first), acc, @monoids.map {|x| el }).to_a }
       F.foldleft.(fn, @monoids.map(&:last)).(stream).to_a
     else
       F.foldleft.(*@monoids.first).(stream)
     end
  else
    raise "Cannot call Foldl on an object that does not have to_stream defined."
  end
end

#kind_of?(clazz) ⇒ Boolean



8
9
10
# File 'lib/raskell/folds.rb', line 8

def kind_of?(clazz)
  [Proc].include?(clazz) || standard_ruby_kind_of?(clazz)
end

#monoidsObject



17
18
19
# File 'lib/raskell/folds.rb', line 17

def monoids
  @monoids
end

#standard_ruby_kind_of?Object



6
# File 'lib/raskell/folds.rb', line 6

alias_method :standard_ruby_kind_of?, :kind_of?

#|(lamb) ⇒ Object



25
26
27
# File 'lib/raskell/folds.rb', line 25

def |(lamb)
  ->(x) { lamb.( self.( x ) ) }
end