Skip to content

Instantly share code, notes, and snippets.

View jship's full-sized avatar

Jason Shipman jship

View GitHub Profile
@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
{- 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)
{- 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
@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
@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:
@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
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
@mrkgnao
mrkgnao / Liege.hs
Last active August 27, 2019 18:11
Servant handled using free monads of coproducts of functors :)
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE IncoherentInstances #-}
{-# LANGUAGE InstanceSigs #-}
@mtesseract
mtesseract / haskell-records.md
Last active March 8, 2023 22:25
Working around Haskell's namespace problem for records

The Problem

Defining records in Haskell causes accessor functions for the record's fields to be defined. There is no seperate namespace for these accessor functions.

The Goal

Be able to

  • use records in Haskell, which share field names.
  • use lenses for accessing these fields
@chrisdone
chrisdone / FieldTH.hs
Last active February 13, 2017 22:17
$(lens 'foo) -- handy one-off lens maker for record fields
{-# LANGUAGE TemplateHaskell #-}
-- | For when you have a record that doesn't have lenses derived for
-- it and you need a lens, just use @$(lens 'thefield)@ and away you go.
module Control.Lens.FieldTH where
import Language.Haskell.TH
lens :: Name -> Q Exp
lens name = do
[|\f r ->
fmap
$(lamE