Skip to content

Instantly share code, notes, and snippets.

@nkpart
Last active September 5, 2018 13:50
Show Gist options
  • Save nkpart/8922083d3c18a8f777b8 to your computer and use it in GitHub Desktop.
Save nkpart/8922083d3c18a8f777b8 to your computer and use it in GitHub Desktop.
{-# LANGUAGE GADTs #-}
module Yolo where
import System.IO.Unsafe
class Yolo f where
yolo :: f a -> a
instance Yolo Maybe where
yolo (Just x) = x
instance Yolo (Either a) where
yolo (Right x) = x
instance Yolo ([]) where yolo = head
data ReadMe a = Read a => ReadMe String
instance Yolo ReadMe where
yolo (ReadMe s) = read s
instance Yolo IO where
yolo = unsafePerformIO
-- > yolo (Just 3)
-- 3
-- > yolo (Right 3)
-- 3
-- > take 5 $ yolo (readFile "Yolo.hs")
-- "{-# L"}"
-- > yolo (ReadMe "5" :: ReadMe Int)
-- 5
@HuwCampbell
Copy link

import Control.Monad.Trans.Except
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Writer
import Control.Monad.Trans.Identity
import Data.Functor.Identity

instance Yolo m => Yolo (ExceptT e m) where
  yolo (ExceptT act) = yolo . yolo $ act

instance Yolo m => Yolo (MaybeT m) where
  yolo (MaybeT act) = yolo . yolo $ act

instance Yolo m => Yolo (WriterT w m) where
  yolo (WriterT act) = fst . yolo $ act

instance Yolo f => Yolo (IdentityT f) where
  yolo (IdentityT act) = yolo $ act

instance Yolo Identity where
  yolo (Identity act) = act

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment