Can anyone come up with a functional (no mutating variables) way in ruby to do the following?
Given this sorted data:
data = [{1 => "A"},
{2 => "B"},
{3 => "B"},
{4 => "C"},
{5 => "D"},
{6 => "D"},
{7 => "D"}]
Yield this
[{"start" => 1, "end" => 1, "value" => "A"},
{"start" => 2, "end" => 3, "value" => "B"},
{"start" => 4, "end" => 4, "value" => "C"},
{"start" => 5, "end" => 7, "value" => "D"}]
data.group_by {|h| h.values.first }.map {|v, a| {"start" => a.first.keys.first, "end" => a.last.keys.first, "value" => v } }– Ron Garrity Aug 22 '12 at 13:41