Class: Streamer::Functors::Group
- Defined in:
- lib/streamer/functors/group.rb
Overview
Group groups a list by a property and a function
Instance Attribute Summary
Attributes inherited from Functor
Instance Method Summary collapse
Methods inherited from Functor
#class_name, #initialize, #type_name
Constructor Details
This class inherits a constructor from Streamer::Functors::Functor
Instance Method Details
#accumulate(list, group_key, operand_key, operator) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/streamer/functors/group.rb', line 16 def accumulate(list, group_key, operand_key, operator) list.each_with_object({}) do |item, val| val[item[group_key]] = (val[item[group_key]] || 0.0).send( operator, (item[operand_key].to_f || 0) ) end end |
#call ⇒ Object
5 6 7 |
# File 'lib/streamer/functors/group.rb', line 5 def call group end |
#group ⇒ Object
9 10 11 12 13 14 |
# File 'lib/streamer/functors/group.rb', line 9 def group group_key = .fetch(:by) operand_key = .fetch(:operand) operator = .fetch(:operator).to_sym accumulate(list, group_key, operand_key, operator) end |