Skip to content

Instantly share code, notes, and snippets.

@AndrasKovacs
Created January 11, 2021 14:17
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 AndrasKovacs/97edd186ac7808339da36730c5872b7f to your computer and use it in GitHub Desktop.
Save AndrasKovacs/97edd186ac7808339da36730c5872b7f to your computer and use it in GitHub Desktop.
-- Solution to challenge: https://github.com/effectfully/haskell-challenges/tree/master/force-elems
-- (Spoiler)
data EC = Elem | Con
data T a = T {ec :: !EC, unT :: a}
appEC :: EC -> (a -> b) -> a -> b
appEC Elem f a = f $! a
appEC Con f a = f a
instance Functor T where
fmap f ta = T Con $ appEC (ec ta) f (unT ta)
instance Applicative T where
pure = T Con
f <*> a = T Con $ appEC (ec a) (unT f) (unT a)
forceElems :: Traversable t => t a -> t a
forceElems = unT . traverse (T Elem)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment