lens icon indicating copy to clipboard operation
lens copied to clipboard

Falsy lenses

Open jackfirth opened this issue 10 years ago • 1 comments

Sometimes it's handy to break the lens laws, just a little bit. Specifically, it might be useful to have a lens that is allowed to "miss" its target, for which viewing returns false and setting is a no-op. There could even be a general function that given a predicate to determine when a lens won't miss, constructs an improper "falsy lens" which obeys the lens laws for all values the predicate returns true for, but for other values always views false and setting does nothing and returns the value unchanged. Currently, the syntax keyword sequence lenses are falsy lenses (but this behavior is undocumented).

jackfirth avatar Jul 08 '15 07:07 jackfirth

Note: This is a special case of the more general notion of monadic lenses, lenses that operate on monadic values instead of plain values. For a given monad type M, the monadic lens function types are (I think):

view :: Lens s a -> s -> M a
set :: Lens s a -> s -> a -> M s
transform :: Lens s a -> s -> (a -> M a) -> M s

In this case the specific monad is like the Maybe monad, but instead of monadic values being a Just wrapper around the plain value or Nothing, monadic values are plain values (except #f) or #f.

jackfirth avatar Aug 30 '15 22:08 jackfirth