Skip to content

Instantly share code, notes, and snippets.

@missingfaktor
Created February 3, 2019 13:15
Show Gist options
  • Select an option

  • Save missingfaktor/92b3b6008d9a7caf4393e09ba09b35d9 to your computer and use it in GitHub Desktop.

Select an option

Save missingfaktor/92b3b6008d9a7caf4393e09ba09b35d9 to your computer and use it in GitHub Desktop.
haskell> :set -XGeneralizedNewtypeDeriving
haskell> newtype Possibilities a = AnyOf { possibilities :: [a] } deriving (Functor, Applicative, Monad, Eq, Show)
haskell>
haskell> flipACoin = AnyOf ["head", "tail"]
haskell> throwADice = AnyOf [1, 2, 3, 4, 5, 6]
haskell>
haskell> import Control.Applicative
haskell>
haskell> (,) <$> flipACoin <*> throwADice
AnyOf {possibilities = [("head",1),("head",2),("head",3),("head",4),("head",5),("head",6),("tail",1),("tail",2),("tail",3),("tail",4),("tail",5),("tail",6)]}
haskell>
haskell> (,) <$> throwADice <*> throwADice
AnyOf {possibilities = [(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(2,1),(2,2),(2,3),(2,4),(2,5),(2,6),(3,1),(3,2),(3,3),(3,4),(3,5),(3,6),(4,1),(4,2),(4,3),(4,4),(4,5),(4,6),(5,1),(5,2),(5,3),(5,4),(5,5),(5,6),(6,1),(6,2),(6,3),(6,4),(6,5),(6,6)]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment