about summary refs log blame commit diff stats
path: root/311decimal-int.subx
blob: b736ba03d3e9db275e272618fc5c9c96d09f9f37 (plain) (tree)
1
2
3
4
5
pan>.traverse(of, identity); } traverse(of, fn) { return fn(this.$value).map(Identity.of); } } class IO { constructor(fn) { this.unsafePerformIO = fn; } [util.inspect.custom]() { return 'IO(?)'; } // ----- Pointed IO static of(x) { return new IO(() => x); } // ----- Functor IO map(fn) { return new IO(compose(fn, this.unsafePerformIO)); } // ----- Applicative IO ap(f) { return this.chain(fn => f.map(fn)); } // ----- Monad IO chain(fn) { return this.map(fn).join(); } join() { return new IO(() => this.unsafePerformIO().unsafePerformIO()); } } class List { constructor(xs) { this.$value = xs; } [util.inspect.custom]() { return `List(${inspect(this.$value)})`; } concat(x) { return new List(this.$value.concat(x)); } // ----- Pointed List static of(x) { return new List([x]); } // ----- Functor List map(fn) { return new List(this.$value.map(fn)); } // ----- Traversable List sequence(of) { return this.traverse(of, identity); } traverse(of, fn) { return this.$value.reduce( (f, a) => fn(a).map(b => bs => bs.concat(b)).ap(f), of(new List([])), ); } } class Map { constructor(x) { this.$value = x; } [util.inspect.custom]() { return `Map(${inspect(this.$value)})`; } insert(k, v) { const singleton = {}; singleton[k] = v; return Map.of(Object.assign({}, this.$value, singleton)); } reduceWithKeys(fn, zero) { return Object.keys(this.$value) .reduce((acc, k) => fn(acc, this.$value[k], k), zero); } // ----- Functor (Map a) map(fn) { return this.reduceWithKeys( (m, v, k) => m.insert(k, fn(v)), new Map({}), ); } // ----- Traversable (Map a) sequence(of) { return this.traverse(of, identity); } traverse(of, fn) { return this.reduceWithKeys( (f, a, k) => fn(a).map(b => m => m.insert(k, b)).ap(f), of(new Map({})), ); } } class Maybe { get isNothing() { return this.$value === null || this.$value === undefined; } get isJust() { return !this.isNothing; } constructor(x) { this.$value = x; } [util.inspect.custom]() { return this.isNothing ? 'Nothing' : `Just(${inspect(this.$value)})`; } // ----- Pointed Maybe static of(x) { return new Maybe(x); } // ----- Functor Maybe map(fn) { return this.isNothing ? this : Maybe.of(fn(this.$value)); } // ----- Applicative Maybe ap(f) { return this.isNothing ? this : f.map(this.$value); } // ----- Monad Maybe chain(fn) { return this.map(fn).join(); } join() { return this.isNothing ? this : this.$value; } // ----- Traversable Maybe sequence(of) { return this.traverse(of, identity); } traverse(of, fn) { return this.isNothing ? of(this) : fn(this.$value).map(Maybe.of); } } class Task { constructor(fork) { this.fork = fork; } [util.inspect.custom]() { return 'Task(?)'; } static rejected(x) { return new Task((reject, _) => reject(x)); } // ----- Pointed (Task a) static of(x) { return new Task((_, resolve) => resolve(x)); } // ----- Functor (Task a) map(fn) { return new Task((reject, resolve) => this.fork(reject, compose(resolve, fn))); } // ----- Applicative (Task a) ap(f) { return this.chain(fn => f.map(fn)); } // ----- Monad (Task a) chain(fn) { return new Task((reject, resolve) => this.fork(reject, x => fn(x).fork(reject, resolve))); } join() { return this.chain(identity); } }