Created
April 26, 2014 15:17
-
-
Save nuboat/11322665 to your computer and use it in GitHub Desktop.
MapReduce.rb
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
{1=>[1,1], | |
3=>[1,1,1,1], | |
5=>[1,1,1] | |
7=>[1,1], | |
2=>[1,1,1]} |
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
require 'pp' | |
nums = [1,3,5,7,2,3,1,3,2,5,2,3,5,7] | |
maps = nums.map { |e| {:key=>e, :value=>1} } | |
hashes = maps.group_by { |hash| hash[:key] } | |
pp hashes |
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
{1=>[{:key=>1, :value=>1}, {:key=>1, :value=>1}], | |
3=> | |
[{:key=>3, :value=>1}, | |
{:key=>3, :value=>1}, | |
{:key=>3, :value=>1}, | |
{:key=>3, :value=>1}], | |
5=>[{:key=>5, :value=>1}, {:key=>5, :value=>1}, {:key=>5, :value=>1}], | |
7=>[{:key=>7, :value=>1}, {:key=>7, :value=>1}], | |
2=>[{:key=>2, :value=>1}, {:key=>2, :value=>1}, {:key=>2, :value=>1}]} |
.map { |k,v| [k,v.map { |h| h[:value] }]}.to_h ประโยคนี้เลยที่อยากได้ ... ห้า ว่าแต่มันอ่านว่ายังไงหว่า
จะว่าไป มันก็เหมือน List Complehensive ใน Python เหมือนกันนะ ค่อยๆ ซึมซับ
ผมเล่นด้วย
nums = [1,3,5,7,2,3,1,3,2,5,2,3,5,7]
hash = nums.each_with_object(Hash.new {[]}) { |v, hash| hash[v] <<= 1 }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ยังไม่ค่อยเข้าใจโจทย์เท่าไหร่ เดาเอาว่าประมาณนี้หรือเปล่าครับ