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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

How side effects work in FP

142 点作者 anchpop大约 3 年前

17 条评论

ReleaseCandidat大约 3 年前
Oh, haven&#x27;t seen a monad article in quite some time.<p>&gt; Monads really are just a convenient way to build up action values<p>Except when they aren&#x27;t, because lists&#x2F;arrays (with map) and Maybe (with map) and .. are something totally different. And that&#x27;s the problem with _all_ these monad tutorials&#x2F;how-tos&#x2F;...: the problem is, that monads (and functors) are not burritos or elephants but _both_. So they are hard to understand looking only at a single instance.<p>Btw. monads are not the only way FP deals with effects, see algebraic effects. <a href="https:&#x2F;&#x2F;github.com&#x2F;yallop&#x2F;effects-bibliography" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;yallop&#x2F;effects-bibliography</a> <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=DNp3ifNpgPM" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=DNp3ifNpgPM</a>
评论 #30788554 未加载
评论 #30790996 未加载
FeepingCreature大约 3 年前
So this is basically Eternalism[1]? Instead of writing a program that reads inputs, experiences a sequence of state mutations and takes actions, you generate the graph of every possible state and path between states annotated with inputs on the edges and outputs on the vertices, and let the runtime pick the actual path through it. And this works because you&#x27;re generating it lazily, but that&#x27;s just an implementation detail. And the program in itself isn&#x27;t ever in any particular state, because the program is the description of every possible state.<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Eternalism_(philosophy_of_time)" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Eternalism_(philosophy_of_time...</a>
评论 #30792484 未加载
评论 #30789700 未加载
评论 #30787903 未加载
评论 #30789746 未加载
delegate大约 3 年前
I don&#x27;t speak Rust well, but I can share my perspective from Clojure, where I tend to think in data. A program is a sequence of immutable functions taking input from the world and reducing that to instructions to mutate the world (aka side effects). The world here can also be some place that stores state. Ideally mutation is the last step of the pipeline, which takes the instructions and produces the side effects.<p>Or simply<p>input -&gt; fn1 -&gt; fn2 -&gt; fnn -&gt; mutate!<p>Where &#x27;fn&#x27; are all immutable.
评论 #30788390 未加载
评论 #30788800 未加载
marcan_42大约 3 年前
Well, that&#x27;s the first time side effects in Haskell made sense to me. Well done.<p>Not that I&#x27;ve ever <i>seriously</i> tried to learn Haskell, but in the past every time I&#x27;ve lazily come across an article about it it&#x27;s always seemed like a bizarre confusing world, even though I know how functional programming (in the sense of purity) works.<p>Now there&#x27;s just one thing missing. We all know what this style of programming is. It&#x27;s asynchronous programming with callbacks. Seriously, Haskell folks, if you started with &quot;all side effecting functions are kind of like async operations with a completion callback (the stuff people do in JavaScript all day), and then we have some syntactic sugar to make it suck less&quot; you&#x27;d have a <i>much</i> easier time getting people to wrap their head around all this.<p>(Yes, I know the details aren&#x27;t exactly the same, but drawing parallels to stuff people already know matters)<p>Seriously, this idea of there being a central effect dispatcher (the bit that runs `main` behind the scenes) is so eerily like the coroutine scheduler in async coroutine paradigms that I can&#x27;t believe more people haven&#x27;t drawn parallels between these programming styles.
评论 #30786956 未加载
评论 #30786975 未加载
评论 #30786977 未加载
评论 #30791891 未加载
评论 #30788332 未加载
ashton314大约 3 年前
&gt; Equational Reasoning<p>Probably the best reason to use FP imo. It just cuts out so much overhead when trying to read other people’s code.
评论 #30787974 未加载
评论 #30787854 未加载
vmchale大约 3 年前
The author doesn&#x27;t mention laziness, which is pretty huge. Laziness is half of immutable data structures!<p>I have a tidbit in the first paragraphs: <a href="http:&#x2F;&#x2F;blog.vmchale.com&#x2F;article&#x2F;effects" rel="nofollow">http:&#x2F;&#x2F;blog.vmchale.com&#x2F;article&#x2F;effects</a>
评论 #30793078 未加载
crdrost大约 3 年前
&gt; I don&#x27;t know why people try to explain monads without explaining this first<p>I mean, I get where you&#x27;re coming from, but the thing that you&#x27;ve described is that a monad and there&#x27;s not an easy way to get it there.<p>But yes, I am with you, you want to explain to someone that Haskell&#x27;s model of side effects is a sort of <i>metaprogramming</i>, much simpler than macros, we just give you a data type for “a program which does some stuff and eventually can produce a ______” and ask you to define a value called `main` which is a program which produces nothing special. And it&#x27;s the compiler&#x27;s job to take that program and give it to you as a binary executable that you can run whenever you like.<p>You also want to give people a number of other examples of things that are monads. “a nullable ____”, “a list of ____s,” “an int (summable) with a ____,” and maybe an example that is not like “a function from ____s to ints,” or “a set of ___s.”<p>The key to telling someone what a monad is, involves trying to explain to them that in some sense “a program which produces a program which produces an int” is not terribly more descriptive than just “a program which produces an int.” If you can combine this with the adjective being outputtish and universal you have a monad.
arciini大约 3 年前
As someone who has never really understood monads (or tried that hard to) but who has done a good amount of Javascript programming, I really like the description of a monad as a series of nested data structures describing the next steps of a project.<p>Ultimately, that&#x27;s very similar to what old-school ways of declaring Promises do in Javascript. You&#x27;re creating a data structure that you then attach a new function to execute with the results.
评论 #30786862 未加载
评论 #30787002 未加载
评论 #30788742 未加载
Jensson大约 3 年前
Haskell is a framework that takes a monad interface implementation and performs IO based on it. The monad in question is used like a self flattening iterator &#x2F; generator, and together with lazy evaluation it can function like a regular program even though the monad itself is implemented using pure functions.
Tainnor大约 3 年前
One benefit of describing state change as a pure data structure, instead of directly executing it, is that you may now write functions that run these state changes in different ways.<p>E.g. you may have a dry-run function that doesn&#x27;t actually change the state but only describes what it would do. Or you could have a function that generates a verbose log of all steps executed. Etc.<p>That doesn&#x27;t necessarily work for IO, as that gets special treatment by the runtime, but you can do it with your own types.
评论 #30791068 未加载
olliej大约 3 年前
Something that I always found funny is that main clearly has to take something as input (otherwise it would not be able to produce different output between runs). So in GHC the main method (main :: IO()) internally takes a value of type RealWorld (theRealWorld) and returns the new RealWorld produced by running the program. I imagine by the time it&#x27;s lowered to actual assembly they&#x27;ve dropped that, but I&#x27;ve never dealt with ghc&#x27;s machine codegen
评论 #30789576 未加载
presentation大约 3 年前
The ligature makes the Haskell look even more magical, can&#x27;t tell what I&#x27;d type to make that &gt;&gt;=== character.
评论 #30789526 未加载
rebeccaskinner大约 3 年前
This is a great way to help people get an intuition for monadic IO.
dmitriid大约 3 年前
&quot;How to do side effects in s&#x2F;FP&#x2F;Haskell&quot;<p>Erlang is a functional programming language and doesn&#x27;t need to jump through hoops to do side effects.<p>So is OCaml. So is...
评论 #30787265 未加载
评论 #30787023 未加载
评论 #30790599 未加载
评论 #30787527 未加载
gegtik大约 3 年前
dweb link:<p>&gt; ipfs resolve -r &#x2F;ipns&#x2F;nauseam.eth&#x2F;coding&#x2F;random&#x2F;how-side-effects-work-in-fp&#x2F;: no link named &quot;coding&quot; under QmdzzonFE9eX6FGs8UbCyoC2XS5NQjaG6gaqhgeUyTHnag
评论 #30791241 未加载
Slackwise大约 3 年前
&gt; You&#x27;re viewing my site on the centralized web. Check me out on the dweb ! (Warning: it&#x27;s slow.)<p>It&#x27;s really hard to take someone serious when they make big, untrue, statements saying the real web is &quot;centralized&quot; right on every page of their site, to peddle crypto scams.<p><a href="https:&#x2F;&#x2F;chadnauseam.com&#x2F;reasoning-quiz" rel="nofollow">https:&#x2F;&#x2F;chadnauseam.com&#x2F;reasoning-quiz</a><p>&gt; Neo-Nazis are holding a demonstration in a small town, waving swastikas around and shouting about Hitler. They seem to be pretty peaceful so far, so the First Amendment says you probably can’t get rid of them. However, their demonstration is near a main street and it could be a minor inconvenience to the traffic trying to go through.<p>&gt;<p>&gt; [ ] Allow the neo-Nazis to demonstrate.<p>&gt;<p>&gt; [ ] Break up the demonstration on the grounds of ‘blocking traffic’.<p>I am at a loss for words. Nazi sympathy under the guise of tolerance.
评论 #30791021 未加载
treis大约 3 年前
FP explanations like this annoy me:<p>That is, this code:<p>x = foo()<p>y = bar(x)<p>z = bar(x)<p>Should mean the same thing as:<p>y = bar(foo())<p>z = bar(foo())<p>Because it&#x27;s obvious to anyone that has written any sort of complex program that those are not the same thing. foo() can be an expensive operation like an HTTP call. Or it might depend on a database which can change state underneath it.<p>I assume FP has answers for these things but the tutorials never cover them. They all imagine a world without state or expensive operations to show how wonderful it is. And that&#x27;s an easy world to program in.
评论 #30789398 未加载
评论 #30790613 未加载
评论 #30789406 未加载
评论 #30789537 未加载
评论 #30789374 未加载
评论 #30789248 未加载