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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Applicative WTF?

95 点作者 lainon超过 6 年前

7 条评论

mikekchar超过 6 年前
It seems to me that people often overthink applicative. It falls right out of functor. Normally when you call fmap, you pass it a function that takes a single parameter. What happens if you pass it a function that takes <i>more than one</i> parameter (the other parameters being curried)? fmap applies a value from the functor to the function. Since it only passes one parameter, the result is a partially applied function. The result is that you end up with a functor containing partially applied functions.<p>For example, take a list of number and a function that takes 2 parameters, x and y. Maybe the function adds x to y (it doesn&#x27;t matter). Pass these to fmap. Your result will be a list of partially applied functions with x being set to the values in the list. Now we want to apply the y parameter. We need something similar to fmap, but rather than taking a function and a functor, it needs to take a functor containing functions and a functor containing data. It will then apply the data to the functions and you will end up with a functor containing the result.<p>That&#x27;s <i>all</i> applicative is. It applies the subsequent parameters to the result of having run fmap on functions that have more than one parameter. It shows up in a lot of different places, though and is incredibly handy. When I&#x27;m writing FP style code in languages that don&#x27;t normally curry parameters, I often find myself currying parameters precisely because I <i>want</i> applicative ;-). It&#x27;s just super convenient.<p>NB: pure being part of applicative is really interesting. I don&#x27;t know for sure, but I&#x27;m relatively sure that a functor is applicative IFF your can define pure for it, which is really interesting to think about.<p>Edit: weird wording
评论 #18292194 未加载
评论 #18292145 未加载
评论 #18291937 未加载
platz超过 6 年前
&gt; Last time I used Haskell, Applicative wasn&#x27;t even a thing. I had read the McBride and Paterson paper that introduced applicative functors, but that was years ago, and I didn&#x27;t remember any of the details.<p>In the previous post he learns how to use an Applicative.<p>In this post, he learns how to define Applicative for a new type he&#x27;s created.<p>Yes, Applicative is abstract enough that it takes a few concrete examples to get a feel for it and grok what it&#x27;s purpose is.<p>This is the work that&#x27;s required to learn a new abstraction. All abstractions that are sufficiently different from previous experience require this kind of effort to make it legible to you.<p>Once you do, it kind of just &#x27;snaps&#x27; and you get it.<p>Then, you get to leverage that knowledge in all the places it is re-used, instead of having to read the library code doing data manipulation in a bespoke way - you can just look at the type can say &quot;yes, this is an Applicative - i know how to manipulate it and it will accord to some laws i can depend on&quot;, without having to read the source code.
评论 #18287687 未加载
评论 #18290646 未加载
jmct超过 6 年前
The reason GHC won&#x27;t infer the definition of Applicative is because there can be multiple valid `Applicative` instances for a type (unlike `Functor` where there is a unique (non-trivial) instance).<p>The canonical example of this is lists, with 2 valid instances of `Applicative`.<p>The author of the post seems to have this realization, but I wanted to call it out, just in case.
评论 #18318790 未加载
评论 #18290091 未加载
ryani超过 6 年前
Tree is just the free monad over the Pair functor, what&#x27;s the problem?
well1912超过 6 年前
&quot;Digging deeper into why this worked this way was interesting, but it&#x27;s bed time, so I&#x27;m going to cut the scroll here.&quot; I can very roughly intuit why this is-- the difference between the two Applicative instances the author noticed was that the shape of the first tree was being fitted into the second tree vs the shape of the second into the first. So if you &quot;bind&quot; the lefthand side of &lt;* &gt; first, its shape will be &quot;fitted&quot; to the righthand side, and if you do the opposite you get the second &quot;fitted&quot; to the first shape.<p>I believe it&#x27;s the same as iterating over the righthand side of &lt;* &gt; in its implementation first instead of the lefthand side like in their first, wrong implementation?<p>But my reasoning feels loose here. I probably just need to look at their implementation of Monad Tree closer. :-)
Balf超过 6 年前
I find it a little easier to decide what the Applicative instance &quot;should&quot; do by starting with<p>pair :: f a -&gt; f b -&gt; f (a, b)<p>from which you can derive the rest using pure and fmap
评论 #18318794 未加载
picduniya超过 6 年前
I relish, lead to I discovered just what I used to be having a look for. You have ended my four day lengthy hunt! God Bless you man. Have a great day. Bye