News for hackers. Stuff that matters.
Re:Lenses by aranchelk on Monday June 27, 2022 @05:38AM (#31892376)
A similar statement could be made about monads. Originally used to implement IO in Haskell because it’s a lazy pure functional language, they’re largely unnecessary in strictly evaluated languages.

Monads are now of course seen by Haskellers as being very useful in many different use cases beyond the original motivation.

IO in PureScript, which I’d describe as strictly evaluated Haskell, is still implemented using monads. The biggest benefit of this (as one might expect) is forcing the separation of pure and impure code, but a cool byproduct is asynchronous IO is also implemented in a monad. Various functions are available to launch async from synchronous IO and lift synchronous functions into async. The ergonomics of mixing sync and async are better than any other language I’ve used.

So theres an interesting question: once you have dot syntax, are there reasons you’d still want lenses? I’ve come up with two.

First, it is nice to use “over” (%~) to avoid specifying a location in a data structure twice (first to read it and apply a function to the read value, second to create a new data structure with the read value replaced with the output of the function. The deeper the data structure, the bigger the benefit.

The second case is writing functions that take lenses as an input and operate generically with larger data structures without knowing where in the larger data structure the values of interest reside ahead of time.

Re:Lenses by prionassembly on Monday June 27, 2022 @10:03AM (#31894726)
I started using monads in Python as sort of a gimmick / something to add flavor to a boring project (I was guessing in a final form we'd keep Result types and throw away most other things) but by now they're a boon to productivity and easy to read in fluent notation. Plus they're easy to implement from scratch if you're dependency-phobic (on the rather rickety selection of monad libraries out there, too).
Re:Lenses by yccs27 on Monday June 27, 2022 @12:55PM (#31897326)
What monad library do you use? When I tried using monads in Python a while ago it felt like Python just wasn't made for monadic programming.
Re:Lenses by prionassembly on Tuesday June 28, 2022 @09:35AM (#31907482)
I use basically variations of copy-pasted code from tutorials and (I think) pymonad. I keep reimplementing things because the last API had warts. Maybe one day I'll have a library.