Skip to content

Instantly share code, notes, and snippets.

@thehenster
Last active August 29, 2015 13:57
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 thehenster/9374235 to your computer and use it in GitHub Desktop.
Save thehenster/9374235 to your computer and use it in GitHub Desktop.
A ruby version of the Haskell colour problem in seven languages in seven weeks
def colour_combinations(colours=[])
colours.flat_map{|x| colours.flat_map{|y| x <= y ? [[x, y]] : [] } }
end
require 'minitest/autorun'
require 'minitest/unit'
class TestColours < MiniTest::Unit::TestCase
def test_colour_combinations
colours = ["black", "white", "blue", "yellow", "red"]
combinations = colour_combinations(colours)
assert combinations.include?(["black", "black"])
assert combinations.include?(["black", "blue"])
refute combinations.include?(["blue", "black"])
assert_equal 15, combinations.size
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment