Module: Trith::Core::Sequence::Combinators
- Defined in:
- lib/trith/core/sequence.rb
Overview
Sequence combinators.
Instance Method Summary (collapse)
- - (Machine) each(seq, quot)
- - (Machine) foldl(seq, identity, quot) (also: #fold, #reduce)
- - (Machine) foldl1(seq, quot)
- - (Machine) map(seq, quot)
Instance Method Details
- (Machine) each(seq, quot)
397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/trith/core/sequence.rb', line 397 def each(seq, quot) seq = case seq when String then seq.each_char else case when seq.respond_to?(:each) then seq.each else raise Machine::InvalidOperandError.new(seq, :each) end end with_saved_continuation(:each) do seq.each { |elem| push(elem).execute(quot) } end end |
- (Machine) foldl(seq, identity, quot) Also known as: fold, reduce
433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/trith/core/sequence.rb', line 433 def foldl(seq, identity, quot) seq = case seq when String then seq.each_char else case when seq.respond_to?(:reduce) then seq when seq.respond_to?(:each) then seq.each else raise Machine::InvalidOperandError.new(seq, :foldl) end end with_saved_continuation(:foldl) do push(seq.reduce(identity) { |prev, elem| push(prev, elem).execute(quot).pop }) end end |
- (Machine) foldl1(seq, quot)
453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/trith/core/sequence.rb', line 453 def foldl1(seq, quot) raise Machine::InvalidOperandError.new(seq, :foldl1) if seq.empty? seq = case seq when String then seq.each_char.to_a else case when seq.respond_to?(:reduce) then seq.dup when seq.respond_to?(:each) then seq.each.to_a else raise Machine::InvalidOperandError.new(seq, :foldl1) end end with_saved_continuation(:foldl1) do push(seq.reduce(seq.pop) { |prev, elem| push(prev, elem).execute(quot).pop }) end end |
- (Machine) map(seq, quot)
414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/trith/core/sequence.rb', line 414 def map(seq, quot) seq = case seq when String then seq.each_char else case when seq.respond_to?(:map) then seq when seq.respond_to?(:each) then seq.each else raise Machine::InvalidOperandError.new(seq, :map) end end with_saved_continuation(:map) do push(seq.map { |elem| push(elem).execute(quot).pop }) end end |