Skip to content
Speros Kokenes edited this page Mar 7, 2019 · 11 revisions

Below are some sample answers to the exercises at https://calmm-js.github.io/partial.lenses/exercises.html

Lenses

Getter and Setter

const lens = "part";

Nested Objects

const lens = [L.removable("inside"), "inside", L.removable("part"), "part"];

Association List

const valOf = key => [
  L.normalize(R.sortBy(L.get("key"))), 
  L.find(d => d.key === key), 
  L.valueOr({ key }), 
  L.removable("val"), 
  "val"
];

Dimorphic Ranges

const customEnd = L.lens(
   whole => whole.start + whole.num,
   (end, whole) => ({
     start: whole.start,
     num: end - whole.start
   })
);
const end = [L.props("start", "num", "end"), L.choices("end", customEnd)];

Traversals

Xces

const xs = [L.elems, L.removable("x"), "x"];

Nested Properties

const nonObjects = L.lazy(rec => L.ifElse(
    d => typeof d === "object", 
    [L.values, rec], 
    L.identity)
)

Isomorphisms

Inverse Puzzle

const a = L.conjugate(L.json(),L.pick({bar: ["foo", L.array(L.complement)]}));
const b = L.conjugate(L.json(), L.pick({foo: ["bar", L.array(L.complement)]}));
const iso = L.iso(L.get(a), L.get(b));

Transforms

Increment and Decrement

const trn = L.branch({ 
    xs: [L.values, L.modifyOp(d => d+1)], 
    ys: [L.elems, L.modifyOp(d => d-1)]
})