enumerate-0.1.1: enumerate all the values in a finite type (automatically)

Safe HaskellNone
LanguageHaskell2010

Data.Enumerate.Function

Description

orphan instances, of 'Enumerate'\/'Eq'\/'Show', for functions.

(that are included for completeness, but not exported by default (i.e. by Data.Enumerate). you probably want build-time instance-resolution errors rather than possible runtime non-termination).

-- doctest
>>> :set -XLambdaCase
>>> let printMappings mappings = traverse (\mapping -> (putStrLn"") >> (traverse print) mapping) mappings >> return()

Synopsis

Documentation

unsafeFromList :: Ord a => [(a, b)] -> a -> b Source

wraps lookup

>>> (unsafeFromList [(False,True),(True,False)]) False
True
>>> (unsafeFromList [(False,True),(True,False)]) True
False

mappingEnumeratedAt :: [a] -> [b] -> [[(a, b)]] Source

[(a,b)] is a mapping, [[(a,b)]] is a list of mappings.

>>> let orderingPredicates = mappingEnumeratedAt [LT,EQ,GT] [False,True]
>>> print $ length orderingPredicates
8
>>> printMappings $ orderingPredicates

(LT,False)
(EQ,False)
(GT,False)

(LT,False)
(EQ,False)
(GT,True)

(LT,False)
(EQ,True)
(GT,False)

(LT,False)
(EQ,True)
(GT,True)

(LT,True)
(EQ,False)
(GT,False)

(LT,True)
(EQ,False)
(GT,True)

(LT,True)
(EQ,True)
(GT,False)

(LT,True)
(EQ,True)
(GT,True)

(LT,False)
(EQ,False)
(GT,False)

(LT,False)
(EQ,False)
(GT,True)

(LT,False)
(EQ,True)
(GT,False)

(LT,False)
(EQ,True)
(GT,True)

(LT,True)
(EQ,False)
(GT,False)

(LT,True)
(EQ,False)
(GT,True)

(LT,True)
(EQ,True)
(GT,False)

(LT,True)
(EQ,True)
(GT,True)

where the (total) mapping:

(LT,False)
(EQ,False)
(GT,True)

is equivalent to the function:

\case
 LT -> False
 EQ -> False
 GT -> True

crossProduct :: [a] -> [b] -> [[(a, b)]] Source

>>> let crossOrderingBoolean = crossProduct [LT,EQ,GT] [False,True]
>>> printMappings $ crossOrderingBoolean
>>> 
(LT,False)
(LT,True)

(EQ,False)
(EQ,True)

(GT,False)
(GT,True)

the length of the outer list is the size of the first set and the length of the inner list is the size of the second set.

>>> print $ length crossOrderingBoolean
3
>>> print $ length (head crossOrderingBoolean)
2