fp-ts-std
Version:
The missing pseudo-standard library for fp-ts.
42 lines (41 loc) • 1.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.until = exports.pass = exports.traverseArray_ = exports.sequenceArray_ = exports.memoize = exports.unless = exports.when = exports.execute = exports.whenInvocationCount = exports.once = void 0;
const IO = require("fp-ts/IO");
const function_1 = require("fp-ts/function");
const Applicative_1 = require("./Applicative");
const Monad_1 = require("./Monad");
const once = (f) => {
const uncalled = Symbol();
let val = uncalled;
return x => {
if (val === uncalled)
val = f(x);
return IO.of(val);
};
};
exports.once = once;
const whenInvocationCount = (p) => f => {
let n = 0;
return () => {
n++;
if (p(n))
f();
};
};
exports.whenInvocationCount = whenInvocationCount;
const execute = (x) => x();
exports.execute = execute;
exports.when = (0, Applicative_1.when)(IO.Applicative);
exports.unless = (0, Applicative_1.unless)(IO.Applicative);
const memoize = (f) => {
const empty = Symbol();
let res = empty;
return () => (res === empty ? (res = f()) : res);
};
exports.memoize = memoize;
exports.sequenceArray_ = (0, function_1.flow)(IO.sequenceArray, IO.map(function_1.constVoid));
const traverseArray_ = f => (0, function_1.flow)(IO.traverseArray(f), IO.map(function_1.constVoid));
exports.traverseArray_ = traverseArray_;
exports.pass = (0, Applicative_1.pass)(IO.Applicative);
exports.until = (0, Monad_1.until)(IO.Monad);
;