Created
April 15, 2020 01:12
-
-
Save oomusou/922e5de15b884af52bba1b6eeadf35dc to your computer and use it in GitHub Desktop.
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
import { pipe, propEq } from 'ramda' | |
import { prop } from 'crocks' | |
class MyMaybe { | |
#value = null | |
#isNothing = true | |
constructor(value) { | |
if (!value) | |
this.#isNothing = true | |
else { | |
this.#isNothing = false | |
this.#value = value | |
} | |
} | |
static Just(value) { | |
return new MyMaybe(value) | |
} | |
static Nothing() { | |
return new MyMaybe() | |
} | |
map(f) { | |
return this.#isNothing ? | |
MyMaybe.Nothing() : | |
MyMaybe.Just(f(this.#value)) | |
} | |
chain(f) { | |
return this.#isNothing ? | |
MyMaybe.Nothing() : | |
f(this.#value) | |
} | |
get(f) { | |
this.#isNothing ? | |
f('Nothing') : | |
f(this.#value) | |
} | |
} | |
let find = f => a => { | |
let result = a.find(f) | |
return (result === undefined) ? MyMaybe.Nothing() : MyMaybe.Just(result) | |
} | |
let data = [ | |
{ title: 'FP in JavaScript', price: 100 }, | |
{ title: 'RxJS in Action', price: 200 }, | |
{ title: 'Speaking JavaScript', price: 300 } | |
] | |
let result = find(x => x.title === 'Speaking JavaScript')(data) | |
result.get(x => console.log(x)) | |
let result_ = find(x => x.title === 'RxJS')(data) | |
result_.get(x => console.log(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment