Sha256: b7b23d7349b6ad2b67613e9e2e898fff3edee090edaaea1c1426282e98487515

Contents?: true

Size: 1.13 KB

Versions: 17

Compression:

Stored size: 1.13 KB

Contents

require "#{File.dirname(__FILE__)}/../lib/stages"

include Stages

#count the occurance of each letter in these song lyrics
def sing
  { :do => 'doe a deer a female deer',
    :re => 'ray a drop of golden sun',
    :mi => 'me a name I call myself',
    :fa => 'far a long long way to run',
    :so => 'a needle pulling thread',
    :la => 'a note to follow so',
    :ti => 'a drink with jam and bread'}
end

def setup_pipeline
  generator = Each.new sing.keys
  loop = Restrict.new
  get_lyric = Map.new{ |x| sing[x]}
  each_character = Each.new{ |x| x.chars }
  whitespace = Select.new{ |x| x != ' '}
  pool = ResumeCount.new
  subtotals = Map.new { |x| x.values.first }
  iterator = Each.new
  aggregator = SuperAggregator.new
  
  generator | loop | get_lyric | each_character | whitespace | pool | subtotals | iterator  |  aggregator
end

class SuperAggregator < Stage
  def initialize
    @accumulator = Hash.new{ |h,k| h[k] = 0}
    super()
  end
  
  def handle_value(value)
    @accumulator[value[0]] += value[1]
    while v = input
      @accumulator[v[0]] += v[1]
    end
    output @accumulator
  end
end

puts setup_pipeline.run.inspect



Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
stages-0.3.4 examples/sing.rb
stages-0.3.3 examples/sing.rb
stages-0.3.2 examples/sing.rb
stages-0.3.1 examples/sing.rb
stages-0.3.0 examples/sing.rb
stages-0.2.10 examples/sing.rb
stages-0.2.9 examples/sing.rb
stages-0.2.8 examples/sing.rb
stages-0.2.7 examples/sing.rb
stages-0.2.6 examples/sing.rb
stages-0.2.5 examples/sing.rb
stages-0.2.4 examples/sing.rb
stages-0.2.3 examples/sing.rb
stages-0.2.2 examples/sing.rb
stages-0.2.1 examples/sing.rb
stages-0.2.0 examples/sing.rb
stages-0.1.2 examples/sing.rb