Created
June 16, 2015 20:43
-
-
Save budu/98fd7a45dfc614e6e1a7 to your computer and use it in GitHub Desktop.
Hash transpose
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
Hash.class_eval do | |
def transpose | |
reduce({}) do |rmemo, (rk, rv)| | |
rv.reduce(rmemo) do |cmemo, (ck, cv)| | |
cmemo[ck] ||= {} | |
cmemo[ck][rk] = cv | |
cmemo | |
end | |
rmemo | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment