Skip to content

Instantly share code, notes, and snippets.

Brian McKenna puffnfresh

View GitHub Profile
View LoadedData.ts
type LoadedData<A> =
<B>(f: (a: A) => B, g: (e: Error) => B, b: B) => B;
function loaded<A>(a: A): LoadedData<A> {
return <B>(f: (a: A) => B, error: (e: Error) => B, empty: B) => f(a);
}
function error<A>(e: Error): LoadedData<A> {
return <B>(success: (a: A) => B, f: (e: Error) => B, empty: B) => f(e);
}
@puffnfresh
puffnfresh / Payments.hs
Created Mar 12, 2019 — forked from friedbrice/Payments.hs
Java6-compatible algebraic data types via Church-Scott Encoding
View Payments.hs
module Payments where
data Customer = Customer { name :: String, age :: Int } deriving (Eq, Ord, Show)
-- I know partial record fields is an anti-pattern, but who's counting?
data Payment
= Cash { customer :: Customer, amount :: Double }
| Credit { customer :: Customer, amount :: Double, cardNumber :: Int }
| Check { customer :: Customer, amount :: Double, routingNumber :: Int, accountNumber :: Int }
deriving (Eq, Ord, Show)
View spacemacs2nix.el
#!/usr/bin/env nix-shell
#! nix-shell -i "emacs --batch -l $HOME/.emacs.d/core/core-load-paths.el -l" -p emacs26-nox
(require 'core-configuration-layer)
(configuration-layer/discover-layers)
(configuration-layer/make-all-packages nil)
(princ "p:\n")
(princ "let checked = n:\n")
(princ " let p' = p.${n} or null;\n")
(princ " in if p'.meta.broken or false then null else p';\n")
View Scott.hs
{-# LANGUAGE ScopedTypeVariables, Rank2Types #-}
module ScottEncoding where
import Prelude hiding (null, length, map, foldl, foldr, take, fst, snd, curry, uncurry, concat, zip, (++))
newtype SMaybe a
= SMaybe { runMaybe :: forall b. b -> (a -> b) -> b }
newtype SList a
= SList { runList :: forall b. b -> (a -> SList a -> b) -> b }
View ghci
Prelude Language.Haskell.TH.Cleanup Generics.Deriving.TH
λ data X = A | B
Prelude Language.Haskell.TH.Cleanup Generics.Deriving.TH
λ putStrLn $(simplifiedTH =<< deriveAll0 ''X)
instance Generic (X :: *)
where type Rep (X :: *) = D1 ('MetaData "X" "Ghci3" "interactive" 'False) (:+: (C1 ('MetaCons "A" 'PrefixI 'False) U1) (C1 ('MetaCons "B" 'PrefixI 'False) U1))
from val_0 = case val_0 of
y_1 -> M1 (case y_1 of
A -> L1 (M1 U1)
B -> R1 (M1 U1))
View ReverseFunction.agda
open import Data.List
open import Data.List.Properties
open import Relation.Binary.PropositionalEquality
record ReverseFunction : Set₁ where
field
f : {A} List A List A
rev : {A} (xs ys : List A) f (xs ++ ys) ≡ f ys ++ f xs
sing : {A} (x : A) f (x ∷ []) ≡ x ∷ []
@puffnfresh
puffnfresh / Example.hs
Created Mar 14, 2018
Prism' as a smart constructor
View Example.hs
module Example (Example, example) where
import Data.Functor (($>))
import Control.Lens (Prism', prism')
import Control.Monad (guard)
data Example
= Example String
deriving (Eq, Ord, Show)
View MonoidObject.hs
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
import Control.Applicative
import Control.Comonad
import Control.Monad
import Data.Functor.Compose
import Data.Functor.Contravariant
import qualified Data.Functor.Contravariant.Day as Contravariant
View ApplyDivide.hs
import Data.Functor.Invariant
class Invariant f => ApplyDivide f where
apdivide :: (a -> b -> c) -> (c -> (a, b)) -> f a -> f b -> f c
-- Example
data Semigroup' a
= Semigroup' { append :: a -> a -> a }
View TH.hs
removeModName :: Name -> Name
removeModName =
filterModName (const False)
removeAllModNames :: Dec -> Dec
removeAllModNames =
transformOnOf (_SigD . _2) typeChildren (typeName %~ removeModName) .
transformOnOf (_ValD . _2 . bodyExp) expChildren (expName %~ removeModName)
simplify :: Dec -> Dec
You can’t perform that action at this time.