R
Robert Dober
Hi list
I actually just need the following method there is no doubt about it
but the name which sprang into mind naturally "partition_by"
has a serious shortcoming, it is not POLS in the sense that
Enumerable#partition returns an array and Enumerable#partition_by
returns a hash.
module Enumerable
def partition_by &blk
result = Hash.new {|h,k| h[k] = [] }
each do | element |
result[ element ] << blk.call element
end
result
end
end
Any ideas for a nice different name?
Does Facet implement the above maybe?
A remark for my fellow fans of inject, the same code with inject
somehow does not feel right, I do not like to use inject with this
pattern
inject(...){ |acc,ele|
...
acc
}
Thanx in advance
Robert
I actually just need the following method there is no doubt about it
but the name which sprang into mind naturally "partition_by"
has a serious shortcoming, it is not POLS in the sense that
Enumerable#partition returns an array and Enumerable#partition_by
returns a hash.
module Enumerable
def partition_by &blk
result = Hash.new {|h,k| h[k] = [] }
each do | element |
result[ element ] << blk.call element
end
result
end
end
Any ideas for a nice different name?
Does Facet implement the above maybe?
A remark for my fellow fans of inject, the same code with inject
somehow does not feel right, I do not like to use inject with this
pattern
inject(...){ |acc,ele|
...
acc
}
Thanx in advance
Robert