Skip to content

Instantly share code, notes, and snippets.

@i-am-tom
i-am-tom / Prototype.php
Last active Oct 1, 2016
Simple example of prototype OOP in PHP.
View Prototype.php
<?php
/**
* The prototype class.
*/
class Object
{
/**
* The object's data.
*/
@i-am-tom
i-am-tom / Y.php
Last active Oct 1, 2016
The Y (and U) combinator(s), with an example, implemented in PHP.
View Y.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 Oct 5, 2016
Using flatMap to traverse graphs.
View flatMapGraph.js
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 Oct 16, 2016
Merging records (with history) using an array scan.
View mergies.js
// 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 Nov 11, 2016
A little tale to help the arrow syntax RFC along.
View RFC.php
<?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 Jul 6, 2017
A non-empty list with a read head.
View Tape.purs
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 Aug 21, 2017
Cofree vs Fix for a JSON schema type.
View FreddyVsJson.hs
{-# 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 Mar 13, 2017
Fantas, Eel, and Specification
View semigroup.js
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
The example code from the `Chain` post!
View chain.js
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
Code from the fantas-eel post on ChainRec.
View chainRec.js
const daggy = require('daggy')
const { Loop, Done } =
daggy.taggedSum({ Loop: ['b']
, Done: ['a'] })
Array.empty = () => []
const Pair = T => {
const Pair_ = daggy.tagged('_1', '_2')