Class: ToStream

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(options = {}) ⇒ ToStream

Represents a generic to_stream function



228
229
230
231
# File 'lib/raskell/streams.rb', line 228

def initialize(options={})
  @before_function = options['before']
  @after_function = options['after']
end

Instance Attribute Details

#after_functionObject (readonly)

Returns the value of attribute after_function.



233
234
235
# File 'lib/raskell/streams.rb', line 233

def after_function
  @after_function
end

#before_functionObject (readonly)

Returns the value of attribute before_function.



233
234
235
# File 'lib/raskell/streams.rb', line 233

def before_function
  @before_function
end

Class Method Details

.new(options = {}) ⇒ Object



235
236
237
# File 'lib/raskell/streams.rb', line 235

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

Instance Method Details

#*(lamb) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
# File 'lib/raskell/streams.rb', line 288

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(lamb, self)
  else
    self.class.new({ 'before' => (self.before_function || Identity.new) * lamb ,
                     'after' => self.after_function})
  end
end

#<=(val) ⇒ Object



312
313
314
315
# File 'lib/raskell/streams.rb', line 312

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

#>=(lamb) ⇒ Object



317
318
319
320
# File 'lib/raskell/streams.rb', line 317

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

#call(collection) ⇒ Object



248
249
250
251
252
253
254
# File 'lib/raskell/streams.rb', line 248

def call(collection)
  before = @before_function
  after = @after_function
  collection = before.(collection) if before
  result = collection.to_stream
  after ? after.(result) : result
end

#fuse(before_converter, after_converter) ⇒ Object



284
285
286
# File 'lib/raskell/streams.rb', line 284

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

#join(after) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/raskell/streams.rb', line 256

def join(after)
  ## to = ToStream, from = FromStream
  ## to = ToStream, from = ToStream
  ## to = ToStream, from = StreamTransducer
  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 FromStream == 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:

  • (Boolean)


243
244
245
# File 'lib/raskell/streams.rb', line 243

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

#standard_kind_of?Object



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

alias_method :standard_kind_of?, :kind_of?

#|(lamb) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/raskell/streams.rb', line 300

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