Created
September 15, 2015 03:22
-
-
Save ChetHarrison/a1a6de9a5af347a063cf to your computer and use it in GitHub Desktop.
functional fantasy examples
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
'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