Skip to content

Instantly share code, notes, and snippets.

@elliot42
Created January 11, 2013 09:46
Show Gist options
  • Save elliot42/4509361 to your computer and use it in GitHub Desktop.
Save elliot42/4509361 to your computer and use it in GitHub Desktop.
given a set S, and an ordered list of sets T0, T1, T2...Tn, return a map from each element E in S to the first set Tk where in Tk
irb(main):064:0> fools
=> [1, 2, 3, 4, 5]
irb(main):003:0> a = [:a, [2, 3]]
=> [:a, [2, 3]]
irb(main):004:0> b = [:b, [4, 5]]
=> [:b, [4, 5]]
irb(main):059:0> [a, b].reduce([fools, {}]) do |h, s|
irb(main):060:1* coolkids = h[0] & s[1]
irb(main):061:1> coolkids_hash = Hash[coolkids.map { |ck| [ck, s[0]] }]
irb(main):062:1> [h[0] - coolkids, h[1].merge(coolkids_hash || {})]
irb(main):063:1> end
=> [[1], {5=>:b, 2=>:a, 3=>:a, 4=>:b}]
:blocked
:primary
:verified_any_type
:proxy
:unverified
:bounced
:none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment