Last active
March 15, 2016 20:49
-
-
Save AphonicChaos/6132a1501aa80384ff96 to your computer and use it in GitHub Desktop.
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 compose(*functions): | |
return reduce(lambda x, y: lambda z: x(y(z)), functions) | |
def issubset(x): | |
return lambda y: set(x).issubset(y) | |
def isdisjoint(x): | |
return lambda y: set(x).isdisjoint(y) | |
def require(have, need, compare=issubset): | |
"""Returns false when requirements aren't met.""" | |
return compare(have)(need) | |
def negate(x): | |
return not x | |
print "Should pass:" | |
print require([1, 2, 3], [1, 2, 3, 4]) | |
print "Should fail:" | |
print require([1, 2, 3], [1, 2]) | |
print "composed should pass:" | |
print require([1, 2, 3], [1, 2, 3, 4], compare=lambda x: compose(negate, isdisjoint(x))) |
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
{-# LANGUAGE OverloadedLists #-} | |
module Main where | |
import Data.Set | |
import Prelude hiding (null) | |
isDisjoint x = not . null . intersection x | |
require' xs ys cmp = cmp xs ys | |
require xs ys = require' xs ys isSubsetOf | |
main = do | |
print "Should pass:" | |
print $ require [1, 2, 3] [1, 2, 3, 4] | |
print "Should fail:" | |
print $ require [1, 2, 3] [1, 2, 3] | |
print "Composed should pass:" | |
print $ require' [1, 2, 3] [1, 2, 3, 4] $ \x -> not . isDisjoint x |
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
{-# LANGUAGE OverloadedLists #-} | |
module Main where | |
import Data.Set | |
import Prelude hiding (null) | |
atLeastOne x = null . intersection x | |
isDisjoint x = not . atLeastOne x | |
requireWith cmp = cmp | |
require = requireWith isSubsetOf | |
main = do | |
print "Should pass:" | |
print $ require [1, 2, 3] [1, 2, 3, 4] | |
print "Should fail:" | |
print $ require [1, 2, 3] [1, 2, 3] | |
print "Composed should pass:" | |
print $ requireWith atLeastOne [1, 2, 3] [1, 2, 3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment