Skip to content

Instantly share code, notes, and snippets.

@i-am-tom
i-am-tom / Prototype.php
Last active October 1, 2016 22:33
Simple example of prototype OOP in PHP.
<?php
/**
* The prototype class.
*/
class Object
{
/**
* The object's data.
*/
@i-am-tom
i-am-tom / Y.php
Last active October 1, 2016 22:34
The Y (and U) combinator(s), with an example, implemented in PHP.
<?php
function U ($f) {
return $f ($f);
}
function Y ($f) {
return U (function ($x) use ($f) {
return $f (function ($y) use ($x) {
return U ($x) ($y);
@i-am-tom
i-am-tom / flatMapGraph.js
Last active October 5, 2016 22:05
Using flatMap to traverse graphs.
const Graph = {
A: ['B', 'C'],
B: ['D'],
C: ['A', 'B'],
D: ['E']
}
const nextSteps = node => Graph[node]
// Forgive me, padre
@i-am-tom
i-am-tom / mergies.js
Last active October 16, 2016 18:40
Merging records (with history) using an array scan.
// reduce with the acc at each stage.
const scan = (f, init, xs) => {
let acc = init
return [init, ... xs.map(
x => acc = f(acc, x)
)]
}
// Some methods...
module Main where
import Prelude
import Effect.Console (logShow)
import TryPureScript (render, withConsole)
-- Declare the recursive type.
data Peano = Z | S Peano
-- This allows us to print the type.
@i-am-tom
i-am-tom / RFC.php
Created November 11, 2016 15:24
A little tale to help the arrow syntax RFC along.
<?php
// == Arrow functions - A Love Story ==
// Arrow functions are wonderful for a variety of reasons, and below are some
// of my favourites. Hopefully, these will provide some sort of support for
// the RFC. Note that none of these have been tested, though...
@i-am-tom
i-am-tom / Main.elm
Created December 11, 2016 12:36
The Orrery
-- ## An Elm-entary visualisation
-- by _Tom Harding_
module Main exposing (..)
-- This project uses a few dependencies. To anyone who's written any
-- amount of Elm before, the only stranger is `AnimationFrame`, from
-- the `elm-lang/animation-frame` package. This lets us subscribe to
-- the browser's RAF API. Everything else should be fairly obvious:
-- `Html` / `Svg` for our view, `Http` / `Json.Decode` for the AJAX
@i-am-tom
i-am-tom / semigroup.js
Created March 13, 2017 21:10
Fantas, Eel, and Specification
const { tagged } = require('daggy')
const First = tagged('val')
First.prototype.concat = function (that) {
return this
}
const Min = tagged('val')
@i-am-tom
i-am-tom / Main.purs
Created March 29, 2017 06:41
Wolfram's Rule 30 cellular automaton in PureScript.
module Main where
import Prelude
import Control.Comonad (class Comonad, class Extend, extend, extract)
import Control.Monad.Eff.Console (log)
import Data.Array ((..), (!!), cons, length, replicate, zipWith)
import Data.Function (on)
import Data.Int (fromStringAs, binary, toNumber, floor)
import Data.Int.Bits ((.&.))
@i-am-tom
i-am-tom / db-alt.js
Last active February 20, 2020 03:01
Database failover modelled with the `alt` typeclass.
const Task = require('data.task')
Task.prototype.alt = function (that) {
return new Task((rej, res) =>
this.fork(_ => that.fork(rej, res), res))
}
const hosts = [
[ 'db1.mysite.com', 'user', 'password' ],
[ 'db2.mysite.com', 'user', 'password' ],