TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Lenses: Composable Getters and Setters for Functional Programming

89 点作者 ericelliott超过 6 年前

6 条评论

kccqzy超过 6 年前
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 未加载
lindig超过 6 年前
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 未加载
nobleach超过 6 年前
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.
coldcode超过 6 年前
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 未加载
renox超过 6 年前
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 未加载
_fbpt超过 6 年前
`set(lens, b, set(lens, store, a)) ≡ set(lens, b, store)`<p>Somehow i sense this code has arguments in the wrong order.