Skip to content

Instantly share code, notes, and snippets.

@AphonicChaos
Last active March 15, 2016 20:49
Show Gist options
  • Save AphonicChaos/6132a1501aa80384ff96 to your computer and use it in GitHub Desktop.
Save AphonicChaos/6132a1501aa80384ff96 to your computer and use it in GitHub Desktop.
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)))
{-# 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
{-# 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