Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "fp-practice",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"start": "nodemon src/index.js localhost 8080"
},
"dependencies": {
"lodash": "4.17.19",
"ramda": "0.26.1"
},
"devDependencies": {
"nodemon": "1.18.4"
},
"keywords": []
}
54 changes: 54 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const R = require("ramda");
const { log } = console;
const { compose, map, equals } = R;

class Functor {
static of(value) {
return new Functor(value);
}
constructor(value) {
this.value = value;
}
fmap(f) {
return new Functor(f(this.value));
}
}

const gt0 = num => num > 0;
const fNum10 = new Functor(10);
const fNum10AfterGt = fNum10.fmap(gt0);
const fNum10AfterGtR = R.map(gt0, fNum10);

// Functor {value:10}
// Functor {value: true}
// {value:true}
log(fNum10, fNum10AfterGt, fNum10AfterGtR);

log(R.map(gt0, [10])); // [true]

log(new Functor(10).fmap(gt0));

//
const id = x => x;
const f = price => price * 1.05 + 10;
const g = total => total + 100;
const fg = compose(
g,
f
);

const f90 = Functor.of(90);
// Identity
log(equals(f90.fmap(id), Functor.of(id(90))));
// Compose
log(f90.fmap(f).fmap(g));
log(f90.fmap(fg));
log(
map(
compose(
g,
f
),
f90
)
);