Skip to content

Instantly share code, notes, and snippets.

@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
@gatlin
gatlin / bool.lhs
Last active August 29, 2015 14:06
What is a Church encoded data type?
===
I wrote a [post][listpost] about a really cool concept in computer science and
programming languages, but I fear that I may have gotten ahead of myself. It
turns out, Haskell is not so well known and notation was proving to be a
problem.
That and the concept is kind of a mind-bender.
@gatlin
gatlin / coro_switch.c
Last active August 29, 2015 14:07
Simple, toy coroutine mechanism in C
/*
* Simple coroutine mechanism in C
*
* Inspired by / stolen from:
*
* http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
*
* I found this very educational and wanted to publish a simple demonstration.
*
* Usage:

Recursion

For more information, see also [this enlightening post on recursion][fuckyou]

It's fucking weird. It's also the crucial property of a language that makes it Turing-complete (that is, powerful enough to compute anything which can be computed). [^1] It's often baked into a language and while it works it makes no sense intuitively.

@gatlin
gatlin / qr.pl
Created December 18, 2014 20:36
I made a command line thing to let me quickly generate and display QR codes
#!/usr/bin/env perl
use v5.20;
use Gtk3 -init;
use Image::PNG::QRCode qw(qrpng);
use File::Temp qw(tempfile);
my $sample_data = $ARGV[0] // "gatlin is so handsome";
my $scale = 9;
@gatlin
gatlin / hellobike.hs
Last active August 29, 2015 14:11
Someone stole my bike! So I'm casting a wide net on Craigslist
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
import Prelude hiding (map)
import Pipes
import Pipes.Prelude (stdoutLn, map)
import Pipes.Interleave
import Network.Wreq
import qualified Data.ByteString.Lazy.Char8 as C
import qualified Data.ByteString.Char8 as SC
@gatlin
gatlin / Main.hs
Last active August 29, 2015 14:12
A (partial) re-implementation of the `memo` application available at http://getmemo.org. Excuse to learn about applicative parser combinators. Work in progress.
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Data.Char (toUpper)
import Prelude hiding (map, take, takeWhile, print, filter)
import Pipes
import Pipes.Prelude (map, fromHandle, toHandle, stdoutLn, filter, take, fold)
import Pipes.Lift
import System.Directory (getHomeDirectory, doesFileExist)
@gatlin
gatlin / applisp.hs
Created January 5, 2015 23:49
A simple parser combinator library and an even simpler lisp parser created with it.
{-# LANGUAGE DeriveFunctor #-}
import Control.Applicative hiding (many,optional)
import Data.Char
import Control.Monad.Free
newtype Parser s t = P {
runParser :: [s] -> [(t, [s])]
}