Skip to content

Instantly share code, notes, and snippets.

View puffnfresh's full-sized avatar

Brian McKenna puffnfresh

View GitHub Profile
@puffnfresh
puffnfresh / Payments.hs
Created March 12, 2019 06:36 — forked from friedbrice/Payments.hs
Java6-compatible algebraic data types via Church-Scott Encoding
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)
@puffnfresh
puffnfresh / closconv.lhs
Created July 13, 2017 06:22 — forked from jozefg/closconv.lhs
Tutorial on Closure Conversion and Lambda Lifting
This is my short-ish tutorial on how to implement closures in
a simple functional language: Foo.
First, some boilerplate.
> {-# LANGUAGE DeriveFunctor, TypeFamilies #-}
> import Control.Applicative
> import Control.Monad.Gen
> import Control.Monad.Writer
> import Data.Functor.Foldable
@puffnfresh
puffnfresh / GCounter.idr
Created September 30, 2016 13:18 — forked from justjoheinz/GCounter.idr
GCounter.idr
module GCounter
import Data.Vect
||| GCounter is a Conflict Free Replicated Datatype.
||| For a definition, see https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type
||| It provides update, query and merge operations.
||| merge is commutative, associative and idempotent.
data GCounter : Nat -> Type where
Nil : GCounter Z
@puffnfresh
puffnfresh / Syntax.scala
Created October 7, 2015 05:53 — forked from shajra-cs/Syntax.scala
A clever use of unapply to get syntax on Scalaz process for error handling
package shajra.extn.scalaz.stream
import scalaz.{ Bind, Traverse, Unapply }
import scalaz.stream.Process
trait Syntax {
implicit class ProcessSyntax[F[_], A](self: Process[F, A]) {
import scala.language.higherKinds
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
trait Apply[F[_]] {
val functor: Functor[F]
def ap[A, B](f: F[A => B]): F[A] => F[B]
module Reverse where
open import Data.List as List hiding (reverse)
open import Data.List.Properties
open import Relation.Binary.PropositionalEquality
record ReverseLike (reverse : ∀ {A : Set} → List A → List A) : Set₁ where
constructor rl
field
reverse0 : ∀ {A} → reverse {A} [] ≡ []
module Primes where
open import Level using (_⊔_)
open import Coinduction
open import Function
open import Data.Empty
open import Data.Unit
open import Data.Nat
open import Data.Nat.Properties
open import Data.Nat.Divisibility
@puffnfresh
puffnfresh / Main.hs
Last active December 23, 2015 15:19 — forked from mkscrg/Main.hs
module Main where
import Data.List (inits)
import Data.List.Split (splitOn)
main :: IO ()
main = print $ wordIndexes "Hello test World"
wordIndexes :: String -> [(Int, String)]
wordIndexes s =
data GenericThing = SpecificThing Char | BadThing
isSpecificThing :: GenericThing -> Bool
isSpecificThing (SpecificThing _) = True
isSpecificThing _ = False
foo :: [GenericThing] -> GenericThing
foo xs
| null xs = error "Empty GenericThings"
| all isSpecificThing xs = head xs
(function(global) {
global.def = function define(name, dependencies, moduleFactory) {
global.amdModules = global.amdModules || {};
if (Object.prototype.toString.call(dependencies) != '[object Array]' && typeof dependencies !== 'function')
throw new Error('dependencies must be an array. moduleFactory must be a function.');
global.amdModules[name] = {
'moduleFactory': moduleFactory || dependencies,
'dependencies': typeof (moduleFactory) === 'undefined' ? [] : dependencies