diff --git a/dist/partial.lenses.cjs.js b/dist/partial.lenses.cjs.js
index 7a35cb15..8a533f9e 100644
--- a/dist/partial.lenses.cjs.js
+++ b/dist/partial.lenses.cjs.js
@@ -194,6 +194,8 @@ var unto = function unto(c) {
};
var unto0 = /*#__PURE__*/unto(0);
+var toTrue = /*#__PURE__*/I.always(true);
+
var notPartial = function complement(x) {
return void 0 !== x ? !x : x;
};
@@ -208,6 +210,8 @@ var expect = function expect(p, f) {
}, f);
};
+var freezeInDev = process.env.NODE_ENV === 'production' ? id : I.freeze;
+
function deepFreeze(x) {
if (I.isArray(x)) {
x.forEach(deepFreeze);
@@ -316,19 +320,32 @@ function selectInArrayLike(xi2v, xs) {
//
-var Select = {
- map: I.sndU,
- of: function of() {},
- ap: function ap(l, r) {
- return void 0 !== l ? l : r;
- }
+function Applicative(map, of, ap) {
+ if (!this) return freezeInDev(new Applicative(map, of, ap));
+ this.map = map;
+ this.of = of;
+ this.ap = ap;
+}
+
+var Monad = /*#__PURE__*/I.inherit(function Monad(map, of, ap, chain) {
+ if (!this) return freezeInDev(new Monad(map, of, ap, chain));
+ Applicative.call(this, map, of, ap);
+ this.chain = chain;
+}, Applicative);
+
+//
+
+var ConstantWith = function ConstantWith(ap, empty) {
+ return Applicative(I.sndU, I.always(empty), ap);
};
-var ConcatOf = function ConcatOf(ap, empty) {
- return { map: I.sndU, ap: ap, of: I.always(empty) };
+var ConstantOf = function ConstantOf(_ref) {
+ var concat = _ref.concat,
+ empty = _ref.empty;
+ return ConstantWith(concat, empty());
};
-var Sum = /*#__PURE__*/ConcatOf(addU, 0);
+var Sum = /*#__PURE__*/ConstantWith(addU, 0);
var mumBy = function mumBy(ord) {
return I.curry(function mumBy(xi2y, t, s) {
@@ -421,12 +438,6 @@ var reqMaybeArray = function reqMaybeArray(msg) {
//
-var reqApplicative = function reqApplicative(name, arg) {
- return function (C) {
- if (!C.of) errorGiven('`' + name + (arg ? '(' + arg + ')' : '') + '` requires an applicative', C, 'Note that you cannot `get` a traversal. Perhaps you wanted to `collect` it?');
- };
-};
-
var reqMonad = function reqMonad(name) {
return function (C) {
if (!C.chain) errorGiven('`' + name + '` requires a monad', C, 'Note that you can only `modify`, `remove`, `set`, and `traverse` a transform.');
@@ -485,17 +496,20 @@ function traversePartialIndex(A, xi2yA, xs, skip) {
//
-var ConstantLog = {
- map: function map(f, _ref) {
- var m = _ref.m,
- p = _ref.p,
- c = _ref.c;
- return { m: '%O <= ' + m, p: [f(p[0]), p], c: c };
- }
-};
-var getLogFn = function getLogFn(x) {
- return { m: '%O', p: [x, consExcept], c: x };
-};
+var SelectLog = /*#__PURE__*/Applicative(function (f, _ref2) {
+ var p = _ref2.p,
+ x = _ref2.x,
+ c = _ref2.c;
+
+ x = f(x);
+ if (!I.isFunction(x)) p = [x, p];
+ return { p: p, x: x, c: c };
+}, function (x) {
+ return { p: [], x: x, c: undefined };
+}, function (l, r) {
+ var v = undefined !== l.c ? l : r;
+ return { p: v.p, x: l.x(r.x), c: v.c };
+});
//
@@ -610,53 +624,30 @@ var modifyAsyncU = function modifyAsyncU(o, f, s) {
return returnAsync(toFunction(o)(s, void 0, IdentityAsync, f));
};
-function makeIx(i) {
- var ix = function ix(s, j) {
- return ix.v = j, s;
- };
- ix.v = i;
- return ix;
-}
-
-function getNestedU(l, s, j, ix) {
- for (var n = l.length, o; j < n; ++j) {
- switch (typeof (o = l[j])) {
- case 'string':
- s = getProp(ix.v = o, s);
- break;
- case 'number':
- s = getIndex(ix.v = o, s);
- break;
- case 'object':
- s = getNestedU(o, s, 0, ix);
- break;
- default:
- s = o(s, ix.v, Constant, ix);
- }
- }return s;
-}
-
-var getU = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : par(0, ef(reqOptic)))(function (l, s) {
+var getAsU = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : par(1, ef(reqOptic)))(function getAs(xi2y, l, s) {
switch (typeof l) {
case 'string':
- return getProp(l, s);
+ return xi2y(getProp(l, s), l);
case 'number':
- return getIndex(l, s);
+ return xi2y(getIndex(l, s), l);
case 'object':
- for (var i = 0, n = l.length, o; i < n; ++i) {
- switch (typeof (o = l[i])) {
- case 'string':
- s = getProp(o, s);
- break;
- case 'number':
- s = getIndex(o, s);
- break;
- default:
- return getNestedU(l, s, i, makeIx(l[i - 1]));
- }
- }return s;
+ {
+ var n = l.length;
+ for (var i = 0, o; i < n; ++i) {
+ switch (typeof (o = l[i])) {
+ case 'string':
+ s = getProp(o, s);
+ break;
+ case 'number':
+ s = getIndex(o, s);
+ break;
+ default:
+ return composed(i, l)(s, l[i - 1], Select, xi2y);
+ }
+ }return xi2y(s, l[n - 1]);
+ }
default:
- return l(s, void 0, Constant, id);
+ return xi2y !== id && l.length !== 4 ? xi2y(l(s, void 0), void 0) : l(s, void 0, Select, xi2y);
}
});
@@ -714,7 +705,7 @@ var getPick = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : res(I.f
var r = void 0;
for (var k in template) {
var t = template[k];
- var v = I.isObject(t) ? getPick(t, x) : getU(t, x);
+ var v = I.isObject(t) ? getPick(t, x) : getAsU(id, t, x);
if (void 0 !== v) {
if (!r) r = {};
r[k] = v;
@@ -1050,10 +1041,9 @@ var orElseU = function orElse(back, prim) {
};
};
-function zeroOp(y, i, C, xi2yC, x) {
- var of = C.of;
- return of ? of(y) : C.map(I.always(y), xi2yC(x, i));
-}
+var zero = function zero(x, _i, C, _xi2yC) {
+ return C.of(x);
+};
//
@@ -1079,13 +1069,6 @@ var pickInAux = function pickInAux(t, k) {
//
-var condOfDefault = /*#__PURE__*/I.always(zeroOp);
-var condOfCase = function condOfCase(p, o, r) {
- return function (y, j) {
- return p(y, j) ? o : r(y, j);
- };
-};
-
// Auxiliary
var seemsArrayLike = function seemsArrayLike(x) {
@@ -1094,28 +1077,18 @@ var seemsArrayLike = function seemsArrayLike(x) {
// Internals
-var Identity = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : I.freeze)({
- map: I.applyU,
- of: id,
- ap: I.applyU,
- chain: I.applyU
-});
+var Identity = /*#__PURE__*/Monad(I.applyU, id, I.applyU, I.applyU);
-var IdentityAsync = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : I.freeze)({
- map: chainAsync,
- ap: function ap(xyP, xP) {
- return chainAsync(function (xP) {
- return chainAsync(function (xyP) {
- return xyP(xP);
- }, xyP);
- }, xP);
- },
- of: id,
- chain: chainAsync
-});
+var IdentityAsync = /*#__PURE__*/Monad(chainAsync, id, function (xyP, xP) {
+ return chainAsync(function (xP) {
+ return chainAsync(function (xyP) {
+ return xyP(xP);
+ }, xyP);
+ }, xP);
+}, chainAsync);
-var Constant = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : I.freeze)({
- map: I.sndU
+var Select = /*#__PURE__*/ConstantWith(function (l, r) {
+ return void 0 !== l ? l : r;
});
var toFunction = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : par(0, ef(reqOptic)))(function toFunction(o) {
@@ -1234,14 +1207,38 @@ var condOf = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : function
};
})(function condOf(of) {
of = toFunction(of);
- var op = condOfDefault;
- var n = arguments.length;
- while (--n) {
- var c = arguments[n];
- op = c.length === 1 ? I.always(toFunction(c[0])) : condOfCase(c[0], toFunction(c[1]), op);
+
+ var n = arguments.length - 1;
+ if (!n) return zero;
+
+ var def = arguments[n];
+ if (def.length === 1) {
+ --n;
+ def = toFunction(def[0]);
+ } else {
+ def = zero;
+ }
+
+ var ps = Array(n);
+ var os = Array(n + 1);
+ for (var i = 0; i < n; ++i) {
+ var c = arguments[i + 1];
+ ps[i] = c[0];
+ os[i] = toFunction(c[1]);
}
- return function condOf(x, i, C, xi2yC) {
- return of(x, i, Constant, op)(x, i, C, xi2yC);
+ os[n] = def;
+
+ return function condOf(x, i, F, xi2yF) {
+ var min = n;
+ of(x, i, Select, function (y, j) {
+ for (var _i3 = 0; _i3 < min; ++_i3) {
+ if (ps[_i3](y, j)) {
+ min = _i3;
+ if (_i3 === 0) return 0;else break;
+ }
+ }
+ });
+ return os[min](x, i, F, xi2yF);
};
});
@@ -1249,19 +1246,6 @@ var ifElse = /*#__PURE__*/I.curry(function ifElse(c, t, e) {
return eitherU(toFunction(t), toFunction(e))(c);
});
-var iftes = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : function (fn$$1) {
- return function iftes(_c, _t) {
- warn(iftes, '`iftes` has been obsoleted. Use `ifElse` or `cond` instead. See CHANGELOG for details.');
- return fn$$1.apply(null, arguments);
- };
-})(function iftes(_c, _t) {
- var n = arguments.length;
- var r = n & 1 ? toFunction(arguments[--n]) : zero;
- while (0 <= (n -= 2)) {
- r = eitherU(toFunction(arguments[n + 1]), r)(arguments[n]);
- }return r;
-});
-
var orElse = /*#__PURE__*/I.curry(orElseU);
// Querying
@@ -1280,16 +1264,12 @@ var choice = function choice() {
return os.reduceRight(orElseU, zero);
};
-var unless = /*#__PURE__*/eitherU(zeroOp, identity);
+var unless = /*#__PURE__*/eitherU(zero, identity);
-var when = /*#__PURE__*/eitherU(identity, zeroOp);
+var when = /*#__PURE__*/eitherU(identity, zero);
var optional = /*#__PURE__*/when(I.isDefined);
-var zero = function zero(x, i, C, xi2yC) {
- return zeroOp(x, i, C, xi2yC);
-};
-
// Indices
var mapIx = function mapIx(ix2j) {
@@ -1321,6 +1301,20 @@ var skipIx = /*#__PURE__*/setName( /*#__PURE__*/tieIx(I.sndU), 'skipIx');
// Debugging
+function getLog(l, s) {
+ var _traverseU = traverseU(SelectLog, function (x) {
+ return { p: [x, consExcept], x: x, c: x };
+ }, l, s),
+ p = _traverseU.p,
+ c = _traverseU.c;
+
+ p = pushTo(p, ['%O']);
+ for (var i = 2; i < p.length; ++i) {
+ p[0] += ' <= %O';
+ }console.log.apply(console, p);
+ return c;
+}
+
function log() {
var show = I.curry(function log(dir, x) {
console.log.apply(console, copyToFrom([], 0, arguments, 0, arguments.length).concat([dir, x]));
@@ -1329,16 +1323,6 @@ function log() {
return isoU(show('get'), show('set'));
}
-var getLog = /*#__PURE__*/I.curry(function getLog(l, s) {
- var _traverseU = traverseU(ConstantLog, getLogFn, l, s),
- m = _traverseU.m,
- p = _traverseU.p,
- c = _traverseU.c;
-
- console.log.apply(console, pushTo(p, [m]));
- return c;
-});
-
// Operations on transforms
var transform = /*#__PURE__*/I.curry(function transform(o, s) {
@@ -1374,14 +1358,14 @@ var assignOp = function assignOp(x) {
};
var modifyOp = function modifyOp(xi2y) {
- return function modifyOp(x, i, C, xi2yC) {
- return zeroOp(x = xi2y(x, i), i, C, xi2yC, x);
+ return function modifyOp(x, i, C, _xi2yC) {
+ return C.of(xi2y(x, i));
};
};
var setOp = function setOp(y) {
- return function setOp(_x, i, C, xi2yC) {
- return zeroOp(y, i, C, xi2yC, y);
+ return function setOp(_x, _i, C, _xi2yC) {
+ return C.of(y);
};
};
@@ -1408,9 +1392,9 @@ function branches() {
// Traversals and combinators
-var elems = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : par(2, ef(reqApplicative('elems'))))(function elems(xs, i, A, xi2yA) {
+function elems(xs, i, A, xi2yA) {
return seemsArrayLike(xs) ? elemsI(xs, i, A, xi2yA) : A.of(xs);
-});
+}
var elemsTotal = function elemsTotal(xs, i, A, xi2yA) {
return seemsArrayLike(xs) ? A === Identity ? mapPartialIndexU(xi2yA, xs, mapPartialIndexU) : A === Select ? selectInArrayLike(xi2yA, xs) : traversePartialIndex(A, xi2yA, xs, traversePartialIndex) : A.of(xs);
@@ -1420,9 +1404,7 @@ var entries = /*#__PURE__*/setName( /*#__PURE__*/toFunction([keyed, elems]), 'en
var keys = /*#__PURE__*/setName( /*#__PURE__*/toFunction([keyed, elems, 0]), 'keys');
-var matches = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : dep(function (re) {
- return re.global ? res(par(2, ef(reqApplicative('matches', re)))) : id;
-}))(function matches(re) {
+function matches(re) {
return function matches(x, _i, C, xi2yC) {
if (I.isString(x)) {
var map = C.map;
@@ -1446,22 +1428,22 @@ var matches = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : dep(fun
}, xi2yC(m[0], reIndex(m)));
}
}
- return zeroOp(x, void 0, C, xi2yC);
+ return C.of(x);
};
-});
+}
-var values = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : par(2, ef(reqApplicative('values'))))( /*#__PURE__*/setName( /*#__PURE__*/branchOr1Level(identity, protoless0), 'values'));
+var values = /*#__PURE__*/setName( /*#__PURE__*/branchOr1Level(identity, protoless0), 'values');
-var children = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : par(2, ef(reqApplicative('children'))))(function children(x, i, C, xi2yC) {
+function children(x, i, C, xi2yC) {
return I.isArray(x) ? elemsI(x, i, C, xi2yC) : I.isObject(x) ? values(x, i, C, xi2yC) : C.of(x);
-});
+}
-var flatten = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : par(2, ef(reqApplicative('flatten'))))(function flatten(x, i, C, xi2yC) {
+function flatten(x, i, C, xi2yC) {
var rec = function rec(x, i) {
return I.isArray(x) ? elemsI(x, i, C, rec) : void 0 !== x ? xi2yC(x, i) : C.of(x);
};
return rec(x, i);
-});
+}
function query() {
var r = [];
@@ -1488,7 +1470,7 @@ var leafs = /*#__PURE__*/satisfying(function (x) {
// Folds over traversals
var all = /*#__PURE__*/I.curry(function all(xi2b, t, s) {
- return !traverseU(Select, function (x, i) {
+ return !getAsU(function (x, i) {
if (!xi2b(x, i)) return true;
}, t, s);
});
@@ -1496,7 +1478,7 @@ var all = /*#__PURE__*/I.curry(function all(xi2b, t, s) {
var and$1 = /*#__PURE__*/all(id);
var any = /*#__PURE__*/I.curry(function any(xi2b, t, s) {
- return !!traverseU(Select, function (x, i) {
+ return !!getAsU(function (x, i) {
if (xi2b(x, i)) return true;
}, t, s);
});
@@ -1512,9 +1494,7 @@ var collectAs = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? I.curry :
var collect = /*#__PURE__*/collectAs(id);
-var concatAs = /*#__PURE__*/mkTraverse(id, function concatAs(m) {
- return ConcatOf(m.concat, m.empty());
-});
+var concatAs = /*#__PURE__*/mkTraverse(id, ConstantOf);
var concat = /*#__PURE__*/concatAs(id);
@@ -1571,16 +1551,24 @@ var forEachWith = /*#__PURE__*/I.curry(function forEachWith(newC, ef$$1, t, s) {
return c;
});
+function get(l, s) {
+ return 1 < arguments.length ? getAsU(id, l, s) : function (s) {
+ return getAsU(id, l, s);
+ };
+}
+
+var getAs = /*#__PURE__*/I.curry(getAsU);
+
var isDefined = /*#__PURE__*/I.curry(function isDefined(t, s) {
- return void 0 !== traverseU(Select, id, t, s);
+ return void 0 !== getAsU(id, t, s);
});
var isEmpty = /*#__PURE__*/I.curry(function isEmpty(t, s) {
- return !traverseU(Select, I.always(true), t, s);
+ return !getAsU(toTrue, t, s);
});
var joinAs = /*#__PURE__*/mkTraverse(toStringPartial, /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : par(0, ef(reqString('`join` and `joinAs` expect a string delimiter'))))(function joinAs(d) {
- return ConcatOf(function (x, y) {
+ return ConstantWith(function (x, y) {
return void 0 !== x ? void 0 !== y ? x + d + y : x : y;
});
}));
@@ -1611,33 +1599,31 @@ var minimumBy = /*#__PURE__*/mumBy(ltU);
var minimum = /*#__PURE__*/minimumBy(id);
var none = /*#__PURE__*/I.curry(function none(xi2b, t, s) {
- return !traverseU(Select, function (x, i) {
+ return !getAsU(function (x, i) {
if (xi2b(x, i)) return true;
}, t, s);
});
var or$1 = /*#__PURE__*/any(id);
-var productAs = /*#__PURE__*/traverse( /*#__PURE__*/ConcatOf(multiplyU, 1));
+var productAs = /*#__PURE__*/traverse( /*#__PURE__*/ConstantWith(multiplyU, 1));
var product = /*#__PURE__*/productAs( /*#__PURE__*/unto(1));
-var selectAs = /*#__PURE__*/traverse(Select);
+var select = process.env.NODE_ENV === 'production' ? get : /*#__PURE__*/I.curry(function select(l, s) {
+ warn(select, '`select` has been obsoleted. Just use `get`. See CHANGELOG for details.');
+ return get(l, s);
+});
-var select = /*#__PURE__*/selectAs(id);
+var selectAs = process.env.NODE_ENV === 'production' ? getAs : /*#__PURE__*/I.curry(function selectAs(f, l, s) {
+ warn(selectAs, '`selectAs` has been obsoleted. Just use `getAs`. See CHANGELOG for details.');
+ return getAs(f, l, s);
+});
var sumAs = /*#__PURE__*/traverse(Sum);
var sum = /*#__PURE__*/sumAs(unto0);
-// Operations on lenses
-
-function get(l, s) {
- return 1 < arguments.length ? getU(l, s) : function (s) {
- return getU(l, s);
- };
-}
-
// Creating new lenses
var lens = /*#__PURE__*/I.curry(lensU);
@@ -1873,7 +1859,7 @@ var array = function array(elem) {
var inverse = function inverse(iso) {
return function (x, i, F, xi2yF) {
return F.map(function (x) {
- return getU(iso, x);
+ return getAsU(id, iso, x);
}, xi2yF(setU(iso, x, void 0), i));
};
};
@@ -1907,10 +1893,10 @@ var indexed = /*#__PURE__*/isoU( /*#__PURE__*/expect(seemsArrayLike, /*#__PURE__
}
n = xs.length;
var j = 0;
- for (var _i3 = 0; _i3 < n; ++_i3) {
- var x = xs[_i3];
+ for (var _i4 = 0; _i4 < n; ++_i4) {
+ var x = xs[_i4];
if (void 0 !== x) {
- if (_i3 !== j) xs[j] = x;
+ if (_i4 !== j) xs[j] = x;
++j;
}
}
@@ -1944,10 +1930,10 @@ var uriComponent = /*#__PURE__*/stringIsoU(decodeURIComponent, encodeURIComponen
var json = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : res(function (iso) {
return toFunction([iso, isoU(deepFreeze, id)]);
}))(function json(options) {
- var _ref2 = options || I.object0,
- reviver = _ref2.reviver,
- replacer = _ref2.replacer,
- space = _ref2.space;
+ var _ref3 = options || I.object0,
+ reviver = _ref3.reviver,
+ replacer = _ref3.replacer,
+ space = _ref3.space;
return isoU(expect(I.isString, function (text) {
return JSON.parse(text, reviver);
@@ -2028,7 +2014,7 @@ var subtract = function subtract(c) {
// Interop
var pointer = function pointer(s) {
- if (s[0] === '#') s = getU(uriComponent, s);
+ if (s[0] === '#') s = getAsU(id, uriComponent, s);
var ts = s.split('/');
var n = ts.length;
for (var i = 1; i < n; ++i) {
@@ -2042,7 +2028,7 @@ var pointer = function pointer(s) {
exports.seemsArrayLike = seemsArrayLike;
exports.Identity = Identity;
exports.IdentityAsync = IdentityAsync;
-exports.Constant = Constant;
+exports.Select = Select;
exports.toFunction = toFunction;
exports.assign = assign;
exports.modify = modify;
@@ -2058,7 +2044,6 @@ exports.choose = choose;
exports.cond = cond;
exports.condOf = condOf;
exports.ifElse = ifElse;
-exports.iftes = iftes;
exports.orElse = orElse;
exports.chain = chain;
exports.choice = choice;
@@ -2071,8 +2056,8 @@ exports.setIx = setIx;
exports.tieIx = tieIx;
exports.joinIx = joinIx;
exports.skipIx = skipIx;
-exports.log = log;
exports.getLog = getLog;
+exports.log = log;
exports.transform = transform;
exports.transformAsync = transformAsync;
exports.seq = seq;
@@ -2109,6 +2094,8 @@ exports.foldl = foldl;
exports.foldr = foldr;
exports.forEach = forEach;
exports.forEachWith = forEachWith;
+exports.get = get;
+exports.getAs = getAs;
exports.isDefined = isDefined;
exports.isEmpty = isEmpty;
exports.joinAs = joinAs;
@@ -2123,11 +2110,10 @@ exports.none = none;
exports.or = or$1;
exports.productAs = productAs;
exports.product = product;
-exports.selectAs = selectAs;
exports.select = select;
+exports.selectAs = selectAs;
exports.sumAs = sumAs;
exports.sum = sum;
-exports.get = get;
exports.lens = lens;
exports.getter = getter;
exports.setter = setter;
diff --git a/dist/partial.lenses.es.js b/dist/partial.lenses.es.js
index c20e3262..cf02fa74 100644
--- a/dist/partial.lenses.es.js
+++ b/dist/partial.lenses.es.js
@@ -1,4 +1,4 @@
-import { defineNameU, isString, isFunction, isArray, freeze, isObject, acyclicEqualsU, array0, object0, sndU, always, curry, curryN, assocPartialU, dissocPartialU, isNumber, constructorOf, toObject, applyU, isDefined, keys, hasU, arityN, id, assign } from 'infestines';
+import { defineNameU, isString, isFunction, always, freeze, isArray, isObject, acyclicEqualsU, array0, object0, inherit, sndU, curry, curryN, assocPartialU, dissocPartialU, isNumber, constructorOf, toObject, applyU, isDefined, keys, hasU, arityN, id, assign } from 'infestines';
var addU = function addU(x, y) {
return x + y;
@@ -190,6 +190,8 @@ var unto = function unto(c) {
};
var unto0 = /*#__PURE__*/unto(0);
+var toTrue = /*#__PURE__*/always(true);
+
var notPartial = function complement(x) {
return void 0 !== x ? !x : x;
};
@@ -204,6 +206,8 @@ var expect = function expect(p, f) {
}, f);
};
+var freezeInDev = process.env.NODE_ENV === 'production' ? id$1 : freeze;
+
function deepFreeze(x) {
if (isArray(x)) {
x.forEach(deepFreeze);
@@ -312,19 +316,32 @@ function selectInArrayLike(xi2v, xs) {
//
-var Select = {
- map: sndU,
- of: function of() {},
- ap: function ap(l, r) {
- return void 0 !== l ? l : r;
- }
+function Applicative(map, of, ap) {
+ if (!this) return freezeInDev(new Applicative(map, of, ap));
+ this.map = map;
+ this.of = of;
+ this.ap = ap;
+}
+
+var Monad = /*#__PURE__*/inherit(function Monad(map, of, ap, chain) {
+ if (!this) return freezeInDev(new Monad(map, of, ap, chain));
+ Applicative.call(this, map, of, ap);
+ this.chain = chain;
+}, Applicative);
+
+//
+
+var ConstantWith = function ConstantWith(ap, empty) {
+ return Applicative(sndU, always(empty), ap);
};
-var ConcatOf = function ConcatOf(ap, empty) {
- return { map: sndU, ap: ap, of: always(empty) };
+var ConstantOf = function ConstantOf(_ref) {
+ var concat = _ref.concat,
+ empty = _ref.empty;
+ return ConstantWith(concat, empty());
};
-var Sum = /*#__PURE__*/ConcatOf(addU, 0);
+var Sum = /*#__PURE__*/ConstantWith(addU, 0);
var mumBy = function mumBy(ord) {
return curry(function mumBy(xi2y, t, s) {
@@ -417,12 +434,6 @@ var reqMaybeArray = function reqMaybeArray(msg) {
//
-var reqApplicative = function reqApplicative(name, arg) {
- return function (C) {
- if (!C.of) errorGiven('`' + name + (arg ? '(' + arg + ')' : '') + '` requires an applicative', C, 'Note that you cannot `get` a traversal. Perhaps you wanted to `collect` it?');
- };
-};
-
var reqMonad = function reqMonad(name) {
return function (C) {
if (!C.chain) errorGiven('`' + name + '` requires a monad', C, 'Note that you can only `modify`, `remove`, `set`, and `traverse` a transform.');
@@ -481,17 +492,20 @@ function traversePartialIndex(A, xi2yA, xs, skip) {
//
-var ConstantLog = {
- map: function map(f, _ref) {
- var m = _ref.m,
- p = _ref.p,
- c = _ref.c;
- return { m: '%O <= ' + m, p: [f(p[0]), p], c: c };
- }
-};
-var getLogFn = function getLogFn(x) {
- return { m: '%O', p: [x, consExcept], c: x };
-};
+var SelectLog = /*#__PURE__*/Applicative(function (f, _ref2) {
+ var p = _ref2.p,
+ x = _ref2.x,
+ c = _ref2.c;
+
+ x = f(x);
+ if (!isFunction(x)) p = [x, p];
+ return { p: p, x: x, c: c };
+}, function (x) {
+ return { p: [], x: x, c: undefined };
+}, function (l, r) {
+ var v = undefined !== l.c ? l : r;
+ return { p: v.p, x: l.x(r.x), c: v.c };
+});
//
@@ -606,53 +620,30 @@ var modifyAsyncU = function modifyAsyncU(o, f, s) {
return returnAsync(toFunction(o)(s, void 0, IdentityAsync, f));
};
-function makeIx(i) {
- var ix = function ix(s, j) {
- return ix.v = j, s;
- };
- ix.v = i;
- return ix;
-}
-
-function getNestedU(l, s, j, ix) {
- for (var n = l.length, o; j < n; ++j) {
- switch (typeof (o = l[j])) {
- case 'string':
- s = getProp(ix.v = o, s);
- break;
- case 'number':
- s = getIndex(ix.v = o, s);
- break;
- case 'object':
- s = getNestedU(o, s, 0, ix);
- break;
- default:
- s = o(s, ix.v, Constant, ix);
- }
- }return s;
-}
-
-var getU = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : par(0, ef(reqOptic)))(function (l, s) {
+var getAsU = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : par(1, ef(reqOptic)))(function getAs(xi2y, l, s) {
switch (typeof l) {
case 'string':
- return getProp(l, s);
+ return xi2y(getProp(l, s), l);
case 'number':
- return getIndex(l, s);
+ return xi2y(getIndex(l, s), l);
case 'object':
- for (var i = 0, n = l.length, o; i < n; ++i) {
- switch (typeof (o = l[i])) {
- case 'string':
- s = getProp(o, s);
- break;
- case 'number':
- s = getIndex(o, s);
- break;
- default:
- return getNestedU(l, s, i, makeIx(l[i - 1]));
- }
- }return s;
+ {
+ var n = l.length;
+ for (var i = 0, o; i < n; ++i) {
+ switch (typeof (o = l[i])) {
+ case 'string':
+ s = getProp(o, s);
+ break;
+ case 'number':
+ s = getIndex(o, s);
+ break;
+ default:
+ return composed(i, l)(s, l[i - 1], Select, xi2y);
+ }
+ }return xi2y(s, l[n - 1]);
+ }
default:
- return l(s, void 0, Constant, id$1);
+ return xi2y !== id$1 && l.length !== 4 ? xi2y(l(s, void 0), void 0) : l(s, void 0, Select, xi2y);
}
});
@@ -710,7 +701,7 @@ var getPick = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : res(f
var r = void 0;
for (var k in template) {
var t = template[k];
- var v = isObject(t) ? getPick(t, x) : getU(t, x);
+ var v = isObject(t) ? getPick(t, x) : getAsU(id$1, t, x);
if (void 0 !== v) {
if (!r) r = {};
r[k] = v;
@@ -1046,10 +1037,9 @@ var orElseU = function orElse(back, prim) {
};
};
-function zeroOp(y, i, C, xi2yC, x) {
- var of = C.of;
- return of ? of(y) : C.map(always(y), xi2yC(x, i));
-}
+var zero = function zero(x, _i, C, _xi2yC) {
+ return C.of(x);
+};
//
@@ -1075,13 +1065,6 @@ var pickInAux = function pickInAux(t, k) {
//
-var condOfDefault = /*#__PURE__*/always(zeroOp);
-var condOfCase = function condOfCase(p, o, r) {
- return function (y, j) {
- return p(y, j) ? o : r(y, j);
- };
-};
-
// Auxiliary
var seemsArrayLike = function seemsArrayLike(x) {
@@ -1090,28 +1073,18 @@ var seemsArrayLike = function seemsArrayLike(x) {
// Internals
-var Identity = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : freeze)({
- map: applyU,
- of: id$1,
- ap: applyU,
- chain: applyU
-});
+var Identity = /*#__PURE__*/Monad(applyU, id$1, applyU, applyU);
-var IdentityAsync = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : freeze)({
- map: chainAsync,
- ap: function ap(xyP, xP) {
- return chainAsync(function (xP) {
- return chainAsync(function (xyP) {
- return xyP(xP);
- }, xyP);
- }, xP);
- },
- of: id$1,
- chain: chainAsync
-});
+var IdentityAsync = /*#__PURE__*/Monad(chainAsync, id$1, function (xyP, xP) {
+ return chainAsync(function (xP) {
+ return chainAsync(function (xyP) {
+ return xyP(xP);
+ }, xyP);
+ }, xP);
+}, chainAsync);
-var Constant = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : freeze)({
- map: sndU
+var Select = /*#__PURE__*/ConstantWith(function (l, r) {
+ return void 0 !== l ? l : r;
});
var toFunction = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : par(0, ef(reqOptic)))(function toFunction(o) {
@@ -1230,14 +1203,38 @@ var condOf = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : functi
};
})(function condOf(of) {
of = toFunction(of);
- var op = condOfDefault;
- var n = arguments.length;
- while (--n) {
- var c = arguments[n];
- op = c.length === 1 ? always(toFunction(c[0])) : condOfCase(c[0], toFunction(c[1]), op);
+
+ var n = arguments.length - 1;
+ if (!n) return zero;
+
+ var def = arguments[n];
+ if (def.length === 1) {
+ --n;
+ def = toFunction(def[0]);
+ } else {
+ def = zero;
+ }
+
+ var ps = Array(n);
+ var os = Array(n + 1);
+ for (var i = 0; i < n; ++i) {
+ var c = arguments[i + 1];
+ ps[i] = c[0];
+ os[i] = toFunction(c[1]);
}
- return function condOf(x, i, C, xi2yC) {
- return of(x, i, Constant, op)(x, i, C, xi2yC);
+ os[n] = def;
+
+ return function condOf(x, i, F, xi2yF) {
+ var min = n;
+ of(x, i, Select, function (y, j) {
+ for (var _i3 = 0; _i3 < min; ++_i3) {
+ if (ps[_i3](y, j)) {
+ min = _i3;
+ if (_i3 === 0) return 0;else break;
+ }
+ }
+ });
+ return os[min](x, i, F, xi2yF);
};
});
@@ -1245,19 +1242,6 @@ var ifElse = /*#__PURE__*/curry(function ifElse(c, t, e) {
return eitherU(toFunction(t), toFunction(e))(c);
});
-var iftes = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : function (fn$$1) {
- return function iftes(_c, _t) {
- warn(iftes, '`iftes` has been obsoleted. Use `ifElse` or `cond` instead. See CHANGELOG for details.');
- return fn$$1.apply(null, arguments);
- };
-})(function iftes(_c, _t) {
- var n = arguments.length;
- var r = n & 1 ? toFunction(arguments[--n]) : zero;
- while (0 <= (n -= 2)) {
- r = eitherU(toFunction(arguments[n + 1]), r)(arguments[n]);
- }return r;
-});
-
var orElse = /*#__PURE__*/curry(orElseU);
// Querying
@@ -1276,16 +1260,12 @@ var choice = function choice() {
return os.reduceRight(orElseU, zero);
};
-var unless = /*#__PURE__*/eitherU(zeroOp, identity);
+var unless = /*#__PURE__*/eitherU(zero, identity);
-var when = /*#__PURE__*/eitherU(identity, zeroOp);
+var when = /*#__PURE__*/eitherU(identity, zero);
var optional = /*#__PURE__*/when(isDefined);
-var zero = function zero(x, i, C, xi2yC) {
- return zeroOp(x, i, C, xi2yC);
-};
-
// Indices
var mapIx = function mapIx(ix2j) {
@@ -1317,6 +1297,20 @@ var skipIx = /*#__PURE__*/setName( /*#__PURE__*/tieIx(sndU), 'skipIx');
// Debugging
+function getLog(l, s) {
+ var _traverseU = traverseU(SelectLog, function (x) {
+ return { p: [x, consExcept], x: x, c: x };
+ }, l, s),
+ p = _traverseU.p,
+ c = _traverseU.c;
+
+ p = pushTo(p, ['%O']);
+ for (var i = 2; i < p.length; ++i) {
+ p[0] += ' <= %O';
+ }console.log.apply(console, p);
+ return c;
+}
+
function log() {
var show = curry(function log(dir, x) {
console.log.apply(console, copyToFrom([], 0, arguments, 0, arguments.length).concat([dir, x]));
@@ -1325,16 +1319,6 @@ function log() {
return isoU(show('get'), show('set'));
}
-var getLog = /*#__PURE__*/curry(function getLog(l, s) {
- var _traverseU = traverseU(ConstantLog, getLogFn, l, s),
- m = _traverseU.m,
- p = _traverseU.p,
- c = _traverseU.c;
-
- console.log.apply(console, pushTo(p, [m]));
- return c;
-});
-
// Operations on transforms
var transform = /*#__PURE__*/curry(function transform(o, s) {
@@ -1370,14 +1354,14 @@ var assignOp = function assignOp(x) {
};
var modifyOp = function modifyOp(xi2y) {
- return function modifyOp(x, i, C, xi2yC) {
- return zeroOp(x = xi2y(x, i), i, C, xi2yC, x);
+ return function modifyOp(x, i, C, _xi2yC) {
+ return C.of(xi2y(x, i));
};
};
var setOp = function setOp(y) {
- return function setOp(_x, i, C, xi2yC) {
- return zeroOp(y, i, C, xi2yC, y);
+ return function setOp(_x, _i, C, _xi2yC) {
+ return C.of(y);
};
};
@@ -1404,9 +1388,9 @@ function branches() {
// Traversals and combinators
-var elems = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : par(2, ef(reqApplicative('elems'))))(function elems(xs, i, A, xi2yA) {
+function elems(xs, i, A, xi2yA) {
return seemsArrayLike(xs) ? elemsI(xs, i, A, xi2yA) : A.of(xs);
-});
+}
var elemsTotal = function elemsTotal(xs, i, A, xi2yA) {
return seemsArrayLike(xs) ? A === Identity ? mapPartialIndexU(xi2yA, xs, mapPartialIndexU) : A === Select ? selectInArrayLike(xi2yA, xs) : traversePartialIndex(A, xi2yA, xs, traversePartialIndex) : A.of(xs);
@@ -1416,9 +1400,7 @@ var entries = /*#__PURE__*/setName( /*#__PURE__*/toFunction([keyed, elems]), 'en
var keys$1 = /*#__PURE__*/setName( /*#__PURE__*/toFunction([keyed, elems, 0]), 'keys');
-var matches = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : dep(function (re) {
- return re.global ? res(par(2, ef(reqApplicative('matches', re)))) : id$1;
-}))(function matches(re) {
+function matches(re) {
return function matches(x, _i, C, xi2yC) {
if (isString(x)) {
var map = C.map;
@@ -1442,22 +1424,22 @@ var matches = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : dep(f
}, xi2yC(m[0], reIndex(m)));
}
}
- return zeroOp(x, void 0, C, xi2yC);
+ return C.of(x);
};
-});
+}
-var values = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : par(2, ef(reqApplicative('values'))))( /*#__PURE__*/setName( /*#__PURE__*/branchOr1Level(identity, protoless0), 'values'));
+var values = /*#__PURE__*/setName( /*#__PURE__*/branchOr1Level(identity, protoless0), 'values');
-var children = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : par(2, ef(reqApplicative('children'))))(function children(x, i, C, xi2yC) {
+function children(x, i, C, xi2yC) {
return isArray(x) ? elemsI(x, i, C, xi2yC) : isObject(x) ? values(x, i, C, xi2yC) : C.of(x);
-});
+}
-var flatten = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : par(2, ef(reqApplicative('flatten'))))(function flatten(x, i, C, xi2yC) {
+function flatten(x, i, C, xi2yC) {
var rec = function rec(x, i) {
return isArray(x) ? elemsI(x, i, C, rec) : void 0 !== x ? xi2yC(x, i) : C.of(x);
};
return rec(x, i);
-});
+}
function query() {
var r = [];
@@ -1484,7 +1466,7 @@ var leafs = /*#__PURE__*/satisfying(function (x) {
// Folds over traversals
var all = /*#__PURE__*/curry(function all(xi2b, t, s) {
- return !traverseU(Select, function (x, i) {
+ return !getAsU(function (x, i) {
if (!xi2b(x, i)) return true;
}, t, s);
});
@@ -1492,7 +1474,7 @@ var all = /*#__PURE__*/curry(function all(xi2b, t, s) {
var and$1 = /*#__PURE__*/all(id$1);
var any = /*#__PURE__*/curry(function any(xi2b, t, s) {
- return !!traverseU(Select, function (x, i) {
+ return !!getAsU(function (x, i) {
if (xi2b(x, i)) return true;
}, t, s);
});
@@ -1508,9 +1490,7 @@ var collectAs = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? curry : re
var collect = /*#__PURE__*/collectAs(id$1);
-var concatAs = /*#__PURE__*/mkTraverse(id$1, function concatAs(m) {
- return ConcatOf(m.concat, m.empty());
-});
+var concatAs = /*#__PURE__*/mkTraverse(id$1, ConstantOf);
var concat = /*#__PURE__*/concatAs(id$1);
@@ -1567,16 +1547,24 @@ var forEachWith = /*#__PURE__*/curry(function forEachWith(newC, ef$$1, t, s) {
return c;
});
+function get(l, s) {
+ return 1 < arguments.length ? getAsU(id$1, l, s) : function (s) {
+ return getAsU(id$1, l, s);
+ };
+}
+
+var getAs = /*#__PURE__*/curry(getAsU);
+
var isDefined$1 = /*#__PURE__*/curry(function isDefined$$1(t, s) {
- return void 0 !== traverseU(Select, id$1, t, s);
+ return void 0 !== getAsU(id$1, t, s);
});
var isEmpty = /*#__PURE__*/curry(function isEmpty(t, s) {
- return !traverseU(Select, always(true), t, s);
+ return !getAsU(toTrue, t, s);
});
var joinAs = /*#__PURE__*/mkTraverse(toStringPartial, /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : par(0, ef(reqString('`join` and `joinAs` expect a string delimiter'))))(function joinAs(d) {
- return ConcatOf(function (x, y) {
+ return ConstantWith(function (x, y) {
return void 0 !== x ? void 0 !== y ? x + d + y : x : y;
});
}));
@@ -1607,33 +1595,31 @@ var minimumBy = /*#__PURE__*/mumBy(ltU);
var minimum = /*#__PURE__*/minimumBy(id$1);
var none = /*#__PURE__*/curry(function none(xi2b, t, s) {
- return !traverseU(Select, function (x, i) {
+ return !getAsU(function (x, i) {
if (xi2b(x, i)) return true;
}, t, s);
});
var or$1 = /*#__PURE__*/any(id$1);
-var productAs = /*#__PURE__*/traverse( /*#__PURE__*/ConcatOf(multiplyU, 1));
+var productAs = /*#__PURE__*/traverse( /*#__PURE__*/ConstantWith(multiplyU, 1));
var product = /*#__PURE__*/productAs( /*#__PURE__*/unto(1));
-var selectAs = /*#__PURE__*/traverse(Select);
+var select = process.env.NODE_ENV === 'production' ? get : /*#__PURE__*/curry(function select(l, s) {
+ warn(select, '`select` has been obsoleted. Just use `get`. See CHANGELOG for details.');
+ return get(l, s);
+});
-var select = /*#__PURE__*/selectAs(id$1);
+var selectAs = process.env.NODE_ENV === 'production' ? getAs : /*#__PURE__*/curry(function selectAs(f, l, s) {
+ warn(selectAs, '`selectAs` has been obsoleted. Just use `getAs`. See CHANGELOG for details.');
+ return getAs(f, l, s);
+});
var sumAs = /*#__PURE__*/traverse(Sum);
var sum = /*#__PURE__*/sumAs(unto0);
-// Operations on lenses
-
-function get(l, s) {
- return 1 < arguments.length ? getU(l, s) : function (s) {
- return getU(l, s);
- };
-}
-
// Creating new lenses
var lens = /*#__PURE__*/curry(lensU);
@@ -1869,7 +1855,7 @@ var array = function array(elem) {
var inverse = function inverse(iso) {
return function (x, i, F, xi2yF) {
return F.map(function (x) {
- return getU(iso, x);
+ return getAsU(id$1, iso, x);
}, xi2yF(setU(iso, x, void 0), i));
};
};
@@ -1903,10 +1889,10 @@ var indexed = /*#__PURE__*/isoU( /*#__PURE__*/expect(seemsArrayLike, /*#__PURE__
}
n = xs.length;
var j = 0;
- for (var _i3 = 0; _i3 < n; ++_i3) {
- var x = xs[_i3];
+ for (var _i4 = 0; _i4 < n; ++_i4) {
+ var x = xs[_i4];
if (void 0 !== x) {
- if (_i3 !== j) xs[j] = x;
+ if (_i4 !== j) xs[j] = x;
++j;
}
}
@@ -1940,10 +1926,10 @@ var uriComponent = /*#__PURE__*/stringIsoU(decodeURIComponent, encodeURIComponen
var json = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id$1 : res(function (iso) {
return toFunction([iso, isoU(deepFreeze, id$1)]);
}))(function json(options) {
- var _ref2 = options || object0,
- reviver = _ref2.reviver,
- replacer = _ref2.replacer,
- space = _ref2.space;
+ var _ref3 = options || object0,
+ reviver = _ref3.reviver,
+ replacer = _ref3.replacer,
+ space = _ref3.space;
return isoU(expect(isString, function (text) {
return JSON.parse(text, reviver);
@@ -2024,7 +2010,7 @@ var subtract = function subtract(c) {
// Interop
var pointer = function pointer(s) {
- if (s[0] === '#') s = getU(uriComponent, s);
+ if (s[0] === '#') s = getAsU(id$1, uriComponent, s);
var ts = s.split('/');
var n = ts.length;
for (var i = 1; i < n; ++i) {
@@ -2035,4 +2021,4 @@ var pointer = function pointer(s) {
return ts;
};
-export { seemsArrayLike, Identity, IdentityAsync, Constant, toFunction, assign$1 as assign, modify, modifyAsync, remove, set, traverse, compose, flat, lazy, choices, choose, cond, condOf, ifElse, iftes, orElse, chain, choice, unless, when, optional, zero, mapIx, setIx, tieIx, joinIx, skipIx, log, getLog, transform, transformAsync, seq, assignOp, modifyOp, setOp, removeOp, branchOr, branch, branches, elems, elemsTotal, entries, keys$1 as keys, matches, values, children, flatten, query, satisfying, leafs, all, and$1 as and, any, collectAs, collect, concatAs, concat, countIf, count, countsAs, counts, foldl, foldr, forEach, forEachWith, isDefined$1 as isDefined, isEmpty, joinAs, join, maximumBy, maximum, meanAs, mean, minimumBy, minimum, none, or$1 as or, productAs, product, selectAs, select, sumAs, sum, get, lens, getter, setter, foldTraversalLens, defaults, define, normalize, required, reread, rewrite, append, filter, find, findWith, first, index, last, prefix, slice, suffix, pickIn, prop, props, propsOf, removable, valueOr, pick, replace$1 as replace, getInverse, iso, array, inverse, complement, identity, is, indexed, reverse, singleton, disjoint, keyed, uri, uriComponent, json, dropPrefix, dropSuffix, replaces, split, uncouple, add$1 as add, divide, multiply$1 as multiply, negate$1 as negate, subtract, pointer };
+export { seemsArrayLike, Identity, IdentityAsync, Select, toFunction, assign$1 as assign, modify, modifyAsync, remove, set, traverse, compose, flat, lazy, choices, choose, cond, condOf, ifElse, orElse, chain, choice, unless, when, optional, zero, mapIx, setIx, tieIx, joinIx, skipIx, getLog, log, transform, transformAsync, seq, assignOp, modifyOp, setOp, removeOp, branchOr, branch, branches, elems, elemsTotal, entries, keys$1 as keys, matches, values, children, flatten, query, satisfying, leafs, all, and$1 as and, any, collectAs, collect, concatAs, concat, countIf, count, countsAs, counts, foldl, foldr, forEach, forEachWith, get, getAs, isDefined$1 as isDefined, isEmpty, joinAs, join, maximumBy, maximum, meanAs, mean, minimumBy, minimum, none, or$1 as or, productAs, product, select, selectAs, sumAs, sum, lens, getter, setter, foldTraversalLens, defaults, define, normalize, required, reread, rewrite, append, filter, find, findWith, first, index, last, prefix, slice, suffix, pickIn, prop, props, propsOf, removable, valueOr, pick, replace$1 as replace, getInverse, iso, array, inverse, complement, identity, is, indexed, reverse, singleton, disjoint, keyed, uri, uriComponent, json, dropPrefix, dropSuffix, replaces, split, uncouple, add$1 as add, divide, multiply$1 as multiply, negate$1 as negate, subtract, pointer };
diff --git a/dist/partial.lenses.js b/dist/partial.lenses.js
index 7f556c64..07756843 100644
--- a/dist/partial.lenses.js
+++ b/dist/partial.lenses.js
@@ -190,6 +190,8 @@
};
var unto0 = /*#__PURE__*/unto(0);
+ var toTrue = /*#__PURE__*/I.always(true);
+
var notPartial = function complement(x) {
return void 0 !== x ? !x : x;
};
@@ -204,6 +206,8 @@
}, f);
};
+ var freezeInDev = I.freeze;
+
function deepFreeze(x) {
if (I.isArray(x)) {
x.forEach(deepFreeze);
@@ -312,19 +316,32 @@
//
- var Select = {
- map: I.sndU,
- of: function of() {},
- ap: function ap(l, r) {
- return void 0 !== l ? l : r;
- }
+ function Applicative(map, of, ap) {
+ if (!this) return freezeInDev(new Applicative(map, of, ap));
+ this.map = map;
+ this.of = of;
+ this.ap = ap;
+ }
+
+ var Monad = /*#__PURE__*/I.inherit(function Monad(map, of, ap, chain) {
+ if (!this) return freezeInDev(new Monad(map, of, ap, chain));
+ Applicative.call(this, map, of, ap);
+ this.chain = chain;
+ }, Applicative);
+
+ //
+
+ var ConstantWith = function ConstantWith(ap, empty) {
+ return Applicative(I.sndU, I.always(empty), ap);
};
- var ConcatOf = function ConcatOf(ap, empty) {
- return { map: I.sndU, ap: ap, of: I.always(empty) };
+ var ConstantOf = function ConstantOf(_ref) {
+ var concat = _ref.concat,
+ empty = _ref.empty;
+ return ConstantWith(concat, empty());
};
- var Sum = /*#__PURE__*/ConcatOf(addU, 0);
+ var Sum = /*#__PURE__*/ConstantWith(addU, 0);
var mumBy = function mumBy(ord) {
return I.curry(function mumBy(xi2y, t, s) {
@@ -417,12 +434,6 @@
//
- var reqApplicative = function reqApplicative(name, arg) {
- return function (C) {
- if (!C.of) errorGiven('`' + name + (arg ? '(' + arg + ')' : '') + '` requires an applicative', C, 'Note that you cannot `get` a traversal. Perhaps you wanted to `collect` it?');
- };
- };
-
var reqMonad = function reqMonad(name) {
return function (C) {
if (!C.chain) errorGiven('`' + name + '` requires a monad', C, 'Note that you can only `modify`, `remove`, `set`, and `traverse` a transform.');
@@ -481,17 +492,20 @@
//
- var ConstantLog = {
- map: function map(f, _ref) {
- var m = _ref.m,
- p = _ref.p,
- c = _ref.c;
- return { m: '%O <= ' + m, p: [f(p[0]), p], c: c };
- }
- };
- var getLogFn = function getLogFn(x) {
- return { m: '%O', p: [x, consExcept], c: x };
- };
+ var SelectLog = /*#__PURE__*/Applicative(function (f, _ref2) {
+ var p = _ref2.p,
+ x = _ref2.x,
+ c = _ref2.c;
+
+ x = f(x);
+ if (!I.isFunction(x)) p = [x, p];
+ return { p: p, x: x, c: c };
+ }, function (x) {
+ return { p: [], x: x, c: undefined };
+ }, function (l, r) {
+ var v = undefined !== l.c ? l : r;
+ return { p: v.p, x: l.x(r.x), c: v.c };
+ });
//
@@ -606,53 +620,30 @@
return returnAsync(toFunction(o)(s, void 0, IdentityAsync, f));
};
- function makeIx(i) {
- var ix = function ix(s, j) {
- return ix.v = j, s;
- };
- ix.v = i;
- return ix;
- }
-
- function getNestedU(l, s, j, ix) {
- for (var n = l.length, o; j < n; ++j) {
- switch (typeof (o = l[j])) {
- case 'string':
- s = getProp(ix.v = o, s);
- break;
- case 'number':
- s = getIndex(ix.v = o, s);
- break;
- case 'object':
- s = getNestedU(o, s, 0, ix);
- break;
- default:
- s = o(s, ix.v, Constant, ix);
- }
- }return s;
- }
-
- var getU = /*#__PURE__*/(par(0, ef(reqOptic)))(function (l, s) {
+ var getAsU = /*#__PURE__*/(par(1, ef(reqOptic)))(function getAs(xi2y, l, s) {
switch (typeof l) {
case 'string':
- return getProp(l, s);
+ return xi2y(getProp(l, s), l);
case 'number':
- return getIndex(l, s);
+ return xi2y(getIndex(l, s), l);
case 'object':
- for (var i = 0, n = l.length, o; i < n; ++i) {
- switch (typeof (o = l[i])) {
- case 'string':
- s = getProp(o, s);
- break;
- case 'number':
- s = getIndex(o, s);
- break;
- default:
- return getNestedU(l, s, i, makeIx(l[i - 1]));
- }
- }return s;
+ {
+ var n = l.length;
+ for (var i = 0, o; i < n; ++i) {
+ switch (typeof (o = l[i])) {
+ case 'string':
+ s = getProp(o, s);
+ break;
+ case 'number':
+ s = getIndex(o, s);
+ break;
+ default:
+ return composed(i, l)(s, l[i - 1], Select, xi2y);
+ }
+ }return xi2y(s, l[n - 1]);
+ }
default:
- return l(s, void 0, Constant, id);
+ return xi2y !== id && l.length !== 4 ? xi2y(l(s, void 0), void 0) : l(s, void 0, Select, xi2y);
}
});
@@ -710,7 +701,7 @@
var r = void 0;
for (var k in template) {
var t = template[k];
- var v = I.isObject(t) ? getPick(t, x) : getU(t, x);
+ var v = I.isObject(t) ? getPick(t, x) : getAsU(id, t, x);
if (void 0 !== v) {
if (!r) r = {};
r[k] = v;
@@ -1046,10 +1037,9 @@
};
};
- function zeroOp(y, i, C, xi2yC, x) {
- var of = C.of;
- return of ? of(y) : C.map(I.always(y), xi2yC(x, i));
- }
+ var zero = function zero(x, _i, C, _xi2yC) {
+ return C.of(x);
+ };
//
@@ -1075,13 +1065,6 @@
//
- var condOfDefault = /*#__PURE__*/I.always(zeroOp);
- var condOfCase = function condOfCase(p, o, r) {
- return function (y, j) {
- return p(y, j) ? o : r(y, j);
- };
- };
-
// Auxiliary
var seemsArrayLike = function seemsArrayLike(x) {
@@ -1090,28 +1073,18 @@
// Internals
- var Identity = /*#__PURE__*/(0, I.freeze)({
- map: I.applyU,
- of: id,
- ap: I.applyU,
- chain: I.applyU
- });
+ var Identity = /*#__PURE__*/Monad(I.applyU, id, I.applyU, I.applyU);
- var IdentityAsync = /*#__PURE__*/(0, I.freeze)({
- map: chainAsync,
- ap: function ap(xyP, xP) {
- return chainAsync(function (xP) {
- return chainAsync(function (xyP) {
- return xyP(xP);
- }, xyP);
- }, xP);
- },
- of: id,
- chain: chainAsync
- });
+ var IdentityAsync = /*#__PURE__*/Monad(chainAsync, id, function (xyP, xP) {
+ return chainAsync(function (xP) {
+ return chainAsync(function (xyP) {
+ return xyP(xP);
+ }, xyP);
+ }, xP);
+ }, chainAsync);
- var Constant = /*#__PURE__*/(0, I.freeze)({
- map: I.sndU
+ var Select = /*#__PURE__*/ConstantWith(function (l, r) {
+ return void 0 !== l ? l : r;
});
var toFunction = /*#__PURE__*/(par(0, ef(reqOptic)))(function toFunction(o) {
@@ -1230,14 +1203,38 @@
};
})(function condOf(of) {
of = toFunction(of);
- var op = condOfDefault;
- var n = arguments.length;
- while (--n) {
- var c = arguments[n];
- op = c.length === 1 ? I.always(toFunction(c[0])) : condOfCase(c[0], toFunction(c[1]), op);
+
+ var n = arguments.length - 1;
+ if (!n) return zero;
+
+ var def = arguments[n];
+ if (def.length === 1) {
+ --n;
+ def = toFunction(def[0]);
+ } else {
+ def = zero;
+ }
+
+ var ps = Array(n);
+ var os = Array(n + 1);
+ for (var i = 0; i < n; ++i) {
+ var c = arguments[i + 1];
+ ps[i] = c[0];
+ os[i] = toFunction(c[1]);
}
- return function condOf(x, i, C, xi2yC) {
- return of(x, i, Constant, op)(x, i, C, xi2yC);
+ os[n] = def;
+
+ return function condOf(x, i, F, xi2yF) {
+ var min = n;
+ of(x, i, Select, function (y, j) {
+ for (var _i3 = 0; _i3 < min; ++_i3) {
+ if (ps[_i3](y, j)) {
+ min = _i3;
+ if (_i3 === 0) return 0;else break;
+ }
+ }
+ });
+ return os[min](x, i, F, xi2yF);
};
});
@@ -1245,19 +1242,6 @@
return eitherU(toFunction(t), toFunction(e))(c);
});
- var iftes = /*#__PURE__*/(function (fn$$1) {
- return function iftes(_c, _t) {
- warn(iftes, '`iftes` has been obsoleted. Use `ifElse` or `cond` instead. See CHANGELOG for details.');
- return fn$$1.apply(null, arguments);
- };
- })(function iftes(_c, _t) {
- var n = arguments.length;
- var r = n & 1 ? toFunction(arguments[--n]) : zero;
- while (0 <= (n -= 2)) {
- r = eitherU(toFunction(arguments[n + 1]), r)(arguments[n]);
- }return r;
- });
-
var orElse = /*#__PURE__*/I.curry(orElseU);
// Querying
@@ -1276,16 +1260,12 @@
return os.reduceRight(orElseU, zero);
};
- var unless = /*#__PURE__*/eitherU(zeroOp, identity);
+ var unless = /*#__PURE__*/eitherU(zero, identity);
- var when = /*#__PURE__*/eitherU(identity, zeroOp);
+ var when = /*#__PURE__*/eitherU(identity, zero);
var optional = /*#__PURE__*/when(I.isDefined);
- var zero = function zero(x, i, C, xi2yC) {
- return zeroOp(x, i, C, xi2yC);
- };
-
// Indices
var mapIx = function mapIx(ix2j) {
@@ -1317,6 +1297,20 @@
// Debugging
+ function getLog(l, s) {
+ var _traverseU = traverseU(SelectLog, function (x) {
+ return { p: [x, consExcept], x: x, c: x };
+ }, l, s),
+ p = _traverseU.p,
+ c = _traverseU.c;
+
+ p = pushTo(p, ['%O']);
+ for (var i = 2; i < p.length; ++i) {
+ p[0] += ' <= %O';
+ }console.log.apply(console, p);
+ return c;
+ }
+
function log() {
var show = I.curry(function log(dir, x) {
console.log.apply(console, copyToFrom([], 0, arguments, 0, arguments.length).concat([dir, x]));
@@ -1325,16 +1319,6 @@
return isoU(show('get'), show('set'));
}
- var getLog = /*#__PURE__*/I.curry(function getLog(l, s) {
- var _traverseU = traverseU(ConstantLog, getLogFn, l, s),
- m = _traverseU.m,
- p = _traverseU.p,
- c = _traverseU.c;
-
- console.log.apply(console, pushTo(p, [m]));
- return c;
- });
-
// Operations on transforms
var transform = /*#__PURE__*/I.curry(function transform(o, s) {
@@ -1370,14 +1354,14 @@
};
var modifyOp = function modifyOp(xi2y) {
- return function modifyOp(x, i, C, xi2yC) {
- return zeroOp(x = xi2y(x, i), i, C, xi2yC, x);
+ return function modifyOp(x, i, C, _xi2yC) {
+ return C.of(xi2y(x, i));
};
};
var setOp = function setOp(y) {
- return function setOp(_x, i, C, xi2yC) {
- return zeroOp(y, i, C, xi2yC, y);
+ return function setOp(_x, _i, C, _xi2yC) {
+ return C.of(y);
};
};
@@ -1404,9 +1388,9 @@
// Traversals and combinators
- var elems = /*#__PURE__*/(par(2, ef(reqApplicative('elems'))))(function elems(xs, i, A, xi2yA) {
+ function elems(xs, i, A, xi2yA) {
return seemsArrayLike(xs) ? elemsI(xs, i, A, xi2yA) : A.of(xs);
- });
+ }
var elemsTotal = function elemsTotal(xs, i, A, xi2yA) {
return seemsArrayLike(xs) ? A === Identity ? mapPartialIndexU(xi2yA, xs, mapPartialIndexU) : A === Select ? selectInArrayLike(xi2yA, xs) : traversePartialIndex(A, xi2yA, xs, traversePartialIndex) : A.of(xs);
@@ -1416,9 +1400,7 @@
var keys = /*#__PURE__*/setName( /*#__PURE__*/toFunction([keyed, elems, 0]), 'keys');
- var matches = /*#__PURE__*/(dep(function (re) {
- return re.global ? res(par(2, ef(reqApplicative('matches', re)))) : id;
- }))(function matches(re) {
+ function matches(re) {
return function matches(x, _i, C, xi2yC) {
if (I.isString(x)) {
var map = C.map;
@@ -1442,22 +1424,22 @@
}, xi2yC(m[0], reIndex(m)));
}
}
- return zeroOp(x, void 0, C, xi2yC);
+ return C.of(x);
};
- });
+ }
- var values = /*#__PURE__*/(par(2, ef(reqApplicative('values'))))( /*#__PURE__*/setName( /*#__PURE__*/branchOr1Level(identity, protoless0), 'values'));
+ var values = /*#__PURE__*/setName( /*#__PURE__*/branchOr1Level(identity, protoless0), 'values');
- var children = /*#__PURE__*/(par(2, ef(reqApplicative('children'))))(function children(x, i, C, xi2yC) {
+ function children(x, i, C, xi2yC) {
return I.isArray(x) ? elemsI(x, i, C, xi2yC) : I.isObject(x) ? values(x, i, C, xi2yC) : C.of(x);
- });
+ }
- var flatten = /*#__PURE__*/(par(2, ef(reqApplicative('flatten'))))(function flatten(x, i, C, xi2yC) {
+ function flatten(x, i, C, xi2yC) {
var rec = function rec(x, i) {
return I.isArray(x) ? elemsI(x, i, C, rec) : void 0 !== x ? xi2yC(x, i) : C.of(x);
};
return rec(x, i);
- });
+ }
function query() {
var r = [];
@@ -1484,7 +1466,7 @@
// Folds over traversals
var all = /*#__PURE__*/I.curry(function all(xi2b, t, s) {
- return !traverseU(Select, function (x, i) {
+ return !getAsU(function (x, i) {
if (!xi2b(x, i)) return true;
}, t, s);
});
@@ -1492,7 +1474,7 @@
var and$1 = /*#__PURE__*/all(id);
var any = /*#__PURE__*/I.curry(function any(xi2b, t, s) {
- return !!traverseU(Select, function (x, i) {
+ return !!getAsU(function (x, i) {
if (xi2b(x, i)) return true;
}, t, s);
});
@@ -1508,9 +1490,7 @@
var collect = /*#__PURE__*/collectAs(id);
- var concatAs = /*#__PURE__*/mkTraverse(id, function concatAs(m) {
- return ConcatOf(m.concat, m.empty());
- });
+ var concatAs = /*#__PURE__*/mkTraverse(id, ConstantOf);
var concat = /*#__PURE__*/concatAs(id);
@@ -1567,16 +1547,24 @@
return c;
});
+ function get(l, s) {
+ return 1 < arguments.length ? getAsU(id, l, s) : function (s) {
+ return getAsU(id, l, s);
+ };
+ }
+
+ var getAs = /*#__PURE__*/I.curry(getAsU);
+
var isDefined = /*#__PURE__*/I.curry(function isDefined(t, s) {
- return void 0 !== traverseU(Select, id, t, s);
+ return void 0 !== getAsU(id, t, s);
});
var isEmpty = /*#__PURE__*/I.curry(function isEmpty(t, s) {
- return !traverseU(Select, I.always(true), t, s);
+ return !getAsU(toTrue, t, s);
});
var joinAs = /*#__PURE__*/mkTraverse(toStringPartial, /*#__PURE__*/(par(0, ef(reqString('`join` and `joinAs` expect a string delimiter'))))(function joinAs(d) {
- return ConcatOf(function (x, y) {
+ return ConstantWith(function (x, y) {
return void 0 !== x ? void 0 !== y ? x + d + y : x : y;
});
}));
@@ -1607,33 +1595,31 @@
var minimum = /*#__PURE__*/minimumBy(id);
var none = /*#__PURE__*/I.curry(function none(xi2b, t, s) {
- return !traverseU(Select, function (x, i) {
+ return !getAsU(function (x, i) {
if (xi2b(x, i)) return true;
}, t, s);
});
var or$1 = /*#__PURE__*/any(id);
- var productAs = /*#__PURE__*/traverse( /*#__PURE__*/ConcatOf(multiplyU, 1));
+ var productAs = /*#__PURE__*/traverse( /*#__PURE__*/ConstantWith(multiplyU, 1));
var product = /*#__PURE__*/productAs( /*#__PURE__*/unto(1));
- var selectAs = /*#__PURE__*/traverse(Select);
+ var select = I.curry(function select(l, s) {
+ warn(select, '`select` has been obsoleted. Just use `get`. See CHANGELOG for details.');
+ return get(l, s);
+ });
- var select = /*#__PURE__*/selectAs(id);
+ var selectAs = I.curry(function selectAs(f, l, s) {
+ warn(selectAs, '`selectAs` has been obsoleted. Just use `getAs`. See CHANGELOG for details.');
+ return getAs(f, l, s);
+ });
var sumAs = /*#__PURE__*/traverse(Sum);
var sum = /*#__PURE__*/sumAs(unto0);
- // Operations on lenses
-
- function get(l, s) {
- return 1 < arguments.length ? getU(l, s) : function (s) {
- return getU(l, s);
- };
- }
-
// Creating new lenses
var lens = /*#__PURE__*/I.curry(lensU);
@@ -1869,7 +1855,7 @@
var inverse = function inverse(iso) {
return function (x, i, F, xi2yF) {
return F.map(function (x) {
- return getU(iso, x);
+ return getAsU(id, iso, x);
}, xi2yF(setU(iso, x, void 0), i));
};
};
@@ -1903,10 +1889,10 @@
}
n = xs.length;
var j = 0;
- for (var _i3 = 0; _i3 < n; ++_i3) {
- var x = xs[_i3];
+ for (var _i4 = 0; _i4 < n; ++_i4) {
+ var x = xs[_i4];
if (void 0 !== x) {
- if (_i3 !== j) xs[j] = x;
+ if (_i4 !== j) xs[j] = x;
++j;
}
}
@@ -1940,10 +1926,10 @@
var json = /*#__PURE__*/(res(function (iso) {
return toFunction([iso, isoU(deepFreeze, id)]);
}))(function json(options) {
- var _ref2 = options || I.object0,
- reviver = _ref2.reviver,
- replacer = _ref2.replacer,
- space = _ref2.space;
+ var _ref3 = options || I.object0,
+ reviver = _ref3.reviver,
+ replacer = _ref3.replacer,
+ space = _ref3.space;
return isoU(expect(I.isString, function (text) {
return JSON.parse(text, reviver);
@@ -2024,7 +2010,7 @@
// Interop
var pointer = function pointer(s) {
- if (s[0] === '#') s = getU(uriComponent, s);
+ if (s[0] === '#') s = getAsU(id, uriComponent, s);
var ts = s.split('/');
var n = ts.length;
for (var i = 1; i < n; ++i) {
@@ -2038,7 +2024,7 @@
exports.seemsArrayLike = seemsArrayLike;
exports.Identity = Identity;
exports.IdentityAsync = IdentityAsync;
- exports.Constant = Constant;
+ exports.Select = Select;
exports.toFunction = toFunction;
exports.assign = assign;
exports.modify = modify;
@@ -2054,7 +2040,6 @@
exports.cond = cond;
exports.condOf = condOf;
exports.ifElse = ifElse;
- exports.iftes = iftes;
exports.orElse = orElse;
exports.chain = chain;
exports.choice = choice;
@@ -2067,8 +2052,8 @@
exports.tieIx = tieIx;
exports.joinIx = joinIx;
exports.skipIx = skipIx;
- exports.log = log;
exports.getLog = getLog;
+ exports.log = log;
exports.transform = transform;
exports.transformAsync = transformAsync;
exports.seq = seq;
@@ -2105,6 +2090,8 @@
exports.foldr = foldr;
exports.forEach = forEach;
exports.forEachWith = forEachWith;
+ exports.get = get;
+ exports.getAs = getAs;
exports.isDefined = isDefined;
exports.isEmpty = isEmpty;
exports.joinAs = joinAs;
@@ -2119,11 +2106,10 @@
exports.or = or$1;
exports.productAs = productAs;
exports.product = product;
- exports.selectAs = selectAs;
exports.select = select;
+ exports.selectAs = selectAs;
exports.sumAs = sumAs;
exports.sum = sum;
- exports.get = get;
exports.lens = lens;
exports.getter = getter;
exports.setter = setter;
diff --git a/dist/partial.lenses.min.js b/dist/partial.lenses.min.js
index cf6043da..ccffffcd 100644
--- a/dist/partial.lenses.min.js
+++ b/dist/partial.lenses.min.js
@@ -1 +1 @@
-!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("infestines")):"function"==typeof define&&define.amd?define(["exports","infestines"],r):r(n.L={},n.I)}(this,function(n,b){"use strict";function e(n,r){for(var t=0,u=r.length;t>0&&0<=n||b.isString(n)},Jn=U({map:b.applyU,of:U,ap:b.applyU,chain:b.applyU}),Tn=U({map:N,ap:function(n,r){return N(function(r){return N(function(n){return n(r)},n)},r)},of:U,chain:N}),_n=U({map:b.sndU}),Gn=U(function(n){switch(typeof n){case"string":return en(n);case"number":return fn(n);case"object":return a(0,n);default:return 4===n.length?n:(e=n,S(function(n,r,t,u){return t.map(b.always(n),u(e(n,r),r))}))}var e}),Hn=b.curry(function(n,r,t){return vn([n,Ot(r)],r,t)}),Kn=b.curry(sn),Qn=b.curry(ln),Vn=b.curry(function(n,r){return vn(n,void 0,r)}),Xn=b.curry(vn),Yn=b.curry(K),Zn=function(e){return S(function(n,r,t,u){return Gn(e(n,r))(n,r,t,u)})},nr=U(function(){for(var n=arguments.length,r=ar;n--;){var t=arguments[n];r=t.length<2?Gn(t[0]):zn(Gn(t[1]),r)(t[0])}return r}),rr=U(function(e){e=Gn(e);for(var i=$n,n=arguments.length;--n;){var r=arguments[n];i=1===r.length?b.always(Gn(r[0])):Bn(r[0],Gn(r[1]),i)}return function(n,r,t,u){return e(n,r,_n,i)(n,r,t,u)}}),tr=b.curry(function(n,r,t){return zn(Gn(r),Gn(t))(n)}),ur=U(function(n,r){for(var t=arguments.length,u=1&t?Gn(arguments[--t]):ar;0<=(t-=2);)u=zn(Gn(arguments[t+1]),u)(arguments[t]);return u}),er=b.curry(Cn),ir=b.curry(function(t,n){return[n,Zn(function(n,r){return void 0!==n?t(n,r):ar})]}),or=zn(v,An),cr=zn(An,v),fr=cr(b.isDefined),ar=function(n,r,t,u){return v(n,r,t,u)},vr=b.curry(function(e,i){return i=Gn(i),S(function(n,t,r,u){return i(n,t,r,function(n,r){return u(n,e(r,t))})})}),sr=k(vr(function(n,r){return void 0!==r?void 0!==n?[r,n]:r:n})),lr=k(vr(b.sndU)),dr=b.curry(function(n,r){var t=K(Z,nn,n,r),u=t.c;return console.log.apply(console,X(t.p,[t.m])),u}),pr=b.curry(function(n,r){return sn(n,U,r)}),hr=b.curry(function(n,r){return ln(n,U,r)}),yr=U(function(){var n=arguments.length,r=ar;if(n)for(r=Gn(arguments[--n]);n;)r=Ln(Gn(arguments[--n]),r);return r}),gr=function(e){return function(n,r,t,u){return v(e,r,t,u,e)}},mr=gr(),br=U(b.curryN(2,function(r){return r=Gn(r),function(n){return function n(r,t){var u=O(null);for(var e in t){var i=t[e];u[e]=b.isObject(i)?n(r,i):Gn(i)}return wn(r,u)}(r,n)}})),jr=br(ar),Ar=U(function(n,r,t,u){return Fn(n)?Dn(n,0,t,u):t.of(n)}),Or=k(Gn([qn,Ar])),xr=k(Gn([qn,Ar,0])),wr=U(function(f){return function(r,n,t,u){if(b.isString(r)){var e=t.map;if(f.global){var i=[""];return i.input=r,i.index=0,T===t?function(n,r,t){for(;t=Nn(t,r);){var u=n(kn(t),Sn(t));if(void 0!==u)return u}}(u,f,i):e((c=r,function(n){for(var r="",t=0,u=function(n){for(var r=[];Rn!==n;)r.push(n[0],n[1]),n=n[2];return r}(n),e=u.length-2;-2!==e;e+=-2){var i=u[e];r+=c.slice(t,Sn(i));var o=u[e+1];void 0!==o&&(r+=o),t=En(i)}return r+c.slice(t)}),function(n,r,t,u,e,i){for(var o=t(Rn);i=Nn(i,e);)o=r(r(n(Rn,t(i)),o),u(kn(i),Sn(i)));return o}(e,t.ap,t.of,u,f,i))}var o=r.match(f);if(o)return e(function(n){return r.replace(f,void 0!==n?n:"")},u(o[0],Sn(o)))}var c;return v(r,void 0,t,u)}}),Ir=U(k(wn(An,w))),Ur=U(function(n,r,t,u){return b.isArray(n)?Dn(n,0,t,u):b.isObject(n)?Ir(n,r,t,u):t.of(n)}),kr=U(function(n,r,u,e){return function n(r,t){return b.isArray(r)?Dn(r,0,u,n):void 0!==r?e(r,t):u.of(r)}(n,r)}),Sr=function(i){return function(n,r,u,e){return function n(r,t){return i(r,t)?e(r,t):Ur(r,t,u,n)}(n,r)}},Er=Sr(function(n){return void 0!==n&&!b.isArray(n)&&!b.isObject(n)}),Nr=b.curry(function(t,n,r){return!K(T,function(n,r){if(!t(n,r))return!0},n,r)}),Rr=Nr(U),qr=b.curry(function(t,n,r){return!!K(T,function(n,r){if(t(n,r))return!0},n,r)}),Mr=(0,b.curry)(function(u,n,r){var e=[];return K(T,function(n,r){var t=u(n,r);void 0!==t&&e.push(t)},n,r),e}),Pr=Mr(U),zr=Q(U,function(n){return _(n.concat,n.empty())}),Cr=zr(U),Dr=b.curry(function(t,n,r){return K(G,function(n,r){return t(n,r)?1:0},n,r)}),Lr=Dr(b.isDefined),Wr=b.curry(function(e,n,r){var i=new Map;return K(T,function(n,r){var t=e(n,r),u=i.get(t);i.set(t,void 0!==u?u+1:1)},n,r),i}),$r=Wr(U),Br=b.curry(function(t,u,n,r){return K(T,function(n,r){u=t(u,n,r)},n,r),u}),Fr=b.curry(function(n,r,t,u){var e=[],i=[];K(T,function(n,r){i.push(n),e.push(r)},t,u);for(var o=i.length-1;0<=o;--o)r=n(r,i[o],e[o]);return r}),Jr=b.curry(function(t,n,r){return K(T,function(n,r){t(n,r)},n,r)}),Tr=b.curry(function(n,t,r,u){var e=n();return K(T,function(n,r){t(e,n,r)},r,u),e}),_r=b.curry(function(n,r){return void 0!==K(T,U,n,r)}),Gr=b.curry(function(n,r){return!K(T,b.always(!0),n,r)}),Hr=Q(R,U(function(t){return _(function(n,r){return void 0!==n?void 0!==r?n+t+r:n:r})})),Kr=Hr(U),Qr=H(j),Vr=Qr(U),Xr=b.curry(function(u,n,r){var e=0,i=0;return K(T,function(n,r){var t=u(n,r);void 0!==t&&(i+=1,e+=t)},n,r),e/i}),Yr=Xr(U),Zr=H(m),nt=Zr(U),rt=b.curry(function(t,n,r){return!K(T,function(n,r){if(t(n,r))return!0},n,r)}),tt=qr(U),ut=Yn(_(d,1)),et=ut(P(1)),it=Yn(T),ot=it(U),ct=Yn(G),ft=ct(z),at=b.curry(pn),vt=at(U),st=b.curry(function(n,r){return pn(n(r),Xn(r))}),lt=U(function(e){var i=P(e);return function(n,r,t,u){return t.map(i,u(void 0!==n?n:e,r))}}),dt=U(function(n){return wt(n,void 0)}),pt=function(e){return function(n,r,t,u){return u(void 0!==n?e(n,r):n,r)}},ht=function(e){return function(n,r,t,u){return t.map(function(n){return void 0!==n?e(n,r):n},u(n,r))}},yt=U(function(o){return function(n,r,t,u){var e=void 0,i=b.array0;return Fn(n)&&Un(o,n,e=[],i=[]),t.map(function(n){var r=n?n.length:0,t=i.length,u=r+t;return u===t?i:J(J(Array(u),0,n,0,r),r,i,0,t)},u(e,r))}}),gt=U,mt=Zn(function(n){return Fn(n)&&n.length?n.length-1:0}),bt=(0,b.curry)(function(f,a){return function(u,n,r,t){var e=Fn(u),i=e&&u.length,o=q(0,i,0,f),c=q(o,i,i,a);return r.map(function(n){var r=n?n.length:0,t=o+r;return J(J(J(Array(i-c+t),0,u,0,o),o,n,0,r),t,u,c,i)},t(e?J(Array(Math.max(0,c-o)),0,u,o,c):void 0,n))}}),jt=function(n){return b.isObject(n)?xt(Kn(Ir,Wn,n)):n},At=U,Ot=function(n){return r.apply(null,b.keys(n))},xt=U(function(e){return function(r,n,t,u){return t.map(function(n){return bn(e,n,r)},u(mn(e,r),n))}}),wt=b.curry(function(e,i){function o(n){return In(i,e,n)}return function(n,r,t,u){return t.map(o,u(In(e,i,n),r))}}),It=b.curry(hn),Ut=hn(C,C),kt=hn(L(Fn,U(function(n){for(var r=n.length,t=Array(r),u=0;uL.choices(optic, ...optics) ~> optic
v11.10.0
L.choose((maybeValue, index) => optic) ~> optic
v1.0.0L.cond(...[(maybeValue, index) => testable, consequentOptic][, [alternativeOptic]]) ~> optic
v13.1.0L.condOf(lens, ...[(maybeValue, index) => testable, consequentOptic][, [alternativeOptic]]) ~> optic
v13.5.0L.condOf(traversal, ...[(maybeValue, index) => testable, consequentOptic][, [alternativeOptic]]) ~> optic
v13.5.0L.ifElse((maybeValue, index) => testable, optic, optic) ~> optic
v13.1.0L.iftes((maybeValue, index) => testable, consequentOptic, ...[, alternativeOptic]) ~> optic
v11.14.0L.orElse(backupOptic, primaryOptic) ~> optic
v2.1.0
-
-L.chain((value, index) => optic, optic) ~> optic
v3.1.0L.choice(...optics) ~> optic
v2.1.0L.optional ~> optic
v3.7.0L.unless((maybeValue, index) => testable) ~> optic
v12.1.0L.when((maybeValue, index) => testable) ~> optic
v5.2.0L.zero ~> optic
v6.0.0
L.joinIx(optic) ~> optic
v13.15.0L.mapIx((index, maybeValue) => index) ~> optic
v13.15.0 ≡ Internals
-
L.Constant ~> Functor
v13.7.0L.Identity ~> Monad
v13.7.0L.IdentityAsync ~> Monadish
v13.12.0L.Select ~> Applicative
v14.0.0L.toFunction(optic) ~> optic
v7.0.0 ≡ Transforming
-
@@ -184,6 +174,15 @@ L.assignOp(object) ~> optic
v11.13.0L.modifyOp((maybeValue, index) => maybeValue) ~> optic
v11.7.0L.removeOp ~> optic
v11.7.0L.setOp(maybeValue) ~> optic
v11.7.0L.assignOp(object) ~> traversal
v11.13.0L.modifyOp((maybeValue, index) => maybeValue) ~> traversal
v11.7.0L.removeOp ~> traversal
v11.7.0L.setOp(maybeValue) ~> traversal
v11.7.0 ≡
L.values ~> traversal
v7.3.0
+L.forEach((maybeValue, index) => undefined, traversal, maybeData) ~> undefined
v11.20.0L.forEachWith(() => context, (context, maybeValue, index) => undefined, traversal, maybeData) ~> context
v13.4.0L.get(traversal, maybeData) ~> maybeValue
v2.2.0L.getAs((maybeValue, index) => maybeValue, traversal, maybeData) ~> maybeValue
v14.0.0L.isDefined(traversal, maybeData) ~> boolean
v11.8.0L.isEmpty(traversal, maybeData) ~> boolean
v11.5.0L.join(string, traversal, maybeData) ~> string
v11.2.0 ≡
L.or(traversal, maybeData) ~> boolean
v9.6.0
L.product(traversal, maybeData) ~> number
v7.2.0L.productAs((maybeValue, index) => number, traversal, maybeData) ~> number
v11.2.0L.select(traversal, maybeData) ~> maybeValue
v9.8.0L.selectAs((maybeValue, index) => maybeValue, traversal, maybeData) ~> maybeValue
v9.8.0L.select(traversal, maybeData) ~> maybeValue
v9.8.0L.selectAs((maybeValue, index) => maybeValue, traversal, maybeData) ~> maybeValue
v9.8.0L.sum(traversal, maybeData) ~> number
v7.2.0L.sumAs((maybeValue, index) => number, traversal, maybeData) ~> number
v11.2.0 ≡ Lenses
-
-
-L.get(lens, maybeData) ~> maybeValue
v2.2.0
@@ -526,7 +523,8 @@
≡ undefined:
L.get(textIn('fi'), undefined)
-With partial lenses, undefined
is the equivalent of non-existent.
With partial lenses, undefined
is the equivalent of
+non-existent.
As with ordinary lenses, we can use the same lens to update titles:
L.set(textIn('en'), 'The title', sampleTitles)
@@ -791,12 +789,12 @@ ≡ L.prop
lens, to be used in a wider range of
-situations than corresponding total optics.
+expectation of an optic, then the input is treated as being undefined
, which
+is the equivalent of non-existent: reading through the optic
+gives undefined
and writing through the optic replaces the focus with the
+written value. This makes the optics in this library partial and allows
+specific partial optics, such as the simple L.prop
lens, to be used
+in a wider range of situations than corresponding total optics.
Making all optics partial has a number of consequences. For one thing, it can
potentially hide bugs: an incorrectly specified optic treats the input as
undefined
and may seem to work without raising an error. We have not found
@@ -909,7 +907,7 @@
≡ Querying
L.choice(...optics)
and L.chain(value => optic, optic)
-MonadPlus over optics
+MonadPlus over traversals
Picking
@@ -1218,13 +1216,26 @@ ≡ L.cond(..., [R.T, alternative])
because in the latter case L.cond
cannot determine that a user defined
predicate will always be true and has to construct a more expensive optic.
+Note that when no [alternative]
is specified, L.cond
returns a
+traversal, because the default L.zero
is a
+traversal.
Note that L.cond
can be implemented using L.choose
, but not
vice versa. L.choose
not only allows the optic to be chosen
dynamically, but also allows the optic to be constructed dynamically and using
the data at the focus.
- ≡ ■ L.condOf(lens, ...[(maybeValue, index) => testable, consequentOptic][, [alternativeOptic]]) ~> optic
v13.5.0
+ ≡ ■ L.condOf(traversal, ...[(maybeValue, index) => testable, consequentOptic][, [alternativeOptic]]) ~> optic
v13.5.0
L.condOf
is like L.cond
except the first argument to L.condOf
-is a lens to get the parameters for the predicates from the underlying view.
+is a traversal whose focuses are tested with the predicates.
+L.condOf(traversal,
+ [ predicate, consequent ]
+ , ...
+ [ , [ alternative ] ] )
+L.condOf
acts like the consequent optic of first [predicate, consequent]
+pair whose predicate accepts any focus produced by the traversal.
+The last argument to L.condOf
can be an [alternative]
singleton, where the
+alternative is an optic to be used in case none of the predicates accepts
+any focus produced by the traversal. If there is no [alternative]
+L.zero
is used.
For example:
L.get(
L.condOf(
@@ -1235,6 +1246,12 @@ ≡ L.any
and L.cond
: L.cond([L.any(p1, t),
+o1], ..., [L.any(pN, t), oN], [o])
.
+
Note that when no [alternative]
is specified, L.condOf
returns a
+traversal, because the default L.zero
is a
+traversal.
≡ ■ L.ifElse((maybeValue, index) => testable, optic, optic) ~> optic
v13.1.0
L.ifElse
creates an optic whose operation is selected based on the given
predicate from the two given optics. If the predicate is truthy on the value at
@@ -1245,102 +1262,12 @@
≡ L.modify(L.ifElse(Array.isArray, L.elems, L.values), R.inc, {x: 1, y: 2, z: 3})
- ≡ ■ L.iftes((maybeValue, index) => testable, consequentOptic, ...[, alternativeOptic]) ~> optic
v11.14.0
-WARNING: L.iftes
has been obsoleted. Use L.ifElse
or
-L.cond
instead. See CHANGELOG for
-details.
-L.iftes
creates an optic whose operation is selected from the given optics and
-predicates on the underlying view.
-L.iftes( predicate, consequent
- [ , ... ]
- [ , alternative ] )
-L.iftes
is not curried unlike most functions in this library. L.iftes
-requires at least two arguments and successive arguments form predicate -
-consequent pairs. The predicates are functions on the underlying view and are
-tested sequentially. The consequents are optics and L.iftes
acts like the
-consequent corresponding to the first predicate that returns true. If L.iftes
-is given an odd number of arguments, the last argument is the alternative
-taken in case none of the predicates returns true. If all predicates return
-false and there is no alternative, L.iftes
acts like L.zero
.
-For example:
-var minorAxis = L.iftes(({x, y} = {}) => Math.abs(y) < Math.abs(x), 'y', 'x')
-
-L.get(minorAxis, {x: -3, y: 1})
-
-L.modify(minorAxis, R.negate, {x: -3, y: 1})
-
-Note that L.iftes
can be implemented using L.choose
.
≡ ■ L.orElse(backupOptic, primaryOptic) ~> optic
v2.1.0
L.orElse(backupOptic, primaryOptic)
acts like primaryOptic
when its view is
not undefined
and otherwise like backupOptic
.
Note that L.choice(...optics)
is equivalent to
optics.reduceRight(L.orElse, L.zero)
and L.choices(...optics)
is equivalent to optics.reduce(L.orElse)
.
- ≡ ■ Querying
-Querying combinators allow one to use optics to query data structures. Querying
-is distinguished from adapting in that querying defaults to an
-empty or read-only zero.
- ≡ ■ L.chain((value, index) => optic, optic) ~> optic
v3.1.0
-L.chain
provides a monadic
-chain
-combinator for querying with optics. L.chain(toOptic, optic)
is equivalent to
-L.compose(
- optic,
- L.choose(
- (value, index) => value === undefined ? L.zero : toOptic(value, index)
- )
-)
-Note that with the R.always
, L.chain
,
-L.choice
and L.zero
combinators, one can consider
-optics as subsuming the maybe monad.
- ≡ ■ L.choice(...optics) ~> optic
v2.1.0
-L.choice
returns a partial optic that acts like the first of the given optics
-whose view is not undefined
on the given data structure. When the views of
-all of the given optics are undefined
, the returned optic acts like
-L.zero
, which is the identity element of L.choice
. See also
-L.choices
.
-For example:
-L.modify([L.elems, L.choice('a', 'd')], R.inc, [{R: 1}, {a: 1}, {d: 2}])
-
- ≡ ■ L.optional ~> optic
v3.7.0
-L.optional
is an optic over an optional element. When used as a traversal,
-and the focus is undefined
, the traversal is empty. When used as a lens, and
-the focus is undefined
, the lens will be read-only.
-As an example, consider the difference between:
-L.set([L.elems, 'x'], 3, [{x: 1}, {y: 2}])
-
-and:
-L.set([L.elems, 'x', L.optional], 3, [{x: 1}, {y: 2}])
-
-Note that L.optional
is equivalent to L.when(x => x !==
-undefined)
.
- ≡ ■ L.unless((maybeValue, index) => testable) ~> optic
v12.1.0
-L.unless
allows one to selectively skip elements within a traversal or to
-selectively turn a lens into a read-only lens whose view is undefined
. See
-also L.when
.
-For example:
-L.modify([L.elems, L.unless(x => x < 0)], R.negate, [0, -1, 2, -3, 4])
-
- ≡ ■ L.when((maybeValue, index) => testable) ~> optic
v5.2.0
-L.when
allows one to selectively skip elements within a traversal or to
-selectively turn a lens into a read-only lens whose view is undefined
. See
-also L.unless
.
-For example:
-L.modify([L.elems, L.when(x => x > 0)], R.negate, [0, -1, 2, -3, 4])
-
-Note that L.when(p)
is equivalent to L.choose((x, i) => p(x, i) ?
-L.identity : L.zero)
.
- ≡ ■ L.zero ~> optic
v6.0.0
-L.zero
is the identity element of L.choice
and
-L.chain
. As a traversal, L.zero
is a traversal of no elements
-and as a lens, i.e. when used with L.get
, L.zero
is a read-only
-lens whose view is always undefined
.
-For example:
-L.collect(
- [L.elems, L.cond([R.is(Array), L.elems], [R.is(Object), 'x'], [L.zero])],
- [1, {x: 2}, [3, 4]]
-)
-
≡ ■ Indices
The indexing combinators allow one to manipulate the indices passed down by
optics. Although optics do not construct paths by default one
@@ -1422,7 +1349,7 @@
≡ L.log
, is intended for debugging.
For example:
-L.getLog(['x', 0, 'y'], {x: [{y: 101}]})
+L.getLog(['data', L.elems, 'y'], {data: [{x: 1}, {y: 2}]})
(If you are looking at the above snippet in the interactive version of this
page, then note that the
@@ -1443,12 +1370,6 @@
≡ L.set(['x', L.log('%s x: %j')], '11', {x: 10})
≡ ■ Internals
- ≡ ■ L.Constant ~> Functor
v13.7.0
-L.Constant
is the Static
-Land
-compatible constant
-Functor
-definition used by Partial Lenses.
≡ ■ L.Identity ~> Monad
v13.7.0
L.Identity
is the Static
Land
@@ -1462,6 +1383,31 @@
≡ ≡ ■ L.Select ~> Applicative
v14.0.0
+L.Select
is the Static
+Land
+compatible
+Applicative
+definition that extends the constant functor to select the first non-undefined
+element.
+The basis for Select
is the following
+monoid
+over JavaScript values:
+var Defined = {
+ empty: _ => undefined,
+ concat: (l, r) => l !== undefined ? l : r
+}
+It is a monoid, because it satisfies the Monoid laws:
+var MonoidLaws = (M, x, y, z) => ({
+ associativity: test(M.concat(M.concat(x, y), z), M.concat(x, M.concat(y, z))),
+ leftIdentity: test(M.concat(M.empty(), x), x) ,
+ rightIdentity: test(M.concat(x, M.empty()), x)
+})
+
+MonoidLaws(Defined, {Try: 'any'}, 'JavaScript', ['values'])
+
+In Partial Lenses undefined
is used to representing
+nothingness.
≡ ■ L.toFunction(optic) ~> optic
v7.0.0
L.toFunction
converts a given optic, which can be a string, an
integer, an array, or a function to an optic function.
@@ -1584,16 +1530,17 @@ ≡ querying monad.
≡ ■ Transforming
- ≡ ■ L.assignOp(object) ~> optic
v11.13.0
-
L.assignOp
creates an optic that merges the given object into the object in
-focus.
+ ≡ ■ L.assignOp(object) ~> traversal
v11.13.0
+L.assignOp
creates a transform that merges the given object into the object in
+focus. When used as a traversal, L.assignOp
acts as a traversal of no
+elements. Usually, however, L.assignOp
is used within
+transforms.
For example:
L.transform([L.elems, L.assignOp({y: 1})], [{x: 3}, {x: 4, y: 5}])
- ≡ ■ L.modifyOp((maybeValue, index) => maybeValue) ~> optic
v11.7.0
-L.modifyOp
creates an optic that maps the focus with the given function. When
-used as a traversal, L.modifyOp
acts as a traversal of no elements. When used
-as a lens, L.modifyOp
acts as a read-only lens whose view is the mapped focus.
+
≡ ■ L.modifyOp((maybeValue, index) => maybeValue) ~> traversal
v11.7.0
+L.modifyOp
creates a transform that maps the focus with the given function.
+When used as a traversal, L.modifyOp
acts as a traversal of no elements.
Usually, however, L.modifyOp
is used within transforms.
For example:
L.transform(
@@ -1605,7 +1552,7 @@ ≡ ≡ ■ L.removeOp ~> optic
v11.7.0
+ ≡ ■ L.removeOp ~> traversal
v11.7.0
L.removeOp
is shorthand for L.setOp(undefined)
.
Here is an example based on a question from a user:
var sampleToFilter = {
@@ -1630,7 +1577,7 @@ ≡ ≡ ■ L.setOp(maybeValue) ~> optic
v11.7.0
+ ≡ ■ L.setOp(maybeValue) ~> traversal
v11.7.0
L.setOp(x)
is shorthand for L.modifyOp(R.always(x))
.
≡ ■ Traversals
A traversal operates over a collection of non-overlapping focuses that are
@@ -1777,8 +1724,8 @@
≡ select:
-L.select(L.query(L.when(R.propEq('language', 'sv')), 'text'), sampleTitles)
+And one can also view the text of a specific language:
+L.get(L.query(L.when(R.propEq('language', 'sv')), 'text'), sampleTitles)
Like CSS selectors, L.query
can be quite convenient, but should be used with
care. The search for matching elements can be expensive and specifying a query
@@ -1816,6 +1763,67 @@
≡ L.branchOr([], {})
.
+ ≡ ■ Querying
+
Querying combinators allow one to use optics to query data structures. Querying
+is distinguished from adapting in that querying defaults to an
+empty or read-only zero.
+ ≡ ■ L.chain((value, index) => optic, optic) ~> traversal
v3.1.0
+L.chain
provides a monadic
+chain
+combinator for querying with optics. L.chain(toOptic, optic)
is equivalent to
+L.compose(
+ optic,
+ L.choose(
+ (value, index) => value === undefined ? L.zero : toOptic(value, index)
+ )
+)
+Note that with the R.always
, L.chain
,
+L.choice
and L.zero
combinators, one can consider
+optics as subsuming the maybe monad.
+ ≡ ■ L.choice(...optics) ~> traversal
v2.1.0
+L.choice
returns a partial optic that acts like the first of the given optics
+whose view is not undefined
on the given data structure. When the views of
+all of the given optics are undefined
, the returned optic acts like
+L.zero
, which is the identity element of L.choice
. See also
+L.choices
.
+For example:
+L.modify([L.elems, L.choice('a', 'd')], R.inc, [{R: 1}, {a: 1}, {d: 2}])
+
+ ≡ ■ L.optional ~> traversal
v3.7.0
+L.optional
is an optic over an optional element. When used as a traversal,
+and the focus is undefined
, the traversal is empty. When used as a lens, and
+the focus is undefined
, the lens will be read-only.
+As an example, consider the difference between:
+L.set([L.elems, 'x'], 3, [{x: 1}, {y: 2}])
+
+and:
+L.set([L.elems, 'x', L.optional], 3, [{x: 1}, {y: 2}])
+
+Note that L.optional
is equivalent to L.when(x => x !==
+undefined)
.
+ ≡ ■ L.unless((maybeValue, index) => testable) ~> traversal
v12.1.0
+L.unless
allows one to selectively skip elements within a traversal. See also
+L.when
.
+For example:
+L.modify([L.elems, L.unless(x => x < 0)], R.negate, [0, -1, 2, -3, 4])
+
+ ≡ ■ L.when((maybeValue, index) => testable) ~> traversal
v5.2.0
+L.when
allows one to selectively skip elements within a traversal. See also
+L.unless
.
+For example:
+L.modify([L.elems, L.when(x => x > 0)], R.negate, [0, -1, 2, -3, 4])
+
+Note that L.when(p)
is equivalent to L.choose((x, i) => p(x, i) ?
+L.identity : L.zero)
.
+ ≡ ■ L.zero ~> traversal
v6.0.0
+L.zero
is a traversal of no elements and is the identity element of
+L.choice
and L.chain
.
+For example:
+L.collect(
+ [L.elems, L.cond([R.is(Array), L.elems], [R.is(Object), 'x'], [L.zero])],
+ [1, {x: 2}, [3, 4]]
+)
+
≡ ■ Folds over traversals
≡ ■ L.all((maybeValue, index) => testable, traversal, maybeData) ~> boolean
v9.6.0
L.all
determines whether all of the elements focused on by the given traversal
@@ -1828,7 +1836,7 @@
≡ L.any
, L.none
, and
-L.selectAs
.
+L.getAs
.
≡ ■ L.and(traversal, maybeData) ~> boolean
v9.6.0
L.and
determines whether all of the elements focused on by the given traversal
are truthy.
@@ -1844,7 +1852,7 @@ ≡ L.any(x => x > 5, primitives, [[[1], 2], {y: 3}, [{l: 4, r: [5]}, {x: 6}]])
See also: L.all
, L.none
, and
-L.selectAs
.
+L.getAs
.
≡ ■ L.collect(traversal, maybeData) ~> [...values]
v3.6.0
L.collect
returns an array of the non-undefined
elements focused on by the
given traversal or lens from a data structure.
@@ -1961,6 +1969,34 @@ ≡ L.forEachWith(() => new Map(), (m, v, k) => m.set(k, v), L.values, {x: 2, y: 1})
Note that a new Map
is returned each time the above expression is evaluated.
+ ≡ ■ L.get(traversal, maybeData) ~> maybeValue
v9.8.0
+L.get
returns the element focused on by a lens from a data
+structure or goes lazily over the elements focused on by the given
+traversal and returns the first non-undefined
element. See
+also L.getLog
.
+For example:
+L.get('y', {x: 112, y: 101})
+
+L.get([L.elems, 'y'], [{x:1}, {y:2}, {z:3}])
+
+Note that L.get
is equivalent to L.getAs(x => x)
.
+ ≡ ■ L.getAs((maybeValue, index) => maybeValue, traversal, maybeData) ~> maybeValue
v14.0.0
+L.getAs
goes lazily over the elements focused on by the given traversal,
+applying the given function to each element, and returns the first
+non-undefined
value returned by the function.
+L.getAs(x => x > 3 ? -x : undefined, L.elems, [3, 1, 4, 1, 5])
+
+L.getAs
operates lazily. The user specified function is only applied to
+elements until the first non-undefined
value is returned and after that
+L.getAs
returns without examining more elements.
+Note that L.getAs
can be used to implement many other operations over
+traversals such as finding an element matching a predicate and checking whether
+all/any elements match a predicate. For example, here is how you could
+implement a for all predicate over traversals:
+var all = (p, t, s) => !L.getAs(x => p(x) ? undefined : true, t, s)
+Now:
+all(x => x < 9, primitives, [[[1], 2], {y: 3}, [{l: 4, r: [5]}, {x: 6}]])
+
≡ ■ L.isDefined(traversal, maybeData) ~> boolean
v11.8.0
L.isDefined
determines whether or not the given traversal focuses on any
non-undefined
element on the given data structure. When used with a lens,
@@ -2039,7 +2075,7 @@
≡ L.none(x => x > 5, primitives, [[[1], 2], {y: 3}, [{l: 4, r: [5]}, {x: 6}]])
-See also: L.all
, L.any
, and L.selectAs
.
+See also: L.all
, L.any
, and L.getAs
.
≡ ■ L.or(traversal, maybeData) ~> boolean
v9.6.0
L.or
determines whether any of the elements focused on by the given traversal
is truthy.
@@ -2063,13 +2099,17 @@ ≡ ≡ ■ L.select(traversal, maybeData) ~> maybeValue
v9.8.0
+ ≡ ■ L.select(traversal, maybeData) ~> maybeValue
v9.8.0
+WARNING: L.select
has been obsoleted. Just use L.get
. See
+CHANGELOG for details.
L.select
goes lazily over the elements focused on by the given traversal and
returns the first non-undefined
element.
L.select([L.elems, 'y'], [{x:1}, {y:2}, {z:3}])
Note that L.select
is equivalent to L.selectAs(x => x)
.
- ≡ ■ L.selectAs((maybeValue, index) => maybeValue, traversal, maybeData) ~> maybeValue
v9.8.0
+ ≡ ■ L.selectAs((maybeValue, index) => maybeValue, traversal, maybeData) ~> maybeValue
v9.8.0
+WARNING: L.selectAs
has been obsoleted. Just use L.getAs
.
+See CHANGELOG for details.
L.selectAs
goes lazily over the elements focused on by the given traversal,
applying the given function to each element, and returns the first
non-undefined
value returned by the function.
@@ -2103,14 +2143,6 @@ ≡ ≡ ■ Lenses
Lenses always have a single focus which can be viewed directly. Put
in another way, a lens specifies a path to a single element in a data structure.
- ≡ ■ Operations on lenses
- ≡ ■ L.get(lens, maybeData) ~> maybeValue
v2.2.0
-L.get
returns the element focused on by a lens from a data
-structure.
-For example:
-L.get('y', {x: 112, y: 101})
-
-Note that L.get
does not work on traversals.
≡ ■ Creating new lenses
≡ ■ L.lens((maybeData, index) => maybeValue, (maybeValue, maybeData, index) => maybeData) ~> lens
v1.0.0
L.lens
creates a new primitive lens. The first parameter is the getter and
@@ -3127,8 +3159,8 @@
≡ ≡ ■ Deepening topics
- ≡ ■ Understanding L.filter
, L.find
, L.select
, and L.when
-
The L.filter
, L.find
, L.select
, and
+
≡ ■ Understanding L.filter
, L.find
, L.get
, and L.when
+The L.filter
, L.find
, L.get
, and
L.when
serve related, but different, purposes and it is important
to understand their differences in order to make best use of them.
Here is a table of their call patterns and type signatures:
@@ -3148,8 +3180,8 @@
L.find: ((Maybe a, Index) -> Boolean) -> PLens [a] a
-L.select(traversal, data) ~> value
-L.select: PTraversal s a -> Maybe s -> Maybe a
+L.get(traversal, data) ~> value
+L.get: PTraversal s a -> Maybe s -> Maybe a
L.when((value, index) => bool) ~> optic
@@ -3157,24 +3189,23 @@
As can be read from above, both L.filter
and L.find
-introduce lenses, L.select
eliminates a traversal, and
+introduce lenses, L.get
eliminates a traversal, and
L.when
introduces an optic, which will always be a traversal in
this section. We can also read that L.filter
and
-L.find
operate on arrays, while L.select
and
+L.find
operate on arrays, while L.get
and
L.when
operate on arbitrary traversals. Yet another thing to make
-note of is that both L.find
and L.select
are
-many-to-one while both L.filter
and L.when
retain
-cardinality.
+note of is that both L.find
and L.get
are many-to-one
+while both L.filter
and L.when
retain cardinality.
The following equations relate the operations in the read direction:
L.get([L.filter(p), 0]) = L.get(L.find(p))
- L.select([L.elems, L.when(p)]) = L.get(L.find(p))
+ L.get([L.elems, L.when(p)]) = L.get(L.find(p))
L.collect([L.elems, L.when(p)]) = L.get(L.filter(p))
In the write direction there are no such simple equations.
L.find
can be used to create a bidirectional view of an element in
an array identified by a given predicate. Despite the name, L.find
is probably not what one should use to generally search for something in a data
structure.
-L.select
(and L.selectAs
) can be used to search
+
L.get
(and L.getAs
) can be used to search
for an element in a data structure following an arbitrary traversal. That
traversal can, of course, also make use of L.when
to filter elements
or to limit the traversal.
@@ -3314,16 +3345,36 @@ ≡ immutable
collections and back).
≡ ■ Use of undefined
-
undefined
is a natural choice in JavaScript, especially when dealing with
-JSON, to represent nothingness. Some libraries use null
, but that is arguably
-a poor choice, because null
is a valid JSON value. Some libraries implement
-special Maybe
types, but the benefits do not seem worth the trouble. First of
-all, undefined
already exists in JavaScript and is not a valid JSON value.
-Inventing a new value to represent nothingness doesn't seem to add much. OTOH,
-wrapping values with Just
objects introduces a significant performance
-overhead due to extra allocations. Operations with optics do not otherwise
-necessarily require large numbers of allocations and can be made highly
-efficient.
+undefined
is arguably a natural choice in JavaScript to represent nothingness:
+
+undefined
is the result of an attempt to access non-existent properties of
+objects.
+undefined
is the result of functions that do not explicitly return another
+value.
+undefined
is not a valid JSON value and does not get mixed up with valid
+JSON values.
+- We can form a monoid over JavaScript values by treating
undefined
as
+zero.
+
+Some libraries use null
, but that is arguably a poor choice, because null
is
+a valid JSON value, which means that when accessing JSON data a result of null
+is ambiguous.
+One downside of using undefined
is that it can sometimes be a valid value.
+Fortunately this is fairly rarely the case so inventing a new value to represent
+nothingness doesn't seem to add much.
+Some libraries implement special Maybe
types, but the benefits do not seem
+worth the trouble or the disadvantages in this context. The main disadvantage
+is that wrapping values with Just
objects introduces a significant performance
+overhead due to extra allocations, because operations with optics do not
+otherwise necessarily require large numbers of allocations and can be made
+highly efficient. Also, a Maybe
+monad
+is not necessary for optics. A
+monoid
+is sufficient for optics based on
+applicatives,
+because applicatives do not have a join operation and are not nested like
+monads.
Not having an explicit Just
object means that dealing with values such as
Just Nothing
requires special consideration.
≡ ■ Allowing strings and integers as optics
@@ -3566,10 +3617,10 @@ ≡ ≡