Created
October 31, 2008 19:17
-
-
Save bdimcheff/21398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# input: | |
# | |
# foo,1,1 | |
# foo,1,2 | |
# foo,1,3 | |
# foo,1,5 | |
# bar,2,1 | |
# bar,1,7 | |
# bar,0,5 | |
# | |
# | |
# output: | |
# | |
# foo,1.0,2.5 | |
# bar,1.0,5.0 | |
a1 = Aggregator.new do |agg| | |
agg.group_by 1 | |
agg.all_columns do |data| | |
data.median | |
end | |
end | |
# other ideas: | |
# this would take a file with 5 columns, group on the first two, | |
# and then output 4 data columns: | |
# * the median of column 3 | |
# * the mean of column 4 | |
# * the median of column 5 | |
# * the mean of column 5 | |
a2 = Aggregator.new do |agg| | |
agg.group_by 1, 2 | |
agg.column 3 do |data| | |
data.median | |
end | |
agg.column 4 do |data| | |
data.mean | |
end | |
agg.column 5 do |data| | |
data.median | |
end | |
agg.column 5 do |data| | |
data.mean | |
end | |
end | |
# this illustrates optional column naming | |
a3 = Aggregator.new do |agg| | |
agg.columns :foo, :bar, :baz | |
agg.group_by :foo | |
agg.column :bar, :baz do |data| | |
data.mean | |
end | |
end | |
a4 = Aggregator.new do |agg| | |
agg.sum(column(1)) | |
agg.column(1, &sum) | |
agg.out column(1).sum | |
end | |
class AggClass < Aggregate | |
group_by :foo | |
output(:foo).sum | |
output(:foo).avg | |
output(:foo).median | |
output(:foo) do |data| # loads all data | |
data.first * 2 | |
end | |
output(:foo).inject(0) do |acc, rec| # streams data | |
acc += rec % 10 | |
end | |
end | |
a5 = AggClass.new | |
a5.aggregate($stdin) | |
agg_class = Class.new(Aggregate) do | |
group_by :foo | |
output(:foo).sum | |
output(:foo).avg | |
output(:foo).median | |
output(:foo) do |data| # loads all data | |
data.first * 2 | |
end | |
output(:foo).inject(0) do |acc, rec| # streams data | |
acc += rec % 10 | |
end | |
end | |
a6 = agg_class.new | |
a6.aggregate($stdin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment