Skip to content

Instantly share code, notes, and snippets.

View FizzBuzz.hs
{-# LANGUAGE DeriveFunctor, TemplateHaskell, FlexibleContexts #-}
module Main where
import Control.Monad.Free (MonadFree, Free (Free, Pure), liftF)
import Control.Monad.Free.TH (makeFree)
import Control.Monad (void, forM_)
data Cmd m k
= Output m k
@tekerson
tekerson / pre-commit
Created Aug 7, 2015
Some pre-commit hooks that check for things that I shouldn't be committing
View pre-commit
#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
View with-temp-directory.js
const tmp = require('tmp')
/**
* Create a temporary directory and ensure that it is cleaned up after the handler completes
* @sig (tmpDir: String -> a) -> Promise a
*/
exports.withTempDirectory = (handler) => new Promise((resolve, reject) => {
tmp.dir({ unsafeCleanup: true }, (err, tmpDir, cleanup) => {
if (err) {
reject(err)
View lotsa-ramda.js
const posLens = R.lensProp('positives')
const negLens = R.lensProp('negatives')
const positives = R.view(posLens)
const negatives = R.view(negLens)
const incrementPositiveCount = R.over(posLens, R.inc)
const incrementNegativeCount = R.over(negLens, R.inc)
const seenBoth = c => R.allPass([
@tekerson
tekerson / calculator-ast-alt.js
Last active Mar 16, 2017
With/without Intermediate Form (AST) comparison
View calculator-ast-alt.js
const AST = {
Add: (left, right) => (cases) => cases['Add'](left, right),
Multiply: (left, right) => (cases) => cases['Multiply'](left, right),
Number: (value) => (cases) => cases['Number'](value)
}
const parse = (str) => {
const stack = []
str.split(' ').forEach(token => {
switch (token) {
View MoneyInvalidAdd.idr
-- Does not compile: different currencies
add (MkMoney "EUR" 1) (MkMoney "USD" 2)
View keybase.md

Keybase proof

I hereby claim:

  • I am tekerson on github.
  • I am brenton (https://keybase.io/brenton) on keybase.
  • I have a public key ASC3ReVzC9x-LBWc51DRRX5uqVJ1immTtP-FpEWB0egoLwo

To claim this, I am signing this object:

@tekerson
tekerson / NonEmpty.ts
Created Nov 30, 2017
Possible implementation of a non-empty collection in TypeScript
View NonEmpty.ts
class NonEmpty<T> {
readonly head: T
protected tail: Array<T>
constructor(head: T, ...tail: T[]) {
this.head = head
this.tail = tail
}
static fromArray<U>(values: Array<U>): NonEmpty<U> | undefined {
if (values.length === 0) return undefined
@tekerson
tekerson / 1.1.js
Created Dec 1, 2017
Advent of Code 2017 - JavaScript
View 1.1.js
const solve = (input) => input
.split('').map(v => parseInt(v, 10))
.reduce((acc, value, i, list) => {
const pair = list[(i + 1) % list.length]
return acc + (value === pair ? value : 0)
}, 0)
@tekerson
tekerson / HigherKindedTypes.ts
Created Dec 13, 2017
Experiment with modelling Higher Kinded Types (HKTs) in TypeScript
View HigherKindedTypes.ts
interface HKT<F, A> {
__hkt: [F, A];
}
namespace FunctorNS {
export interface Functor<F, A> extends HKT<F, A> {
map<B>(f: (a: A) => B): Functor<F, B>;
}
export const map = <F, A, B>(