TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Lenses: Composable Getters and Setters for Functional Programming

89 pointsby ericelliottover 6 years ago

6 comments

kccqzyover 6 years ago
I love lenses and other optics like prisms and folds, but only in a statically typed language like Haskell. It seems to me that it would be very hard to get things right in a dynamically typed language:<p>* First, it&#x27;s possible that different elements of an array or different properties of an object has different shapes. I can&#x27;t imagine how lenses can deal with that in an idiomatic way.<p>* Second, it&#x27;s just difficult to get things right without a compiler&#x27;s help. Forget a `traverse`? Now you are operating nonsense on the wrong level of your nested data structure. Trying to modify an inner structure but you only have a Fold instead of a Traversal? You won&#x27;t know it won&#x27;t work without running it to get some cryptic error.<p>* Furthermore, without types, we obscure the fact that setters and getters are unified: if `a` stands for the part and `s` for the whole, we just have<p><pre><code> (a -&gt; f a) -&gt; (s -&gt; f s) </code></pre> that is, given a way to transform some inner part, we can transform the whole as well. Getters and setters are just supplying a concrete choice of f, namely the const functor and the identity functor. If we pick f to be the identity functor, then with some unwrapping we have<p><pre><code> (a -&gt; a) -&gt; (s -&gt; s) </code></pre> so if there is a function that can modify the part, we have a function that can modify the whole by keeping the rest the same and modify that part. If we pick f to be the const functor, then with some unwrapping we have<p><pre><code> (a -&gt; r) -&gt; (s -&gt; r) </code></pre> so if there is a function that can extract the value from the part, we can extract the value from the whole as well. Without types, this rather elegant unification of getter and setter seems to be lost.
评论 #18750864 未加载
评论 #18751390 未加载
评论 #18751051 未加载
评论 #18757751 未加载
lindigover 6 years ago
In a language like C, the dot &#x27;.&#x27; and the arrow &#x27;-&gt;&#x27; are used for access into structures - both for setting and retrieving values. I think intuitively about a lens as a programmable dot which I can use to compose an access path. (You might have heard that the bind operator in a monad can be thought of as a programmable semicolon - this is similar.) This intuition might mean little if you haven&#x27;t looked at lenses in more detail, but it does make sense once you have.
评论 #18752156 未加载
nobleachover 6 years ago
I recommend this post as well: <a href="https:&#x2F;&#x2F;medium.com&#x2F;@gcanti&#x2F;introduction-to-optics-lenses-and-prisms-3230e73bfcfe" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;@gcanti&#x2F;introduction-to-optics-lenses-and...</a><p>The author of the post is also the author of a TypeScript port of Monocle for Scala.
coldcodeover 6 years ago
Has anyone done a performance analysis on the use of the lens concept in various languages? Given it is essentially duplicating data for every set it would seem to produce a lot of garbage for whatever memory allocator style is in use.
评论 #18752284 未加载
renoxover 6 years ago
In C you don&#x27;t need lens, either you set a member of a struct or you copy the struct and you set a member of the copy, trivial stuff. Why do you need a library for this kind of thing in Haskell?
评论 #18751496 未加载
评论 #18751713 未加载
_fbptover 6 years ago
`set(lens, b, set(lens, store, a)) ≡ set(lens, b, store)`<p>Somehow i sense this code has arguments in the wrong order.