通过简单的镜头实现,我可以执行通常的修改,设置,获取和删除操作:
// 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);
但是,delete
令人不满意,因为我想删除整个属性,而不是仅用任何内容替换该值。
我该如何实现?
*请注意,我通常会使用正确的Option
类型来表示没有值。
答案 0 :(得分:2)
这就是我要做的:
// type Lens a b = forall f. Functor f => (b -> f b) -> a -> f a
// newtype Const b a = Const { getConst :: b } deriving Functor
const Const = getConst => ({ getConst, map: _ => Const(getConst) });
// newtype Identity a = Identity { runIdentity :: a } deriving Functor
const Identity = runIdentity => ({ runIdentity, map: f => Identity(f(runIdentity)) });
// remove :: String -> Object -> Object
const remove = key => ({ [key]: _, ...rest }) => rest;
// prop :: String -> Lens Object (Maybe Value)
const prop = key => fun => obj =>
fun(obj.hasOwnProperty(key) ? { fromJust: obj[key] } : null)
.map(data => Object.assign(remove(key)(obj), data && { [key]: data.fromJust }));
// get :: Lens a b -> a -> b
const get = lens => data => lens(Const)(data).getConst;
// modify :: Lens a b -> (b -> b) -> a -> a
const modify = lens => fun => data => lens(x => Identity(fun(x)))(data).runIdentity;
// set :: Lens a b -> b -> a -> a
const set = lens => value => modify(lens)(_ => value);
// del :: Lens a (Maybe b) -> a -> a
const del = lens => set(lens)(null);
// foo :: Lens Object (Maybe Value)
const foo = prop("foo");
console.log(get(foo)({ foo: 10, bar: 20 })); // { fromJust: 10 }
console.log(del(foo)({ foo: 10, bar: 20 })); // { bar: 20 }
可见,像foo
这样的物镜的类型签名为Lens Object (Maybe Value)
。这是有道理的,因为如果您尝试get(foo)({ bar: 20 })
,您将一无所获。 del
函数适用于任何聚焦于可能值并将其值设置为空的透镜(即null
)。
信用额度为showing me的贝尔吉(Bergi),可以对计算出的属性进行模式匹配。
答案 1 :(得分:0)
一种可能的方法是定义一个表示删除的特殊类型:
const Lens = f => ({runLens: f, [Symbol.toStringTag]: "Lens"});
const objLens = map => k =>
Lens(f => o => map(x => {
if (x[Symbol.toStringTag] === "Deleter") {
const p = Object.assign({}, o);
delete p[k];
return p;
}
else
return Object.assign({}, o, {[k]: x});
}) (f(o[k])));
const Id = x => ({runId: x, [Symbol.toStringTag]: "Id"});
const idMap = f => tx => Id(f(tx.runId)); // functor
const _const = x => y => x;
// deletion type
const Deleter =
({get runDeleter() {return Deleter}, [Symbol.toStringTag]: "Deleter"});
// MAIN
const o = {foo: "abc", bar: 123};
const del = objLens(idMap) ("foo").runLens(_const(Id(Deleter))) (o);
console.log("del", del.runId);
但是,_const(Id(Deleter))
并不是特别直观,但看起来有些黑。希望有更好的方法。