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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Fold-... and Monoids

103 点作者 ykm4 个月前

13 条评论

quchen4 个月前
Not sure why the article has to mention monads? I mean there’s the (mathematically correct) joke that »monads are monoids in the category of endofunctors«, but understanding that requires getting elbow deep into category theory, and if you’re not one of the maybe 10 people in the world gifted that way has zero practical use when programming.<p>&gt; A monad is a monoid over a set of curried functions.<p>Is that so? Sounds very wrong to me. If we want to go the monad joke way, monads have to have an operation (a -&gt; m b) that composes, but those are just normal functions, and there’s nothing curried about it. It’s a statement that one could bend enough so it’s kind of right, but what it really does is raise eyebrows.<p>&gt; Monads force sequential processing because you set up a pipeline and the earlier stages of the pipeline naturally must run first.<p>No, a counterexample is the (esoteric) reverse state monad, where values flow the normal way but state comes from the results of future computations.
评论 #42633414 未加载
评论 #42632295 未加载
wesselbindt4 个月前
&gt; A monad is a monoid over a set of curried functions<p>I think this statement will have two kinds of readers. People who are not familiar with monads, for whom it&#x27;ll fly right over their heads, and people who are familiar with monads, who will be annoyed at its inaccuracy.
评论 #42635217 未加载
评论 #42633097 未加载
Gehinnn4 个月前
A nice property of monoids is associativity, which allows for some interesting incremental algorithms, e.g. by using balanced trees: If the fold of × on a list is computed in clever way, the fold of × on a list where one element is modified can be computed in logarithmic time, given the computation of the first list.<p>A good example for a monoid are strings with the string concatenation operation! This is used a lot in text editors. Homomorphisms are also of practical relevance here.<p>If × is a commutative group operation (i.e. inverse elements exist and a×b=b×a), that can even be done in constant time in a trivial way.
mcphage4 个月前
&gt; Although fold-left is commonly used to accumulate results, it is more general than that. We can use fold-left as a driver for a state machine. The second argument to fold-left is the initial state, and the combining function is the state transition function. The list argument provides a single input to the state machine on each state transition.<p>At that point you&#x27;ve lost associativity: ((state * transition) * transition) is meaningful, but (state * (transition * transition)) isn&#x27;t well defined. Which means you&#x27;re no longer talking about monoids.<p>Another way to look at it—by associativity, fold-left and fold-right should be equal. If they&#x27;re not, or if one is defined and the other isn&#x27;t, then you don&#x27;t have associativity.
kqr4 个月前
&gt; If I haven’t used fold-left or fold-right in a while, I sometimes forget which one computes what.<p>I&#x27;m glad I&#x27;m not the only one struggling with this! Though I have started remembering it a different way: I pretend the &#x27;r&#x27; in &#x27;foldr&#x27; stands for recursive. Thus it&#x27;s easier to remember that<p><pre><code> foldr(º, [a, b, ...]) ~= a º (b º ...) </code></pre> where the right term for each operator is given by the recursive call. In contrast, then, foldl must be the iterative variant where<p><pre><code> foldl(º, z, [a, b, ...]) ~= z º= a z º= b ... return z</code></pre>
评论 #42635244 未加载
评论 #42638784 未加载
cartoffal4 个月前
&gt; Here are some monoids: string-append over strings, addition over integers, state transition over machine states, compose over unary functions.<p>Correction: function composition is not a monoid over unary functions, only over families of endofunctions (functions of type `a -&gt; a` for some `a`). You can&#x27;t compose a function of type `a -&gt; b` with a function of type `a -&gt; b` for example, when `a` &#x2F;= `b`, because one gives back a value of type `b` which cannot be passed into a function expecting a value of type `a`.
mrkeen4 个月前
Thinking in terms of monoids can be quite helpful even if you&#x27;re not in pure functions land.<p>For instance, if you&#x27;re putting together an on-disk full-text search index, immutability techniques become very relevant (since you can&#x27;t insert into the middle of files like you can do with in-memory hashmaps). Just make small indexes and a (binary, associative) index-merge operation. Now you have an online&amp;updatable search engine which doesn&#x27;t need to wait for full reindexes over the data to include new documents.
TypingOutBugs4 个月前
Anytime I see Monads or Monoids in the title I am obligated to share one of the greatest YouTube videos of all time :)<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=ADqLBc1vFwI" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=ADqLBc1vFwI</a>
评论 #42635085 未加载
评论 #42634207 未加载
评论 #42635040 未加载
评论 #42635905 未加载
jnhnum14 个月前
Lost me when they got the definitions of semigroups and monoids wrong.<p>Semigroups are not required to have any identity, and the monoidal identity needs to be both a left and right identity.
评论 #42637437 未加载
tmoertel4 个月前
What&#x27;s particularly interesting is that folds are not limited to processing lists. For any recursive data structure, you can create a corresponding fold. What&#x27;s even more interesting is that you can organize your code to automatically create the corresponding fold from a given recursive data structure!<p>Here&#x27;s one example I wrote up using trees as the data structure:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;tmoertel&#x2F;practice&#x2F;blob&#x2F;master&#x2F;EPI&#x2F;09&#x2F;soln_09_002_find_unbalanced_node.lhs">https:&#x2F;&#x2F;github.com&#x2F;tmoertel&#x2F;practice&#x2F;blob&#x2F;master&#x2F;EPI&#x2F;09&#x2F;soln...</a><p>Here&#x27;s another example, this one in Python: <a href="https:&#x2F;&#x2F;github.com&#x2F;tmoertel&#x2F;practice&#x2F;blob&#x2F;master&#x2F;dailycodingproblem_com&#x2F;count_unival_subtrees.py">https:&#x2F;&#x2F;github.com&#x2F;tmoertel&#x2F;practice&#x2F;blob&#x2F;master&#x2F;dailycoding...</a>
评论 #42636464 未加载
tpoacher4 个月前
Very nice article, thank you.<p>One of the clearest, most relatable definitions of what a monoid actually is, why you should care if at all, and how it relates to monads at the end. Great!<p>Might have been nice to add a footnote on what an &quot;endofunctor&quot; is as well, broadly speaking, given the whole &quot;a monad is a monoid in the category of endofunctors&quot; mantra that one first hears when they try to figure out monads.
gleenn4 个月前
This was uniquely followable mathy code
revskill4 个月前
Is it useful in solving leetcode problems ?
评论 #42631973 未加载
评论 #42635913 未加载