When manipulating immutable datastructures, what's the difference between Clojure's assoc-in and Haskell's lenses?

前端 未结 4 1401
名媛妹妹
名媛妹妹 2021-02-18 22:45

I need to manipulate and modify deeply nested immutable collections (maps and lists), and I\'d like to better understand the different approaches. These two libraries solve more

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-18 23:41

    This question is somewhat analogous to asking what's the difference between Clojure's for and Haskell's monads. I'll mimic the answers so far: sure for is sort of like a List monad, but monads are so much more generic and powerful.

    But, this is somewhat silly, right? Monads have been implemented in Clojure. Why aren't they used all the time? Clojure has at its core a different philosophy about how to handle state, but still feels free to borrow good ideas from great languages like Haskell in its libraries.

    So, sure, assoc-in, get-in, update-in, etc. are sort of like lenses for associative data structures. And there are implementations of lenses in general in Clojure out there. Why aren't they used all the time? It is a difference in philosophy (and perhaps the eerie feeling that with all the setters and getters we'd be making another Java inside Clojure and somehow end up marrying our mother). But, Clojure feels free to borrow good ideas, and you can see lens-inspired approaches making their way into cool projects like Om and Enliven.

    You have to be careful asking such questions, because like half-siblings who occupy some of the same space Clojure and Haskell are bound to be borrowing from each other and squabbling a bit about who is right.

提交回复
热议问题