Skip to content

Instantly share code, notes, and snippets.

View jship's full-sized avatar

Jason Shipman jship

View GitHub Profile
import Control.Monad.IO.Class
import Control.Monad.Codensity
import System.IO
managedActions :: Codensity IO ()
managedActions = do
input <- Codensity $ withFile "in.txt" ReadMode
output <- Codensity $ withFile "out.txt" WriteMode
contents <- liftIO $ hGetContents input
@marcosh
marcosh / Application.hs
Created June 14, 2018 13:20
Web applications as profunctors
module Application where
import Data.Profunctor
newtype Application request response = Application {unApplication :: request -> IO response}
instance Profunctor Application where
dimap actOnRequest actOnResponse application = Application $ (fmap actOnResponse) . (unApplication application) . actOnRequest
@gelisam
gelisam / ApplicativeMap.hs
Created August 26, 2018 15:58
Combining Maps using the Applicative idiom: f <$$> map1 <**> map2 <**> map3
-- Combining Maps using the Applicative idiom.
{-# LANGUAGE DeriveFunctor, DeriveFoldable, DeriveTraversable #-}
module ApplicativeMap where
import Data.Map.Strict (Map)
import qualified Data.Map as Map
-- When combining multiple Applicative computations, a common Haskell idiom is
-- to use (<$>) and (<*>) to combine the results using a function:
@carymrobbins
carymrobbins / NoInlineWhere.hs
Last active July 29, 2019 16:46
What happens when you use NOINLINE on a function defined in a where clause? Hint: optimization levels matter!
import System.IO.Unsafe
main :: IO ()
main = do
thing 1
thing 2
thing 3
thing' 1
thing' 2
{- stack --resolver lts-16.8 --install-ghc exec ghci --package "protolude text binary" -}
{-# LANGUAGE DeriveGeneric, DuplicateRecordFields, ExistentialQuantification, FlexibleContexts, RankNTypes, ScopedTypeVariables, StandaloneDeriving #-}
{- ghcid -c "stack X.hs" -}
module Existenial where
import Data.Text
import Data.Binary
import GHC.Generics
{- cabal:
build-depends: base
build-depends: transformers
ghc-options: -threaded
-}
import Control.Concurrent (myThreadId, threadDelay, forkOS)
import Control.Monad (forever, void)
import Control.Monad.IO.Class (liftIO)
@gelisam
gelisam / ServantGenericLinks.hs
Last active October 11, 2022 16:46
How to get a record of record of links from a record of records of Servant routes