Skip to content

Instantly share code, notes, and snippets.

@soupi
soupi / kinds.hs
Created October 10, 2023 09:02
kind inference with rows/variants
#!/usr/bin/env cabal
{- cabal:
build-depends: base, mtl, containers, uniplate
ghc-options: -Wall
-}
-- | An example of a kind inference for data types using
-- unification-based constraint solving.
--
@soupi
soupi / kinds.hs
Last active July 17, 2024 14:04
Kind inference using unification-base constraint solving.
#!/usr/bin/env cabal
{- cabal:
build-depends: base, mtl, containers, uniplate
ghc-options: -Wall
-}
-- | An example of a kind inference for data types using
-- unification-based constraint solving.
--
-- See the blog post:
@soupi
soupi / eval_with_reader.hs
Last active January 21, 2022 16:23
How to use Reader and IORef for static scope and mutation
{-# options_ghc -Wall #-}
{-# language LambdaCase #-}
-- | Run repl with:
--
-- > cabal repl --build-depends containers --build-depends mtl
--
-- or
--
-- > stack exec --package containers --package mtl -- ghci
@soupi
soupi / upload.hs
Created November 13, 2021 14:32
upload a file to http server
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Web.Scotty
import Control.Monad.IO.Class
import Network.Wai.Middleware.RequestLogger
import Network.Wai.Middleware.Static
import Network.Wai.Parse
@soupi
soupi / moving-gc.txt
Last active January 25, 2021 21:47
output of -sstderr for running nyx-game with --nonmoving-gc
5,249,078,184 bytes allocated in the heap
234,375,952 bytes copied during GC
9,022,600 bytes maximum residency (8 sample(s))
1,733,392 bytes maximum slop
24 MiB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 5100 colls, 0 par 0.323s 0.328s 0.0001s 0.0005s
Gen 1 8 colls, 0 par 0.020s 0.020s 0.0025s 0.0056s
@soupi
soupi / client.hs
Created July 1, 2020 14:14
Echo server/client haskell
module Main (main) where
import Control.Monad (forever, unless)
import qualified Data.ByteString.Char8 as C
import Network.Socket
import Network.Run.TCP (runTCPClient)
import Network.Socket.ByteString (recv, sendAll)
import System.IO (hFlush, stdout)
main :: IO ()
@soupi
soupi / bfs.hs
Created May 10, 2020 19:48
shortest path using bfs
{-# LANGUAGE ViewPatterns, TupleSections #-}
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Sequence as Seq
import Control.Monad.Except
type User = String
type From = User
type To = User
@soupi
soupi / setup-stack-ghc-shortcuts.sh
Last active May 10, 2020 08:18
stack ghc shortcuts
#!/bin/bash
if [ ! -f ~/.local/bin/ghc ]; then
echo '#!/bin/bash' > ~/.local/bin/ghc;
echo 'stack exec -- ghc $@' >> ~/.local/bin/ghc;
chmod +x ~/.local/bin/ghc;
echo "~/.local/bin/ghc created."
else
echo "~/.local/bin/ghc already exists."
fi
@soupi
soupi / cmus_now_playing.hs
Last active July 21, 2020 05:57
Fancily write to file which song is playing in cmus (can be read from obs using textmonitor script)
#!/usr/bin/env stack
-- stack --resolver lts-15.7 script --package text --package process
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import qualified Data.Text as T
import qualified Data.Text.IO as T
import System.Process
@soupi
soupi / Plate-for-dump.hs
Last active February 1, 2020 06:45
Plate.hs src
-- stack exec --package uniplate --package criterion -- ghc Plate.hs -O2 -ddump-simpl -dsuppress-all > /tmp/Plate-dump.hs
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
import GHC.Generics
import Transform
data Expr