With my simple lens implementation i can perform the usual modify, set, get and delete operations:
// Lens type
const Lens = f => ({runLens: f, [Symbol.toStringTag]: "Lens"});
const objLens = map => k =>
Lens(f => o =>
map(x => Object.assign({}, o, {[k]: x})) (f(o[k]))); // object lens
// Id type
const Id = x => ({runId: x, [Symbol.toStringTag]: "Id"});
const idMap = f => tx => Id(f(tx.runId)); // functor
// Const type
const Const = x => ({runConst: x, [Symbol.toStringTag]: "Const"});
const constMap = f => tx => Const(tx.runConst); // functor
// auxiliary function
const _const = x => y => x;
// MAIN
const o = {foo: "abc", bar: 123};
const get = objLens(constMap) ("foo").runLens(x => Const(x)) (o),
set = objLens(idMap) ("bat").runLens(_const(Id(true))) (o),
mod = objLens(idMap) ("foo").runLens(s => Id(s.toUpperCase())) (o),
del = objLens(idMap) ("foo").runLens(_const(Id(null))) (o); //*
console.log("get", get.runConst);
console.log("set", set.runId);
console.log("mod", mod.runId);
console.log("del", del.runId);
However, delete is unsatisfying as I want to delete the whole property rather than merely replacing the value with nothing.
How can I achieve this?
*Please note that i usually would have used a proper Option type to indicate no values.