lens icon indicating copy to clipboard operation
lens copied to clipboard

Add prisms or other functionality that tests a lens's applicability

Open kstrafe opened this issue 9 years ago • 4 comments

Lenses can sometimes fail:

(lens-view (hash-ref-lens 'b) #hash((a . 0)))

This is especially troublesome for composed lenses that act on deeply nested structures. I'd love there to be a sort of lens-test? or lens-correct? to test a lens on some data.

kstrafe avatar Feb 27 '17 00:02 kstrafe

Could you describe the composed lens you were working with and a broad overview of what you were trying to do with your data? There's multiple ways of handling this sort of problem, and the extra information would help.

jackfirth avatar Feb 27 '17 02:02 jackfirth

Here are the composed lenses

https://github.com/BourgondAries/elevators/blob/master/source/lenses.rkt

Here is an example usage

https://github.com/BourgondAries/elevators/blob/master/source/utilities.rkt#L160-L162

It is sometimes possible that elevators contains no key (id) that's used in (hash-ref-lens id), and as such, this code crashes.

Ideally it'd be possible to check if the composed lens "works" on the data structure. Another possibility (specifically for hash-ref-lens) is to provide a second function that acts as a fallback/default key-value creator.

kstrafe avatar Feb 27 '17 02:02 kstrafe

If the key isn't present, what would you like to do? Set a default key and continue? Or something else?

jackfirth avatar Feb 27 '17 02:02 jackfirth

In the above case: Log a message and return the elevators structure unedited.

Something along the lines of

(if (lens-works? this:call elevators)
    ... ; do the transformation on elevators
    elevators)

kstrafe avatar Feb 27 '17 02:02 kstrafe