Skip to content

Instantly share code, notes, and snippets.

View jship's full-sized avatar

Jason Shipman jship

View GitHub Profile
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
# http://stackoverflow.com/a/9701130
GIT_COMMITTER_DATE="`date`" git commit --amend --date "`date`"
#!/usr/bin/env stack
-- stack --resolver nightly-2017-09-13 --install-ghc exec ghci --package vector
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
#!/usr/bin/env stack
-- stack --resolver lts-10.8 --install-ghc exec ghci --package lens
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens (Getter, makeLenses, view)
data Foo = Foo
{ _fooInt :: Int
, _fooString :: String
@jship
jship / rank-n-types-intuition.md
Last active May 24, 2018 15:51
RankNTypes intuition

A coworker and I were discussing RankNTypes and why the extension is used in lens. RankNTypes has confused me for a while, but making the connection to lens gave me a good intuition (strangely?). Here’s a recap in case it’s useful to anyone else:

If you have a normal higher-order function:

foo :: Num a => (a -> a) -> a
foo f = f 1

We can pass foo any function that fits the (a -> a) with a Num constraint:

@jship
jship / DictTrick.hs
Last active May 25, 2018 17:35
Small example showing the Dict trick to discover available instances from a GADT
#!/usr/bin/env stack
-- stack --resolver lts-10.8 --install-ghc exec ghci
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
-- A key type for pretend use in looking up a doggo in a database.
newtype Key a = Key { unKey :: String }
-- subForestWithParent returns the subforest of the input 'Tree', where each
-- node value in each 'Tree' of the subforest is paired with the value of its
-- parent node.
--
-- ghci> let tree = Node 1 [Node 2 [Node 4 []], Node 3 [Node 5 [Node 7 []], Node 6 []]]
-- ghci> putStrLn . drawTree . fmap show $ tree
-- 1
-- |
-- +- 2
-- | |
printTree :: Show a => Tree a -> IO ()
printTree = putStrLn . Tree.drawTree . fmap show
tree :: Tree Int
tree = Tree.unfoldTree buildNode 1 where
buildNode :: Int -> (Int, [Int])
buildNode n
| 2 * n + 1 <= 7 = (n, [2 * n, 2 * n + 1])
| otherwise = (n, [])
#!/usr/bin/env stack
-- stack --resolver lts-16.3 --install-ghc exec ghci
{-# OPTIONS_GHC -Wall -Werror #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
import Data.Set (Set)
import qualified Data.Set as Set
@jship
jship / translubernete
Created August 7, 2020 16:04
This script helps ensure my writing is precise and free of today's popular shorthand
#!/usr/bin/env stack
-- stack --resolver lts-15.6 script --package text
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Text (Text)
import Prelude
import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO