Skip to content

Instantly share code, notes, and snippets.

@gatlin
gatlin / output.txt
Last active October 26, 2016 06:32
applicative parser built with Tubes
Code: (+ 5 5)
Free (AApp (Free (ASymbol "+")) [Free (ANumber 5),Free (ANumber 5)])
---
Code: (lambda (x) (* x x))
Free (ALambda [Free (ASymbol "x")] (Free (AApp (Free (ASymbol "*")) [Free (ASymbol "x"),Free (ASymbol "x")])))
---
Code: ((\ (x) (* x x)) 5 (+ 10 2))
Free (AApp (Free (ALambda [Free (ASymbol "x")] (Free (AApp (Free (ASymbol "*")) [Free (ASymbol "x"),Free (ASymbol "x")])))) [Free (ANumber 5),Free (AApp (Free (ASymbol "+")) [Free (ANumber 10),Free (ANumber 2)])])
---
Code: '(1 2 3)
@gatlin
gatlin / pid.lhs
Created August 17, 2016 20:42
Streaming PID controller in Haskell
PID controller in Haskell
===
A major project I want to embark on at some point in the future is making a
quadrotor. I've made one before but I was at the mercy of a lot of off-the-shelf
software that I'm not altogether sure was entirely correct, either.
So I want to eventually program a quadrotor (or similarly awesome robot thing)
and I would really enjoy doing it in Haskell. It has a low footprint and is
already used in other real time settings.

Bike Math!

Note: For the sake of clarity I'll probably write things you already know. It's just as much to eliminate communication errors as it is for me to keep track of what the fuck I'm even talking about. It does not imply any kind of judgment of what I assume about you :)

Here's the math behind gears and how they are used to efficiently rotate wheels. The same principles underlie how cars work but the mechanisms are

@gatlin
gatlin / 1-js-lenses.md
Last active June 5, 2025 19:20
Simple implementation of lenses in JavaScript

What is a lens?

Note: you can copy the file 2-lenses.js below over to repl.it and play along from home!

A lens in programming, much like a lens in the real world, allows you to focus in on a small part of a larger whole and then do something with or to

'use strict';
let str = "the quick brown fox jumps over the lazy dog";
let fw = Array.prototype.map
.call(str, (c) => c === ' '
? ' '
: String.fromCodePoint(c.codePointAt(0) + 0xFEE0))
.join('');
console.log(fw);
@gatlin
gatlin / ot.hs
Last active January 24, 2020 17:03
{- |
Operational transformation in Haskell using (co)free (co)monads.
-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
import Data.List (splitAt)
import Control.Monad
@gatlin
gatlin / list_essay_code.js
Created April 8, 2016 23:10
Companion code to my essay on lists (http://niltag.net/essays/lists.html)
'use strict';
/**
* Our little list implementation
*/
///// The foundation
let nil = (f) => (x) => x;
let cons = (h) => (t) => (f) => (x) => f(h)(t(f)(x));
let foldr = (list) => (f) => (x) => list(f)(x);
/**
* ECMAScript module boilerplate
*
* (c) 2016 Gatlin Johnson <[email protected]>
* with significant credit to github user @cwmyers
*
* Follow this template and your module will be compatible with browsers or any
* number of sophisticated ES dependency systems.
*
* INSTRUCTIONS
@gatlin
gatlin / my_prelude.hs
Last active September 19, 2017 23:36
{- * Foundational functions -}
id :: a -> a
id x = x
const :: a -> b -> a
const x y = x
fix :: (a -> a) -> a
fix f = f (fix f)
@gatlin
gatlin / orca.hs
Last active September 19, 2017 23:37
yeah I'm thinking about Orc again
{-
This came from looking at the source for the orc package on hackage.
I wanted to know why it works, and to see if I can come up with an
alternative implementation which does cool things, and possibly provide
a mechanism for orchestrating non-linear tube computations.
-}
-- | Pilfered from the orc package on Hackage.
-- The original is specialized to one MonadIO instance.
-- Also this looks suspiciously like a continuation monad.