Skip to content

Instantly share code, notes, and snippets.

@rsiddle
Last active June 9, 2017 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsiddle/a6808c6cf2f37b6a46828dd4d3f3b7c4 to your computer and use it in GitHub Desktop.
Save rsiddle/a6808c6cf2f37b6a46828dd4d3f3b7c4 to your computer and use it in GitHub Desktop.
d-2.rb
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