Skip to content

Instantly share code, notes, and snippets.

@gatlin
gatlin / adt.rkt
Created February 18, 2014 00:41
Playing with ADTs and other bullshit
#lang racket
; simple cons pairs
(define (my-cons fst snd)
(λ (f) (f fst snd)))
(define (my-first c)
(c (λ (a b) a)))
(define (my-second c)
@gatlin
gatlin / booldef.rkt
Last active August 29, 2015 13:57
Languages don't need built-in conditionals! Bah!
(define True (λ () (λ (a b) (a))))
(define False (λ () (λ (a b) (b))))
@gatlin
gatlin / simple_alarm.hs
Created April 15, 2014 10:21
Load this into ghci, load a song into your MPD playlist, and then run startTimer with the correct number of seconds.
{-# LANGUAGE OverloadedStrings #-}
import Network.MPD
import Control.Concurrent.Timer
import Control.Concurrent.Suspend.Lifted
import System.Environment
startTimer seconds = oneShotTimer (withMPD (play Nothing) >> return ()) (sDelay (fromInteger seconds))
@gatlin
gatlin / lisp.py
Last active August 29, 2015 14:00
Tail recursive, algebraic definition of a simple cons list in Python, along with some example functions.
def tail_rec(fun):
'''
Receive a function `fun` as an argument;
Return a function which accepts `fun` and runs it in a loop.
'''
def tail(fun):
a = fun
while callable(a):
a = a()
return a
@gatlin
gatlin / fizzbuzz.hs
Last active August 29, 2015 14:01
Haskell FizzBuzz solution using monoids. I didn't write this but I wanted to separate it from its original article for didactic reasons.
{-# LANGUAGE MonadComprehensions #-}
-- From https://bmark.us/bmark/readable/060063d3dd0330
import Data.Monoid ((<>))
import Data.Maybe (fromMaybe)
import Control.Monad (mapM_)
fizzbuzz x = fromMaybe (show x) $ [ "fizz" | x `rem` 3 == 0 ]
<> [ "buzz" | x `rem` 5 == 0 ]
@gatlin
gatlin / free_monad_functor_adt.hs
Created June 10, 2014 07:50
Two things in one: functors implemented as algebraic data types; and a free monad derived automatically from it. No typeclasses necessary!
{-# LANGUAGE RankNTypes #-}
data F f = F {
_map :: forall a b. (a -> b) -> f a -> f b
}
data Mu f a
= Term { retract :: a }
| Cont (f (Mu f a))
@gatlin
gatlin / freestream.hs
Last active August 29, 2015 14:03
Iteratee-based Streaming utilities from a free monad
{- | THIS HAS MOVED
-
- https://github.com/gatlin/FreeStream
-}
@gatlin
gatlin / typeops.lhs
Created July 25, 2014 16:35
Type operators
Type operators!
===
Turns out a GHC language extension makes a lot of cool things possible that I
think you hinted at:
> {-# LANGUAGE TypeOperators #-}
> data a + b = Inl a | Inr b deriving Show
@gatlin
gatlin / id.js
Created August 8, 2014 22:13
Example of using closures in JavaScript to create a protected generator
var getId = function() {
var _id = 0;
return function() {
return _id++;
}
}()
// usage
console.log(getId());
/***
* Fun with closures!
*
* Run this with node.js:
*
* $> node closurewanking.js
*/
var http = require('http');