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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

State Monad for the Rest of Us

47 点作者 arialdomartini10 个月前

5 条评论

arialdomartini10 个月前
A series of articles starting from the very scratch and getting to the State Monad. It&#x27;s thought to be novice-friendly: although using F#, it assumes no knowledge of it. If only it aroused someone&#x27;s curiosity around F#, that would make my day.<p>It shows how algorithms with mutable state can be implemented with pure functions, with immutable variables only.<p>The series itself is an excuse to take several detours on other Functional Programming topics: currying, partial application, recursion, Functors, Applicative Functors.<p>The source code includes F# and C# examples. In the next weeks it will be followed by code examples showing how to apply a state monad in practice.
评论 #41202076 未加载
评论 #41202089 未加载
评论 #41202133 未加载
评论 #41206166 未加载
darby_nine10 个月前
You should probably mention in the introduction what the state monad <i>is</i> to those who don&#x27;t use F#. At first blush it just seems redundant—what is a monad if not a representation of state?
评论 #41203199 未加载
评论 #41202594 未加载
评论 #41202260 未加载
ashton31410 个月前
Monad patterns cut down on boilerplate even in languages where you might not strictly “need” them. I’ve been working on a project in Elixir and I found the writer monad to be exactly what I needed. I wrote about it here: <a href="https:&#x2F;&#x2F;lambdaland.org&#x2F;posts&#x2F;2024-05-01_definitely_not_about_monads&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lambdaland.org&#x2F;posts&#x2F;2024-05-01_definitely_not_about...</a><p>A week or two ago, I found myself working in the same codebase. I had to do some refactoring and, thanks to the monad I had already established, the refactoring <i>reduced</i> code while <i>expanding</i> functionality. The best refactors happen when the patterns make it easy to do.
Iceland_jack10 个月前
Any function matching the type<p><pre><code> St -&gt; (a, St) </code></pre> can viewed as `State St&#x27; parameterized over `a&#x27;. There are several behaviors that organize such functions, such as Monad which &quot;overloads the semicolon&quot; to work on stateful computations. Another behavior implements a stateful interface: `MonadState St&#x27;.<p><pre><code> get :: St -&gt; (St, St) put :: St -&gt; (St -&gt; ((), St)) </code></pre> With type classes in Haskell, behaviour is type-directed so in order to reap the benefits we declare a new type.<p><pre><code> {-# Language DerivingVia #-} {-# Language GADTSyntax #-} -- get :: My St -- put :: St -&gt; My () newtype My a where My :: (St -&gt; (a, St)) -&gt; My a deriving (Functor, Applicative, Monad, MonadState St, MonadFix) via State St</code></pre>
评论 #41202908 未加载
loa_in_10 个月前
Isn&#x27;t every monad a state monad?
评论 #41203523 未加载