new function lensWhere
See original GitHub issueCan we have a function which uses a predicate to focus on an element in an array?
const lensWhere = (func) => R.lens(
R.find(func),
(focus, target) => R.update(R.findIndex(func, target), focus, target)
)
Example - https://goo.gl/eacsoa
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Composing lenses with Control.Lens where an intermediate ...
It seems I should make a new lens for that. I think I should compose the books and title lenses with an intermediate...
Read more >Functional Lenses, How Do They Work | by Drew Tipson
Ideally, the splicing/setting functions will take three arguments in this order: first a reference to where to splice/set, then the new value, ...
Read more >Quest 2 Left Controller Fails to function & lens bulge
I've bought new batteries, cleaned out the components, ... As for the bulge, there appeared a bulge on the lens where you view...
Read more >The Fn (Function) Buttons
Choose the roles played by the function buttons on the lens (where applicable). Lens Function Button Defaults. The default assignments are: A XF18-120mmF4...
Read more >Data.Lens.Minimal - Hackage
This is a function intended to be used with the with , by , or new functions. ... This is a Lens where...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@CrossEye FYI, I just wrote a short section to the Partial Lenses documentation on a related topic: Myth: Partial Lenses are not lawful. Note that the section is a subsection of the On lens laws section.
@tusharmath:
Again, this behavior is a matter of the underlying implementation. It’s not clear what that behavior should be, I guess, but it’s easy enough to say that since our list is only three elements long, there can simply be no 120th element to focus on. And hence we can make the call that something like
set
doesn’t do anything in this case. (Of course, this does show thatPut-Get
fails here too, and perhaps we need to clean up that implementation.) But that’s not always clear. The 0th element might be more of an interesting case.It would be easier to demonstrate the issue with an analog to
lensPath
/lensProp
than withlensIndex
, because those make missing structures easier to imagine. To make the case here, we can descend to sparse arrays (which Ramda generally does avoid, so it’s not quite clear how much it matters.):Even though this list does not have an element with index 0, the lens that focuses on that spot would allow us to do a
set
. Again, perhaps this is easier withlensPath
:My issue is that no conceivable implementation would allow us to do this sort of thing with an arbitrary predicate. Here we can create an element at index 0 or at path
foo/bar
, but we can’t create one that will satisfy an arbitrary predicate.I think I picked the wrong issue to ease my way back into Ramda, because this is so clear in my head, but I’m having a hard time describing it. One more try:
The problem is that the predicate is not about the structure of the container. It’s about the content of the value at the focus. And since that can change through lens invocations (one of the reasons for lenses, after all) we can’t count on this to function properly as a lens.