Skip to content

Examples of isomorphisms and lenses that currently can't be written compositionally

Vesa Karvonen edited this page Sep 1, 2018 · 5 revisions

Feel free to add examples of complex isomorphisms and lenses that you had to write from scratch using L.iso or L.lens. The idea is to collect examples here so that it would be easier to recognize potential generalized combinators to be added to Partial Lenses.

Selecting items from an object (or array?) based on a list of selected identifiers

By @rikutiira

var selected = [{ group: 'group1', id: 1 }, { group: 'group2', id: 3 }]

var itemsByGroups = {
    group1: [
        { id: 1, name: 'foo' },
        { id: 2, name: 'bar' }
    ],
    group2: [
        { id: 3, name: 'bar2' }
    ]
}

// how to write a lens which gives me a flat array of items based on selected items
var selectedItems = L.get(...)

Potential solution is to use the new L.partsOf lens constructor that allows one to turn a traversal into a lens:

const selectedItems = L.partsOf([
  L.values,
  L.skipIx(L.elems),
  L.when(({id}, group) => R.contains({group, id}, selected)) // <- O(n^2)
])

Try it in this playground.