Skip to content

Instantly share code, notes, and snippets.

@JonathonMA
Last active December 22, 2015 01:39
Show Gist options
  • Save JonathonMA/6398082 to your computer and use it in GitHub Desktop.
Save JonathonMA/6398082 to your computer and use it in GitHub Desktop.
Cross product of an array against itself n times
def cross values, n = 3
Array.new(n, values).inject do |acc, i|
acc.product(i).map(&:flatten)
end
end
require 'set'
describe do
it "should cross itself" do
arr = [0, 128]
out = [
[0, 0, 0],
[0, 0, 128],
[0, 128, 0],
[0, 128, 128],
[128, 0, 0],
[128, 0, 128],
[128, 128, 0],
[128, 128, 128],
]
cross(arr).to_set.should eq out.to_set
end
end
@JonathonMA
Copy link
Author

sqlite3 colors.db <<_EOS_
.mode column
CREATE TABLE colors (int color_value);
INSERT INTO colors VALUES (0), (128);
SELECT * FROM colors red, colors green, colors blue;
_EOS_
rm colors.db
$ sh lol.sql
0           0           0         
0           0           128       
0           128         0         
0           128         128       
128         0           0         
128         0           128       
128         128         0         
128         128         128   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment