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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Lenses

192 点作者 Gadiguibou将近 3 年前

12 条评论

misja111将近 3 年前
Great article. But I&#x27;m wondering, how many of you have used Lenses in larger, or legacy projects? It is this sentence that gives me some doubts:<p>&gt; The formulation is wonky, and very difficult to grasp. Don&#x27;t worry if the intuition hasn&#x27;t kicked in. It turns out that you can use lenses quite a bit without fully grokking them.<p>Whenever I read something like this about a framework, I imagine that sooner or later some colleague will fall in love with the power of the framework too much and write code that is very hard to understand&#x2F;maintain. But I have to admit that I haven&#x27;t worked much with Lenses myself yet so maybe things are not so bad?
评论 #31893101 未加载
评论 #31891409 未加载
评论 #31891564 未加载
评论 #31900316 未加载
评论 #31894374 未加载
评论 #31893004 未加载
评论 #31893780 未加载
评论 #31895495 未加载
评论 #31894919 未加载
评论 #31891463 未加载
评论 #31903109 未加载
评论 #31897144 未加载
评论 #31898933 未加载
the_duke将近 3 年前
Lenses let you do some amazing things, but I think most of their existence is just thanks to how truly horrible (nested) record field access and updates were.<p>This is a lot better now with record dot syntax. [1]<p>[1] <a href="https:&#x2F;&#x2F;ghc-proposals.readthedocs.io&#x2F;en&#x2F;latest&#x2F;proposals&#x2F;0282-record-dot-syntax.html#record-dot-syntax" rel="nofollow">https:&#x2F;&#x2F;ghc-proposals.readthedocs.io&#x2F;en&#x2F;latest&#x2F;proposals&#x2F;028...</a>
评论 #31892376 未加载
评论 #31891576 未加载
评论 #31902925 未加载
m4lvin将近 3 年前
Funny coincidence. Yesterday I watched the talk by SPJ about Lenses and it finally clicked for me :-)<p><a href="https:&#x2F;&#x2F;skillsmatter.com&#x2F;skillscasts&#x2F;4251-lenses-compositional-data-access-and-manipulation" rel="nofollow">https:&#x2F;&#x2F;skillsmatter.com&#x2F;skillscasts&#x2F;4251-lenses-composition...</a><p>Maybe someone has an actual public link to this with no login needed?<p>edit: thanks for the subcomment! <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=k-QwBL9Dia0" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=k-QwBL9Dia0</a>
评论 #31891484 未加载
评论 #31897436 未加载
评论 #31895353 未加载
kqr将近 3 年前
What&#x27;s really neat about lenses to me (more of a practitioner than a theoretician) is that while the level 0 explanation of them is &quot;they&#x27;re like getters and setters in your OOP language&quot; they extend the concept much further.<p>Lenses can be &quot;indirect&quot; or &quot;pseudo&quot; getters or setters, for want of a better term. I.e. they can filter among many fields to set, then modify multiple fields at once, change only non-null values, replace only null values, and so on. So in a sense they&#x27;re like multiple cursors at the code execution level. But they can also contain more complicated transformations if that makes your code simpler (sort of like C# properties.)<p>They are also first class concepts meaning you can pass these accessors around without caring about which object you will use them on.<p>And they compose with each other very nicely.<p>Those three properties make them super powerful and I really with I had them in more languages.<p>----<p>Lenses are the sort of thing where if they had been the default in more languages, people would wonder why on earth anyone would want to have the obviously inferior getters and setters of today&#x27;s OOP languages.
评论 #31891340 未加载
评论 #31891293 未加载
评论 #31892537 未加载
评论 #31891276 未加载
评论 #31896049 未加载
okasaki将近 3 年前
&gt; alice.address.city = &quot;Los Angeles&quot;;<p>&gt; The first issue in Haskell is that we can&#x27;t mutate alice; we instead have to return a new Person value with the updated city.<p>I don&#x27;t get it. Ok, so it can&#x27;t mutate, but why can&#x27;t haskell have an easy syntax and just return a copy with the updated value? Why does it need this very convoluted way for updating records?<p>eg you could have {{ }} denote an &quot;update block&quot;, and this has some special syntax to change values, add or remove items from lists&#x2F;dicts and similar<p><pre><code> let new_alice = {{ alice.address.city = &quot;Los Angeles&quot;, alice.cart.items += 123 }}</code></pre>
评论 #31899726 未加载
评论 #31899173 未加载
评论 #31899252 未加载
评论 #31899314 未加载
malft将近 3 年前
The type looks a little insane, but it&#x27;s just a combination of three mundane hacks that also exist in other languages.<p>The first: If you need a serializer&#x2F;deserializer in a simple language like old-school Java, you can either write two methods for each class, or you can combine them into one method like this:<p>class HoveringSkull inmplements Streamable { String desc; float height; void stream(Stream c) { desc = c.stream(desc); height = c.stream(height); }<p>... and have two different implementations of Streamable. That&#x27;s what the choice of (pro-)functor instance does -- you can swap out the implementation to get &#x27;read&#x27; and &#x27;write&#x27; (and &#x27;deepClone&#x27; and &#x27;equals&#x27; and &#x27;hashCode&#x27; and &#x27;defaultGui&#x27; and &#x27;treeMatch&#x27; and ...)
sriram_malhar将近 3 年前
Good God! This kind of stuff is built for a very different kind of brain than mine, that&#x27;s for sure.<p>I always come away feeling that Haskell makes the simple things tough, and the tough things beyond my reach. For me, it seems so much simpler to deal with in-place single-writer mutation, where aliasing is controlled by the type system, ala Rust or D, or, something like Elm&#x27;s record updating syntax. Even Erlang will do in a pinch.<p>I hold Haskellers&#x27; abilities in high regard, but at the end of the day, I&#x27;m not sure if they produce better software than other equally capable&#x2F;rigorous&#x2F;knowledgeable developers. (Don&#x27;t mean to dredge up the static&#x2F;dynamic typing debate, but given the literature I have read, there is no clear winner).
评论 #31894752 未加载
评论 #31894452 未加载
评论 #31892515 未加载
评论 #31894970 未加载
nephanth将近 3 年前
This is the kind of operations that make me like ocaml&#x27;s down to earth approach<p><pre><code> let new_alice = {alice with address=new_address}</code></pre>
ufo将近 3 年前
Does anyone know what&#x27;s the situation with lenses and type errors these days? Many moons ago, when I played around with lenses, I remember that the type error messages were could get very scary. Paragraphs of inscrutable type errors for a single typo, something that could make even C++ template errors look nice.
评论 #31896709 未加载
评论 #31899346 未加载
Rumudiez将近 3 年前
For anyone looking to play around with lenses outside of Haskell, Ramda JS has a great implementation along with many other FP utilities. Been a happy user for a few years now with several production use cases, all still going strong
infogulch将近 3 年前
hytradboi 2022 hosted a 10m talk [1] about Project Cambria [2] which imagines bi-directional lenses as the basis for deep compatibility between software systems across time and domain.<p>[1]: <a href="https:&#x2F;&#x2F;www.hytradboi.com&#x2F;2022&#x2F;cambria-schema-translations-in-distributed-systems-using-bidirectional-lenses" rel="nofollow">https:&#x2F;&#x2F;www.hytradboi.com&#x2F;2022&#x2F;cambria-schema-translations-i...</a><p>[2]: <a href="https:&#x2F;&#x2F;www.inkandswitch.com&#x2F;cambria&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.inkandswitch.com&#x2F;cambria&#x2F;</a>
norswap将近 3 年前
I&#x27;m kind of befuddled as to why the original &quot;Modifier functions&quot; solution is not better than the &quot;And now we can finally unify our getter and modify lenses into one:&quot; solution.
评论 #31894996 未加载