Last active
June 9, 2017 12:43
-
-
Save rsiddle/a6808c6cf2f37b6a46828dd4d3f3b7c4 to your computer and use it in GitHub Desktop.
d-2.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
def d(a, b, &block) | |
va = a.is_a?(Array) ? 1 : 0 | |
vb = b.is_a?(Array) ? 2 : 0 | |
v = va + vb | |
case v | |
when 0 | |
block.call(a, b) | |
when 1 | |
a.map do |e| | |
d(e, b, &block) | |
end | |
when 2 | |
b.map do |e| | |
d(a, e, &block) | |
end | |
when 3 | |
al = a.length | |
bl = b.length | |
s = [al, bl].max | |
(0...s).collect do |e| | |
d(a[e % al], b[e % bl], &block) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment