@ebflat9/fp
Advanced tools
Comparing version 1.0.12 to 1.0.13
64
index.js
@@ -1,2 +0,3 @@ | ||
import { Maybe, Just, Nothing, Result, Success, Failure, Try, TryAsync } from './maybe.js' | ||
import { Maybe, Result, Try, TryAsync, IO, IOAsync } from './maybe.js' | ||
export { Maybe, Result, Try, TryAsync, IO, IOAsync } | ||
/* | ||
@@ -125,8 +126,6 @@ * My lil functional programming collection | ||
instance[name].apply(instance, args) | ||
export const bound = | ||
(name, ...args) => | ||
instance => | ||
args === [] | ||
? instance[name].bind(instance) | ||
: Function.prototype.bind.apply(instance[name], [instance].concat(args)) | ||
export const bound = (name, ...args) => | ||
args === [] | ||
? instance => instance[name].bind(instance) | ||
: instance => Function.prototype.bind.apply(instance[name], [instance].concat(args)) | ||
export const setPropM = curry((name, value, a) => | ||
@@ -182,2 +181,7 @@ a && name in a ? ((a[name] = value), a) : a | ||
export const pipeAsync = (...fns) => fns.reduceRight(composeAsync2) | ||
export const mapAsync = async (f, a) => await Promise.all(a.map(f)) | ||
export const reduceAsync = async (f, init, a) => | ||
await a.reduce((p, val) => p.then(() => f(val)), Promise.resolve(init)) | ||
export const filterAsync = async (f, a) => | ||
await mapAsync(f, a).then(bools => a.filter((_, i) => Boolean(bools[i]))) | ||
@@ -207,4 +211,5 @@ // flat | ||
export const last = a => a[a.length - 1] | ||
export const every = curry((f, M) => M.every(f)) | ||
export const some = curry((f, M) => M.some(f)) | ||
export const every = curry((f, arr) => arr.every(f)) | ||
export const some = curry((f, arr) => arr.some(f)) | ||
export const find = curry((f, arr) => arr.find(f)) | ||
export const sum = (...args) => args.reduce((x, y) => x + y, 0) | ||
@@ -236,2 +241,4 @@ export const average = ns => sum(...ns) / ns.length | ||
export const toString = String | ||
export const prepend = curry((s1, s2) => `${s1}${s2}`) | ||
export const append = curry((s1, s2) => `${s2}${s1}`) | ||
@@ -634,39 +641,2 @@ export const tryCatch = curry((f, g) => { | ||
// Lenses | ||
class Constant { | ||
#value | ||
constructor(v) { | ||
this.#value = Maybe.of(v) | ||
this.map = () => this | ||
} | ||
get value() { | ||
return this.#value | ||
} | ||
} | ||
class Variable { | ||
#value | ||
constructor(v) { | ||
this.#value = Maybe.of(v) | ||
this.map = fn => new Variable(fn(v)) | ||
} | ||
get value() { | ||
return this.#value | ||
} | ||
} | ||
export const lens = (getter, setter) => fn => obj => | ||
fn(getter(obj)).map(value => setter(value, obj)) | ||
export const view = curry((lensAttr, obj) => lensAttr(x => new Constant(x))(obj).value) | ||
export const set = curry( | ||
(lensAttr, newVal, obj) => lensAttr(() => new Variable(newVal))(obj).value | ||
) | ||
export const over = curry( | ||
(lensAttr, mapfn, obj) => lensAttr(x => new Variable(mapfn(x)))(obj).value | ||
) | ||
export const lensProp = p => lens(prop(p), setProp(p)) | ||
// Iterables | ||
@@ -776,3 +746,1 @@ export function* mapWith(fn, iterable) { | ||
} | ||
export { Maybe, Just, Nothing, Try, TryAsync, Result, Failure, Success } |
83
maybe.js
@@ -0,1 +1,2 @@ | ||
import { compose, composeAsync } from './index.js' | ||
// Maybe | ||
@@ -43,6 +44,6 @@ function throwError(error) { | ||
} | ||
fold(fn = identity) { | ||
fold(fn = x => x) { | ||
return fn(this.value) | ||
} | ||
filter(fn = identity) { | ||
filter(fn = x => x) { | ||
return fn(this.value) ? new Just(a) : new Nothing() | ||
@@ -231,1 +232,79 @@ } | ||
} | ||
export class IO { | ||
#unsafePerformIO; | ||
[Symbol.toStringTag] = 'IO' | ||
constructor(fn) { | ||
this.#unsafePerformIO = fn | ||
} | ||
getOrElse(defaultValue) { | ||
return this.#unsafePerformIO() ?? defaultValue | ||
} | ||
getOrElseThrow(error) { | ||
return this.#unsafePerformIO() ?? throwError(error) | ||
} | ||
get unsafePerformIO() { | ||
return this.#unsafePerformIO | ||
} | ||
fold(fn = x => x) { | ||
return fn(this.#unsafePerformIO) | ||
} | ||
map(fn) { | ||
return IO.of(compose(fn, this.#unsafePerformIO)) | ||
} | ||
flatMap(fn) { | ||
return IO.of(compose(fn, this.#unsafePerformIO)).merge() | ||
} | ||
merge() { | ||
return this.#unsafePerformIO | ||
} | ||
toString() { | ||
return `IO#(${this.#unsafePerformIO.name})` | ||
} | ||
toJSON() { | ||
return { type: 'IO', value: this.#unsafePerformIO } | ||
} | ||
static of(fn) { | ||
return new IO(fn) | ||
} | ||
} | ||
export class IOAsync { | ||
#unsafePerformIO; | ||
[Symbol.toString] = 'IOAsync' | ||
constructor(fn) { | ||
this.#unsafePerformIO = fn | ||
} | ||
async getOrElse(defaultValue) { | ||
return (await this.#unsafePerformIO()) ?? defaultValue | ||
} | ||
async getOrElseThrow(error) { | ||
return (await this.#unsafePerformIO()) ?? throwError(error) | ||
} | ||
async unsafePerformIO() { | ||
return await this.#unsafePerformIO() | ||
} | ||
async fold(fn = async x => await x) { | ||
return await fn(this.#unsafePerformIO) | ||
} | ||
async map(fn) { | ||
return IO.of(composeAsync(fn, this.unsafePerformIO)) | ||
} | ||
async flatMap(fn) { | ||
return IO.of(composeAsync(fn, this.unsafePerformIO)).merge() | ||
} | ||
merge() { | ||
return this.#unsafePerformIO | ||
} | ||
toString() { | ||
return `IOAsync#(${this.#unsafePerformIO.name})` | ||
} | ||
toJSON() { | ||
return { type: 'IOAsync', value: this.#unsafePerformIO } | ||
} | ||
static of(fn) { | ||
return new IOAsync(fn) | ||
} | ||
} |
{ | ||
"name": "@ebflat9/fp", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"description": "my fp utils", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
41020
10
1471