Module: System::Collections::OneForOneStreamLambdas

Extended by:
OneForOneStreamLambdas
Included in:
OneForOneStreamLambdas, F
Defined in:
lib/raskell/f.rb

Instance Method Summary collapse

Instance Method Details

#mapleftObject



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/raskell/f.rb', line 342

def mapleft
  @@mapleft||= ->(fn) { 
      ->(stream) {
        next_fn = ->(state) {
          next_el = state.next_item
          if next_el == [:done]
            [:done]
          elsif next_el.first == :skip
            [:skip, Stream.new(next_fn, next_el.last)]
          elsif next_el.first == :yield
            [:yield, fn.(next_el[1]), Stream.new(next_fn, next_el.last)]
          else
            raise "#{next_el.inspect} is not a valid stream state!"
          end
        } 
        Stream.new(next_fn, stream) 
      } * to_stream
  }
end

#replace_withObject



419
420
421
# File 'lib/raskell/f.rb', line 419

def replace_with
  @@replace_with||= ->(to_replace_with, to_replace) { map.(->(x) { x == to_replace ? to_replace_with : x }) }
end

#scanleftObject



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/raskell/f.rb', line 362

def scanleft
  @@scanleft||= ->(f,u) { 
    ->(stream) {
      next_fn = ->(state) {
        result_so_far = state.first
        strm = state.last
        next_item = strm.next_item
        tag = next_item[0]
        val = next_item[1]
        next_stream = next_item.last
        if tag == :done
          [:done]
        elsif tag == :skip
          [:skip, Stream.new(next_fn, [result_so_far, next_stream])]
        elsif tag == :yield
          new_result = f.(result_so_far, val)
          [:yield, new_result, Stream.new(next_fn, [new_result, next_stream]) ]
        else
          raise "#{next_item} is a malformed stream response"
        end
      }
      Stream.new(next_fn, [u, stream])
    } * to_stream
  
  }
end

#zip_withObject



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/raskell/f.rb', line 389

def zip_with
  @@zip_with||= ->(fn) {
    ->(left_stream, right_stream, *streams) {
        streams = ([left_stream, right_stream] + streams).map(&:to_stream)
        next_fn = ->(state) {
          val_so_far = state.first
          strms = state.drop(1)
          next_stream = strms.first
          next_item = next_stream.next_item
          new_streams = strms.drop(1) + [next_stream.next_item.last == :done ? empty : next_item.last]
          tag = next_item.first
          val = tag == :done ? nil : next_item[1]
          if tag == :done
            [:done]
          elsif tag == :skip
            [:skip, Stream.new(next_fn, [val_so_far] +  new_streams)]
          elsif tag == :yield && val_so_far.length == streams.length - 1
            [:yield, fn.(*(val_so_far + [val])), Stream.new(next_fn, [[]] + new_streams)]
          elsif tag == :yield
            [:skip, Stream.new(next_fn, [val_so_far + [val]] + new_streams)]
          else
            raise "#{next_item} is a malformed stream response!"
          end
        }
  
        Stream.new(next_fn, [[]] + streams)
      }
  }
end