Class: System::Scanl

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

Constant Summary collapse

@@scanl =
->(f,u) { Scanl.new([f,u])}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*monoids) ⇒ Scanl

must be [fn, unit] pair, a monoid



77
78
79
80
# File 'lib/raskell/folds.rb', line 77

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

Class Method Details

.scanlObject



124
125
126
# File 'lib/raskell/folds.rb', line 124

def self.scanl
  @@scanl
end

Instance Method Details

#*(lamb) ⇒ Object



86
87
88
# File 'lib/raskell/folds.rb', line 86

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

#+(scanl) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/raskell/folds.rb', line 102

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

#<<(val) ⇒ Object



94
95
96
# File 'lib/raskell/folds.rb', line 94

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

#>>(lamb) ⇒ Object



98
99
100
# File 'lib/raskell/folds.rb', line 98

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

#call(stream) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/raskell/folds.rb', line 110

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.scanleft.(fn, @monoids.map(&:last)).(stream)
     else
       F.scanleft.(*@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

Returns:

  • (Boolean)


73
74
75
# File 'lib/raskell/folds.rb', line 73

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

#monoidsObject



82
83
84
# File 'lib/raskell/folds.rb', line 82

def monoids
  @monoids
end

#standard_ruby_kind_of?Object



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

alias_method :standard_ruby_kind_of?, :kind_of?

#|(lamb) ⇒ Object



90
91
92
# File 'lib/raskell/folds.rb', line 90

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