Skip to content

Instantly share code, notes, and snippets.

@ChetHarrison
Created September 15, 2015 03:22
Show Gist options
  • Save ChetHarrison/a1a6de9a5af347a063cf to your computer and use it in GitHub Desktop.
Save ChetHarrison/a1a6de9a5af347a063cf to your computer and use it in GitHub Desktop.
functional fantasy examples
'use strict'
import Id from 'id'
const func = x => x + 2;
const extFunc = x => x.extract( ) * 10;
const intId = Id.of( 1 );
const arrayId = Id.of( [ 1, 2 ] );
const stringId = Id.of( 'stuff' );
const functionId = Id.of( func );
const equals = intId
.equals( intId ); // true
// requires arrays
const concat = arrayId
.concat( arrayId )
.extract( ); // [1, 2, 1, 2]
const reduce = intId
.reduce( ( a, b ) => a + b, 1 ); // 2
const map = stringId
.map( element => element + 'foo' )
.extract( ); // stufffoo
const ap = functionId
.ap( intId )
.extract( ); // 3
const traverse = arrayId
.traverse( x => x.map( func ) ); // [ Id, Id ]
const chain = intId
.chain( func ); // 3
const extend = intId
.extend( extFunc )
.extract( ); // 10
// ops
const examples = [
equals,
concat,
reduce,
map,
ap,
traverse,
chain,
extend
];
examples.map( example => console.log( example ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment