Class: FromStream

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clazz = Array, options = {}) ⇒ FromStream

Returns a new instance of FromStream.



116
117
118
119
120
121
122
123
124
# File 'lib/raskell/streams.rb', line 116

def initialize(clazz=Array, options={})
  if clazz.kind_of?(Hash)
    options = clazz
    clazz = Array
  end
  @before_function = options['before']
  @after_function = options['after']
  @output_type = clazz
end

Instance Attribute Details

#after_functionObject

Returns the value of attribute after_function.



126
127
128
# File 'lib/raskell/streams.rb', line 126

def after_function
  @after_function
end

#before_functionObject

Returns the value of attribute before_function.



126
127
128
# File 'lib/raskell/streams.rb', line 126

def before_function
  @before_function
end

Class Method Details

.new(clazz = Array, options = {}) ⇒ Object



128
129
130
# File 'lib/raskell/streams.rb', line 128

def self.new(clazz=Array,options={})
  options['before'] && options['after'] ? ->(x) { self.standard_new(clazz,options).(x) } : self.standard_new(clazz,options)
end

Instance Method Details

#*(lamb) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/raskell/streams.rb', line 191

def *(lamb)
  if lamb.kind_of?(Identity)
    self
  elsif [FromStream, StreamTransducer].include?(lamb.class)
    ## then fuse away the streams by just making this the Identity.new function
    self.fuse(lamb, self)
  else
    self.class.new({ 'before' => (self.before_function || Identity.new) * lamb ,
                     'after' => self.after_function})
  end
end

#<=(val) ⇒ Object



215
216
217
218
# File 'lib/raskell/streams.rb', line 215

def <=(val)
  # feed data from the right
  self.(val.())
end

#>=(lamb) ⇒ Object



220
221
222
223
# File 'lib/raskell/streams.rb', line 220

def >=(lamb)
  # feed data from the left, assuming I am a wrapped Object of some sort
  lamb.(self.())
end

#call(stream) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/raskell/streams.rb', line 141

def call(stream)
  before = @before_function
  after = @after_function
  stream = before.(stream) if before
  result = self.unfold(stream)
  after ? after.(result) : result
end

#fuse(before_converter, after_converter) ⇒ Object



187
188
189
# File 'lib/raskell/streams.rb', line 187

def fuse(before_converter, after_converter)
  before_converter.join(after_converter)
end

#join(after) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/raskell/streams.rb', line 163

def join(after)
  before = self
  if after.class == before.class && !after.before_function && !before.after_function
    before.class.new({
      'before' => before.before_function,
      'after' => after.after_function
    })
  elsif ToStream == after.class
    StreamTransducer.new({
      'before' =>  before.before_function,
      'inside' => (after.before_function || Identity.new) * ((after.kind_of?(StreamTransducer) ? inside_function : nil) || Identity.new) * (before.after_function || Identity.new),
      'after' => after.after_function
    })
  elsif StreamTransducer == after.class && !before.after_function && !after.before_function
    StreamTransducer.new({
        'before' => before.before_function,
        'inside' => after.inside_function,
        'after' => after.after_function
      })
  else
    ->(xs) { after.(before.(xs)) }
  end
end

#kind_of?(clazz) ⇒ Boolean

Returns:



136
137
138
# File 'lib/raskell/streams.rb', line 136

def kind_of?(clazz)
  clazz == Proc || standard_kind_of?(clazz)
end

#standard_kind_of?Object



135
# File 'lib/raskell/streams.rb', line 135

alias_method :standard_kind_of?, :kind_of?

#unfold(stream) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/raskell/streams.rb', line 150

def unfold(stream)
  result = @output_type.new
  # puts 'unfolding'
  # puts stream.inspect
  next_val = stream.next_item
  while next_val.first != :done
    #puts next_val.inspect
    result << next_val[1] if next_val.first == :yield
    next_val = next_val.last.next_item
  end
  result
end

#|(lamb) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/raskell/streams.rb', line 203

def |(lamb)
  if lamb.kind_of?(Identity)
    self
  elsif [FromStream, ToStream, StreamTransducer].include?(lamb.class)
    ## then fuse away the streams by just making this the Identity.new function
    self.fuse(self, lamb)
  else
    self.class.new({ 'before' => self.before_function,
                     'after' => (self.after_function || Identity.new) | lamb })
  end
end