Skip to content
Vesa Karvonen edited this page Mar 8, 2019 · 11 revisions

Below are some sample answers to the exercises.

Lenses

Getter and Setter *

const lens = L.lens(whole => whole.part, (part, whole) => ({...whole, part}))
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.choices("end", [L.props("start", "num"), customEnd])

Nowadays it is also possible to use L.add and avoid the custom L.lens:

const end = L.choices("end", L.choose(({start}) => ["num", L.add(start)]))

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 iso = L.conjugate(L.json(), L.pick({bar: ["foo", L.array(L.complement)]}))

At the time the exercise was written L.conjugate didn't exist, so one would have written:

const iso = [
    L.json(),
    L.pick({bar: ["foo", L.array(L.complement)]}),
    L.inverse(L.json())
]

Transforms

Increment and Decrement *

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