Skip to content

Instantly share code, notes, and snippets.

@chrisdone
chrisdone / intero-file-embed.hs
Created October 7, 2019 13:21
intero-file-embed
{-# LANGUAGE CPP #-}
-- |
module Data.FileEmbed2
( makeRelativeToProject2
, module Data.FileEmbed
) where
import Data.FileEmbed
import Language.Haskell.TH
@chrisdone
chrisdone / Web.hs
Created October 3, 2019 14:08
Web yesod wrapper
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE GADTs #-}
-- | A restricted web type.
module Web
( Web(..)
, runWebHandler
{-# LANGUAGE BangPatterns #-}
import Control.DeepSeq
import Data.List hiding (foldl)
import Prelude hiding (foldl)
foldl = \f accumulator list ->
case list of
[] -> accumulator
x:xs -> foldlS f (f accumulator x) xs
@chrisdone
chrisdone / foldl-vs-foldl-strict.md
Last active November 11, 2019 19:49
Strict fold vs regular fold

Duet's folds example

Duet has this folding example:

data List a = Nil | Cons a (List a)
foldr = \f z l ->
  case l of
    Nil -> z
    Cons x xs -> f x (foldr f z xs)
-- | Execute the steps.
execute :: [SomeStep] -> RIO MainEnv ()
execute steps = do
resources <- newIORef mempty
mapRIO
(\MainEnv {logger} -> ExecuteEnv {logger = logger . ExecuteLog, resources})
(mapM_ executeSomeStep steps)
@chrisdone
chrisdone / file-embed-demo.hs
Created August 14, 2019 08:55
File embed expansion demo
> :set -XOverloadedStrings
> :set -XTemplateHaskell
> import Language.Haskell.TH
> $(bsToExp "Hello, World" >>= stringE . show)
"AppE (VarE GHC.IO.Unsafe.unsafePerformIO) (AppE (AppE (VarE Data.ByteString.Unsafe.unsafePackAddressLen) \
\(LitE (IntegerL 12))) (LitE (StringPrimL [72,101,108,108,111,44,32,87,111,114,108,100])))"
>
> :set -ddump-splices
@chrisdone
chrisdone / cleaner-forms.hs
Last active August 12, 2019 09:10
forms experimentation type family
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Bifunctor
@chrisdone
chrisdone / ShuffleGraph.hs
Last active August 7, 2019 11:26
Shuffle a graph
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Graph.Shuffle where
-- | Shuffle a graph into a randomly sorted list, preserving
-- topological order.
{-
> quickCheckWith stdArgs {maxSuccess=10000} prop_identity
@chrisdone
chrisdone / StableShuffle.hs
Last active August 6, 2019 17:02
Graph stable shuffle
{-# LANGUAGE PartialTypeSignatures #-}
module StableShuffle where
import Control.Monad
import Control.Monad.Random
import Data.Bifunctor
import Data.List
import qualified Data.Map.Strict as M
import Data.Maybe
import Data.Ord
@chrisdone
chrisdone / StableShuffle.hs
Created August 6, 2019 15:54
StableShuffle.hs
{-# LANGUAGE PartialTypeSignatures #-}
module StableShuffle where
import Control.Monad
import Control.Monad.Random.Class
import Data.List
import qualified Data.Map.Strict as M
import Data.Maybe
import Data.Ord
import System.Random.Shuffle