Skip to content

Commit

Permalink
reorganize code slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
ashanbrown committed Oct 10, 2014
1 parent 88063ac commit 41a539b
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/chartnado/series/wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,29 @@ def add(*series, scalar_sum: 0.0)
(series, scalars) = [__getobj__, *series].partition { |s| s.respond_to?(:map) }
scalar_sum += scalars.reduce(:+) || 0.0

result =
if wrap(series.first).dimensions == 3
series.map(&:to_a).flatten(1).group_by(&:first).map do |name, values|
data = values.map(&:second).reduce(Hash.new(scalar_sum)) do |hash, values|
values.each do |key, value|
hash[key] += value
end
hash
if wrap(series.first).dimensions == 3
result = series.map(&:to_a).flatten(1).group_by(&:first).map do |name, values|
data = values.map(&:second).reduce(Hash.new(scalar_sum)) do |hash, values|
values.each do |key, value|
hash[key] += value
end
[
name, data
]
end
elsif series.first.is_a?(Hash)
keys = series.flat_map(&:keys).uniq
keys.reduce({}) do |hash, key|
hash[key] = (series.map { |s| s[key] }.compact.reduce(:+) || 0) + scalar_sum
hash
end
elsif series.first.is_a?(Array)
series.map { |s| s.reduce(:+) + scalar_sum }
else
scalar_sum
[
name, data
]
end
elsif series.first.is_a?(Hash)
keys = series.flat_map(&:keys).uniq
result = keys.reduce({}) do |hash, key|
hash[key] = (series.map { |s| s[key] }.compact.reduce(:+) || 0) + scalar_sum
hash
end
elsif series.first.is_a?(Array)
result = series.map { |s| s.reduce(:+) + scalar_sum }
else
result = scalar_sum
end

wrap(result)
end
Expand Down

0 comments on commit 41a539b

Please sign in to comment.