monocle-ts icon indicating copy to clipboard operation
monocle-ts copied to clipboard

`Lens#composeAt` method?

Open wbadart opened this issue 3 years ago • 0 comments

🚀 Feature request

Current Behavior

It seems that most of the optics include a couple of methods for composing each of the other kinds of optics in order to drill down into the structure another level. Lens, for example, has

  • compose
  • composeIso
  • composeLens
  • composeOptional
  • composePrism
  • composeTraversal

I was wondering: is there a technical or theoretical limitation that prevents composeAt from being implemented? Or just a question of resources? (If the latter, I'm happy to take a stab at the implementation).

Who does this impact? Who is this for?

I bring it up since it's personally useful, but I suspect that since At is such a useful tool, I won't be the last person to want to use it in a more deeply-nested setting. I'm not sure where At (and composing it with other optics) falls on the beginner-advanced spectrum, though it seems to be a pretty important tool for working with immutable maps/ records. (Do any of the other optics allow you to "delete" keys? If so, that could be the basis of another workaround.)

Describe alternatives you've considered

For context, I have a structure like this:

interface Foo {
    bars: Readonly<Record<string, Bar>>
}

And am trying to write something like

import * as A from 'monocle-ts/At';
import * as L from 'monocle-ts/Lens';
import { some, none } from 'fp-ts/Option';

const FooL = {
    barAt: pipe(L.id<Foo>(), L.prop('bars'), L.composeAt(A.atReadonlyRecord()),
}

FooL.barAt('a').set(none)({bars: {a: 10, b: 20}})  // {bars: {b: 20}}

So, to bring it back to the workarounds I tried: I wasn't sure where At falls in the optics hierarchy, so I tried using atReadonlyRecord with composeTraversal and composeOptional. As I should have guessed, these didn't typecheck, and I couldn't figure out a straight-forward conversion.

Really, I'm just trying to include Ats in my pipelines. Is there an existing approach to this that would make the addition of composeAt moot?

Many thanks!

wbadart avatar Nov 02 '21 23:11 wbadart