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...
@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 / Tape.purs
Last active July 6, 2017 15:10
A non-empty list with a read head.
module Data.Tape where
import Prelude
import Control.Comonad (class Comonad, extract)
import Control.Extend (class Extend, extend)
import Data.List (List(Cons, Nil), uncons)
import Data.Foldable
( class Foldable
, foldl
@i-am-tom
i-am-tom / FreddyVsJson.hs
Created August 21, 2017 18:59
Cofree vs Fix for a JSON schema type.
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE OverloadedLists #-}
module FreddyVsJson where
import Control.Comonad.Cofree
import Data.Fix
import Data.Map
-- Here's our JSON "DSL". With this, we can represent any
@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 / chain.js
Created May 15, 2017 19:57
The example code from the `Chain` post!
const Option = require('fantasy-options')
const Either = require('fantasy-eithers')
const Task = require('data.task')
const { tagged } = require('daggy')
const { Some, None } = Option
const { Left, Right } = Either
const Pair = tagged('_1', '_2')
@i-am-tom
i-am-tom / chainRec.js
Created May 30, 2017 16:42
Code from the fantas-eel post on ChainRec.
const daggy = require('daggy')
const { Loop, Done } =
daggy.taggedSum({ Loop: ['b']
, Done: ['a'] })
Array.empty = () => []
const Pair = T => {
const Pair_ = daggy.tagged('_1', '_2')