Skip to content

Instantly share code, notes, and snippets.

View ChrisPenner's full-sized avatar
:bowtie:
Happily Hacking

Chris Penner ChrisPenner

:bowtie:
Happily Hacking
View GitHub Profile
@ChrisPenner
ChrisPenner / FreeProfunctor.hs
Created March 20, 2021 15:13
'Progressive' Free profunctors!
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
module Data.Profunctor.Free where
import Data.Profunctor
import Control.Category (Category, (>>>))
data IsCat =
HasCategory
@ChrisPenner
ChrisPenner / git-transplant
Last active February 12, 2021 15:26
Transplant commits from one source to another
#!/bin/bash
if [[ $# -ne 2 ]]; then
cat >&2 <<EOF
Transplant a branch from one root to another.
Handy if you've done a squash-merge and need to rebase <from> the old branch <onto> the new master.
Usage:
git transplant <from> <to>
where:
@ChrisPenner
ChrisPenner / RequireTypeAp.hs
Created November 18, 2020 18:19
A hack to require type applications, even when not necessary
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
-- Don't export this, it's just to make it so the type family *could* have a different
@ChrisPenner
ChrisPenner / SemiRepresentable.hs
Created October 12, 2020 04:46
SemiRepresentable
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
module SemiRepresentable where
import qualified Data.Map as M
import Numeric.Natural
import qualified Data.Set as S
import Data.These
-- Bind an async to be cancelled when the current computation ends.
bindAsync :: IO a -> ContT r IO ()
bindAsync m = do
ContT $ \cc -> do
withAsync m . const $ cc ()
-- Use bindAsync to auto-cancel the thread when the containing computation finishes
testBoundAsync :: ContT r IO ()
testBoundAsync = do
bindAsync . forever $ print "can you hear me now?"
@ChrisPenner
ChrisPenner / MapF.hs
Last active September 20, 2020 19:36
MapF
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
module MyMap where
@ChrisPenner
ChrisPenner / factorial.py
Last active September 19, 2020 08:13
Tail Recursion in Python without Introspection
from tail_recursion import tail_recursive, recurse
# Normal recursion depth maxes out at 980, this one works indefinitely
@tail_recursive
def factorial(n, accumulator=1):
if n == 0:
return accumulator
recurse(n-1, accumulator=accumulator*n)
@ChrisPenner
ChrisPenner / haddock-up.sh
Created June 24, 2020 15:17
Fix Haddock Docs
#!/bin/bash
# Adapted from script by Dimitri Sabadie <dimitri.sabadie@gmail.com>
dist=$(stack path --dist-dir --stack-yaml ./stack.yaml)
packagename=$(awk '/^name:\s*(.*)/{ print $2 }' ./*.cabal)
packageversion=$(awk '/^version:\s*(.*)/{ print $2 }' ./*.cabal)
echo -e "\033[1;36mGenerating documentation for $packagename-$packageversion\033[0m"
@ChrisPenner
ChrisPenner / TextOptics.hs
Created April 19, 2020 21:53
Optics for doing some text manipulation
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE FlexibleContexts #-}
module Lib where
import Control.Lens
import Control.Applicative
import qualified Data.Text as T
@ChrisPenner
ChrisPenner / cap.sh
Created April 5, 2020 18:16
Capture can replay command line output
#!/bin/bash
capdir=/tmp/cap
mkdir -p "$capdir"
record() {
tee "${capdir}/${1:-default}"
}
replay() {