Last active
August 29, 2015 14:17
-
-
Save dmh2000/d8f78998257f042f4f5d to your computer and use it in GitHub Desktop.
Simple example of pointfree-fantasy composition and mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
"use strict"; | |
// tested with io.js 1.5.1 which supports ES6 'const' | |
// there are no mutable variables in the entire gist | |
const assert = require('assert'); | |
const util = require('util'); | |
const P = require('pointfree-fantasy'); | |
const map = P.map; | |
const compose = P.compose; | |
const Maybe = require('pointfree-fantasy/instances/maybe'); | |
const Identity = require('pointfree-fantasy/instances/identity').Identity; | |
const inspect = function(x) {return util.inspect(x,{showHidden:true,depth:null});}; | |
console.log("======================================"); | |
console.log("CATEGORIES"); | |
console.log("======================================"); | |
// ordinary functions that do something | |
const f = function(x) {return x * 2;}; | |
const g = function(x) {return x / 3;}; | |
const h = function(x) {return x + 1;}; | |
// ordinary identity function | |
const id = function(x) {return x;}; | |
console.log("Ordinary Functions:"); | |
// ========================================== | |
// associativity | |
// ========================================== | |
// X = (f . g) . h | |
const a1 = compose(h,compose(g,f)); | |
const a2 = compose(compose(h,g),f); | |
assert.equal(a1(1),a2(1),"associativity"); | |
console.log(' associativity OK'); | |
// ========================================== | |
// identity | |
// ========================================== | |
// f.id === id.f | |
const i1 = compose(f,id); | |
const i2 = compose(id,f); | |
assert.equal(i1(1),i2(1),"identity"); | |
console.log(' identity OK'); | |
console.log("======================================"); | |
console.log("create Functors"); | |
// ========================================== | |
// promote ordinary functions to Functors | |
// ========================================== | |
const mf = map(f); | |
const mg = map(g); | |
const mh = map(h); | |
// ========================================== | |
// create compositions for associativity | |
// ========================================== | |
const m1 = compose(mh,compose(mg,mf)); | |
const m2 = compose(compose(mh,mg),mf); | |
// ========================================== | |
// create compositions for identity | |
// ========================================== | |
// map(f) . id == id . map(f) | |
const mid = map(id); | |
const mi1 = compose(mf,mid); | |
const mi2 = compose(mid,mf); | |
// ========================================== | |
// create compositions for distributivity | |
// ========================================== | |
const m5 = compose(mf,mg); | |
const m6 = map(compose(f,g)); | |
console.log("======================================"); | |
// ========================================== | |
// Functors | |
// ========================================== | |
console.log("Functors:"); | |
const one = Identity(1); | |
// =============================================== | |
// associativity | |
// =============================================== | |
assert.deepEqual(m1(one),m2(one),"associativity"); | |
console.log(' associativity OK'); | |
// ========================================== | |
// identity | |
// ========================================== | |
assert.deepEqual(mi1(one),mi2(one),"identity"); | |
console.log(' identity OK'); | |
// ========================================== | |
// map(f).map(g) === map(f.g) | |
// ========================================== | |
assert.deepEqual(m5(one),m6(one),"distributive"); | |
console.log(' distributivity OK'); | |
console.log("======================================"); | |
// ========================================== | |
// Arrays | |
// ========================================== | |
console.log("Arrays:"); | |
const A = [1,2,3]; | |
// ========================================== | |
// associativity | |
// ========================================== | |
assert.deepEqual(m1(A),m2(A),"associativity"); | |
console.log(' associativity OK'); | |
// ========================================== | |
// identity | |
// ========================================== | |
// map(f) . id == id . map(f) | |
assert.deepEqual(mi1(A),mi2(A),"identity"); | |
console.log(' identity OK'); | |
// ========================================== | |
// map(f).map(g) === map(f.g) | |
// ========================================== | |
assert.deepEqual(m5(A),m6(A),"distributive"); | |
console.log(' distributivity OK'); | |
console.log("======================================"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment