Skip to content

Instantly share code, notes, and snippets.

@i-am-tom
i-am-tom / monad.js
Created June 5, 2017 20:19
The Monad example from the Fantasy Land series.
const Promise = require('fantasy-promises')
const daggy = require('daggy')
//- Regular `compose` - old news!
//+ compose :: (b -> c)
//+ -> (a -> b)
//+ -> a -> c
const compose = f => g => x =>
f(g(x))
@i-am-tom
i-am-tom / extend.js
Created June 12, 2017 19:38
All the example code from the Extend article of Fantas, Eel, and Specification.
const daggy = require('daggy')
const { uncurryN } = require('wi-jit')
Array.prototype.empty = () => []
Sum = daggy.tagged('value')
Sum.prototype.concat = function (that) {
return Sum(this.value + that.value)
}
@i-am-tom
i-am-tom / Comonad.js
Created June 19, 2017 20:36
Code for Game of Life from the Comonad article.
const { tagged } = require('daggy')
const Pair = tagged('_1', '_2')
//+ data Store p s = Store (p -> s) p
const Store = tagged('lookup', 'pointer')
Array.prototype.equals = function (that) {
return this.length === that.length
&& this.every((x, i) => x.equals ? x.equals(that[i])
@i-am-tom
i-am-tom / Main.purs
Last active August 24, 2017 19:51
Issues around FRP.Event due to circular dependencies.
module Main where
import Prelude
import Control.Apply (lift2)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Maybe (Maybe(Just, Nothing))
import Data.Tuple (Tuple(Tuple))
import FRP (FRP)
@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 / working.md
Created September 10, 2017 08:51
Lambda Calculus evaluation of `compose flip flip id`
((\f.\g.\x.f(gx))(\f.\x.\y.fyx)(\f.\x.\y.fyx))(\x.x)

Renaming things for clarity...

((\f.\g.\x.f(gx))(\f2.\x2.\y2.f2 y2 x2)(\f3.\x3.\y3.f3 y3 x3))(\x4.x4)
@i-am-tom
i-am-tom / RowToTuple.hs
Last active September 12, 2017 21:34
Discarding row labels to reduce records to "HLists" (nested tuple chains)
module Main where
import Prelude (Unit, ($), discard)
import Type.Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Data.Record (get)
import Data.Tuple (Tuple)
import Data.Tuple.Nested ((/\))
@i-am-tom
i-am-tom / Main.purs
Created December 14, 2017 21:59
Generating unique identifiers with a purely functional approach.
module Main where
-- Often in applications, we want to generate unique IDs. In imperative
-- programming, we might be tempted to define, albeit with obfuscation,
-- a global integer that we increment every time we want a new value.
-- In this gist, we'll look at how to achieve the same thing using the
-- state type to keep things good and pure.
import Control.Apply (lift2)
import Control.Monad.Eff (Eff)
@i-am-tom
i-am-tom / Base.Nat.hs
Last active January 8, 2018 13:09
Primitive recursion schemes and combinators specialised to Nat.
-- Folding
project :: Nat -> Maybe Nat
cata :: (Maybe a -> a) -> Nat -> a
gcata :: Comonad w => Distributive w => (Maybe (w a) -> a) -> Nat -> a
para :: (Maybe (Nat, a) -> a) -> Nat -> a
gpara :: Comonad w => Distributive w => (Maybe (Nat, w a) -> a) -> Nat -> a
@i-am-tom
i-am-tom / fanfic.md
Last active April 14, 2018 22:13
The morning of Gary Burgess, 01/09/17

"For services to the PureScript Community, Gary Burgess!"

You've done it, Gary. Moore, Lineker, Coleman, and now Burgess. All the work was worth it. The halls erupted with praise. Children dressed as Space Ghost, teens with "I only get high on Halogen" t-shirts, a giant banner held aloft with the message, "Tuple @garyb me". Through the noise of the crowds and Phil's uninterpretable Northern accent, he barely managed to hear his theme tu-

BZZP. BZZP.