either function monad in javascript
class Monad {
// pure :: a -> M a
pure = () => { throw "pure method needs to be implemented" }
// flatMap :: # M a -> (a -> M b) -> M b
flatMap = (x) => { throw "flatMap method needs to be implemented" }
// map :: # M a -> (a -> b) -> M b
map = f => this.flatMap(x => new this.pure(f(x)))
}
export class Option extends Monad {
// pure :: a -> Option a
pure = (value) => {
if ((value === null) || (value === undefined)) {
return none;
}
return new Some(value)
}
// flatMap :: # Option a -> (a -> Option b) -> Option b
flatMap = f =>
this.constructor.name === 'None' ?
none :
f(this.value)
// equals :: # M a -> M a -> boolean
equals = (x) => this.toString() === x.toString()
}
class None extends Option {
toString() {
return 'None';
}
}
// Cached None class value
export const none = new None()
Option.pure = none.pure
export class Some extends Option {
constructor(value) {
super();
this.value = value;
}
toString() {
return `Some(${this.value})`
}
}
Also in JavaScript:
- how to interrupt scroll with jquery
- javascript array column
- react dark mode npm
- import slider material ui
- java serialize object to json
- set a value in session using javascript
- jquery siblings
- how to validate file extension in javascript
- change class javascript
- style display block js
- find longest word in string javascript
- how to make a modal stay center of screen
- js settimeout mdn
- get the current date time in javascript in 12 hour format
- js get color of pixel
- client side redirect javascript
- how to add keyframe in emotion stled
- codeigniter ajax crud using datatables
- relative width and height image react native
- type of variable js
- python http request post json example
- javascript check if is nan
- currentTime(); javascript
- jquery if checkbox is checked