Skip to content

Instantly share code, notes, and snippets.

@gatlin
gatlin / keepassalong.pl
Created May 22, 2014 14:31
A script to convert a CSV exported from KeePass(X) to a .kdb format.
#!/usr/bin/env perl
use v5.14;
use strict;
use warnings;
use File::KeePass;
use Text::CSV;
use Data::Dump qw(pp);
use Term::ReadPassword;
@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 / gatsby.hs
Last active September 1, 2017 19:25
Hunter S Thompson once typed out the entirety of "The Great Gatsby" so that he could feel what it was like to write the great American novel. In this spirit I am reimplementing core concepts in functional programming and trying to work out the harder puzzles myself.
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ExistentialQuantification #-}
@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');
@gatlin
gatlin / slurp.rkt
Last active August 29, 2015 14:06
I have a lot of saved posts on Reddit, but I also compulsively save pug-related posts. So I wrote this script to download my saved posts, filter out the pug-related ones, and save them to a local database.
#lang racket
(require db)
(require "utils.rkt")
; open a connection to our database
(define (call-with-conn db proc)
(let ([conn (sqlite3-connect #:database db
#:mode 'read/write)])
(proc conn)))
@gatlin
gatlin / prototype.rkt
Last active August 29, 2015 14:06
Simple almost-prototypal object system in pure Racket.
#lang racket
;; Wherein we define a prototypal object system
; A λ-function which abstracts the basic operations one can perform on an "object":
; - getting a property
; - setting a property
; - yielding its state
; - replacing its state
(define (make-object state)
@gatlin
gatlin / eqdef.rkt
Created September 12, 2014 17:36
Simple macro for Typed Racket which makes definitions look nicer to me
#lang typed/racket
(define-syntax (= stx)
(syntax-case stx ()
[(= sym expr)
#'(define sym expr)]
[(= sym (arg ...) expr)
#'(define (sym arg ...) expr)]))
;; example usage