Skip to content

Instantly share code, notes, and snippets.

View douglascodes's full-sized avatar

Douglas King douglascodes

View GitHub Profile
@douglascodes
douglascodes / controlled_perm.rb
Last active December 15, 2015 12:39
A controlled permutation. Cycles an array of arrays calling the proc with each possible set of the sub arrays whilst preserving the positions of the original array. Basically gets every combination of "One from column A, one from column B, ..." and so on.
a_of_a = [["Jazz", "Soul", "Rock"],["Cheeseburgers", "Hot dogs", "Milkshakes"], ["cable", "radio", "internet"]]
result = Array.new(a_of_a.length)
procky = Proc.new { |arg|
print arg.join(" and "), " are my favorite things. \n"
}
def controlled_perm(x, a_of_a, result, procky)
x += 1
if a_of_a.length == x
procky.call(result)
return