Module: System::Collections::HigherOrderStreamLambdas

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

Instance Method Summary collapse

Instance Method Details

#all?Boolean



501
502
503
# File 'lib/raskell/f.rb', line 501

def all?
  @@all_p||= ->(f) { ->(x) { x == Nothing } * find_where.(->(x) { !f.(x)})}
end

#any?Boolean

this is going to become equal.(Nothing) * first * find_where.(F.not.(f))



505
506
507
# File 'lib/raskell/f.rb', line 505

def any?
  @@any_p||= ->(f) { ->(x) { x != Nothing } * find_where.(f)}
end

#filterObject



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/raskell/f.rb', line 465

def filter
  @@filter||= ->(fn) { 
    ->(stream) {
      next_fn = ->(state) {
        next_el = state.next_item
        if next_el == [:done]
          [:done]
        elsif next_el.first == :skip || (next_el.first == :yield && !fn.(next_el[1]))
          [:skip, Stream.new(next_fn, next_el.last)]
        elsif next_el.first == :yield && fn.(next_el[1])
          [next_el.first, 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

#find_last_whereObject



489
490
491
# File 'lib/raskell/f.rb', line 489

def find_last_where
  @@find_last_where||= ->(fn){ last * filter.(fn) }
end

#find_whereObject



485
486
487
# File 'lib/raskell/f.rb', line 485

def find_where
  @@find_where||= ->(fn){ first * filter.(fn) }
end

#first_index_whereObject



493
494
495
# File 'lib/raskell/f.rb', line 493

def first_index_where
  @@first_index_where||= ->(fn) { first * find_where.( fn * last ) * zip_with_index }
end

#flatmapObject



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/raskell/f.rb', line 428

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

#group_byObject



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/raskell/f.rb', line 517

def group_by
  @@group_by||= ->(fn) { 
    next_fn = ->(state) {
      strm = state.last
      group = state.first
      next_item = strm.next_item
      tag = next_item.first
      val = next_item[1]
      next_stream = next_item.last
      if tag == :done && group == []
        [:done]
      elsif tag == :done
        [:yield, group, empty]
      elsif tag == :skip
        [:skip, Stream.new(next_fn, [group, next_stream])]
      elsif tag == :yield && (group.length == 0 || fn.(val) == fn.(group.last))
        [:skip, Stream.new(next_fn, [group + [val], next_stream])]
      elsif tag == :yield
        [:yield, group, Stream.new([[], next_stream])]
      else
        raise "#{next_item} is a malformed stream response!"
      end
    }
    Stream.new(next_fn, [[], state])
  } * to_stream
end

#last_index_whereObject



497
498
499
# File 'lib/raskell/f.rb', line 497

def last_index_where
  @@last_index_where||= ->(fn) { first * find_last_where.( fn * last ) * zip_with_index }
end

#slice_byObject



509
510
511
# File 'lib/raskell/f.rb', line 509

def slice_by
  @@slice_by||= ->(starts_with_fn, ends_with_fn) { take_until.(ends_with_fn) * drop_until.(starts_with_fn) }
end

#split_byObject



513
514
515
# File 'lib/raskell/f.rb', line 513

def split_by
  @@split_by = ->(fn) { take_while.(fn) + drop_while.(fn) }
end

#window_byObject



544
545
546
# File 'lib/raskell/f.rb', line 544

def window_by
  @@window_by||= self.group_by ## basically, window_by is group_by, but where you pass it a relation (a true/false function) instead of an arbitrary function
end