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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Mindset shifts for functional programming (with Clojure)

162 点作者 janetacarr大约 2 年前

8 条评论

roenxi大约 2 年前
&gt; Recursion over Looping<p>Part of what makes Clojure a great programming language is that you don&#x27;t have to believe this if you don&#x27;t want to. Nobody has yet convinced me that recursion has any sustained advantage over looping.<p>Using a loop is generally bad practice if a more specialised operation is available (don&#x27;t loop if something is a simple map or reduce for example). But if the situation justifies a recursion then it usually justifies a loop unless the recursion is particularly neat. Recursion has the same problem as looping - it doesn&#x27;t tell anyone anything about what the code is really doing. If I see map then I have implicit and explicit expectations about what is about to happen.<p>I enjoyed the article though.
评论 #35452441 未加载
评论 #35452459 未加载
评论 #35452360 未加载
评论 #35453885 未加载
评论 #35458770 未加载
评论 #35453145 未加载
评论 #35458090 未加载
评论 #35455244 未加载
评论 #35454397 未加载
评论 #35455640 未加载
评论 #35458159 未加载
评论 #35455081 未加载
samsquire大约 2 年前
If your team and you can read it later or when it goes wrong, then it&#x27;s fine.<p>I don&#x27;t want to need to think hard about what code does, it should be clear. Or jump through 100 files to find a simple algorithm that has been divided into 100 pieces.<p>I think people refactor to their own understanding or mental model or refactor-to-understand.<p>I think there are cases where imperative code is intuitive and others where functional code is intuitive.<p>I&#x27;ve worked on two professional projects in Clojure at a surface level but I still find Python easier to read, but that&#x27;s my experience YMMV.<p>There&#x27;s a point in my programming projects when my own understandability is weakened and I this week I&#x27;ve been trying to think of a mindset that simplifies the problem. I am experimenting with multithreaded code in Java that implements left-right concurrency control. The idea is readers don&#x27;t block writers and writers don&#x27;t block readers and writers don&#x27;t block writers. Give each thread a shard and rely on commutative property of your tree data structure and have a coordinator thread handle merging. The benefit: each thread can operate at single core speeds without any synchronization for reading OR writing. Buffer flipping is handled by the coordinator thread.<p>Each thread has its own copy of state which is merged by the coordinating thread. So it&#x27;s an eventually consistent system. The result: each thread can modify its own copy of the data as much as it wants and it can see its own snapshot of global state at the last snapshot point. Cross thread writes always happen on the inactive buffer.<p>How do you think about data transformation pipelines? If only there was an IDE for kafka or clojure transformation pipelines.
评论 #35453980 未加载
margorczynski大约 2 年前
I think the main thing is not really FP vs whatever else but the most fundamental thing is declarative vs imperative. That is probably the biggest jump in mindset when coming from e.g. C or C++ to something like Haskell - that instead of telling the computer&#x2F;compiler what to do, you describe what you want.
评论 #35452227 未加载
评论 #35454345 未加载
sharas-大约 2 年前
The advantage of clojure is in data centricity. It is not about shifting mind to recursion from looping. Clojure has a &#x27;for&#x27; which is it&#x27;s list comprehension loop. The actual fun of clojure: <a href="https:&#x2F;&#x2F;bitslap.it&#x2F;blog&#x2F;posts&#x2F;fun-of-clojure.html" rel="nofollow">https:&#x2F;&#x2F;bitslap.it&#x2F;blog&#x2F;posts&#x2F;fun-of-clojure.html</a>
评论 #35454270 未加载
dandeere大约 2 年前
The biggest problem of functional languages is that they teach things of doing things that are not how computers work internally. For that C is much better. Assembly language the best. Or any other procedural language will teach you more about how computers run under the hood.<p>Recursion is a purely math concept, in real life the chips don&#x27;t work with recursion, they work strictly with if-then (more current&#x2F;less current) logic. Assembly language is also procedural.<p>Also, there is another fundamental thing that is a blocker for functional programming - our brain. For recursion you have to keep in brain many things while you call yourself. This is extremely hard (hmm, I wonder why the Lisp Machine was so pricey ;) ) - especially for the majority of people whose brain has problem to remember more than 2 things in the span of 5 seconds.<p>If you like math, yeah, sure use Haskell or Clojure, but for the most people it&#x27;s an overkill and for the most part people still tend to write very procedural code - you have for loops in Clojure - I am wondering why ;D<p>I like some things that are often associated with functional programming, e.g. immutability, but I am not a fan of recursion at all. Also there seem to be some obsession of having dynamic types in many Lisp and functional programming languages, for example Clojure or Elixir, and I hate it.
评论 #35478340 未加载
koito17大约 2 年前
As a Clojure programmer, I find it a bit unfortunate that most of this post, like many other posts demonstrating Clojure, tend to paint a rosey picture and never mention the realities one has to reconcile in a Clojure codebase.<p>For instance, one of the sections in this blog post is titled &quot;Functions over Objects&quot; but any Clojure programmer with experience will agree that most code ends up becoming some sort of map munging. Rather than &quot;functions over objects&quot; you really have &quot;map-munging and function instrumentation in dev over banging on concrete instances of classes&quot;.<p>There is also this statement<p>&gt; functional programming languages often facilitate iteration through recursion<p>then the author proceeds to mention loop&#x2F;recur, which is nice, but it is dishonest at best and lying at worst to imply loop&#x2F;recur is the most natural or common way we iterate over a structure. Off the top of my head, I always see idioms like `(for [[k v] some-map] (do things with k v))`. It would also be nice to demonstrate how faux-recursive looks like with loop&#x2F;recur rather than stating Clojure&#x27;s lack of TCO and not elaborating further on how the idiomatic version of `recursive-map` would look like.<p>Next,<p>&gt; by (nearly) eliminating side-effects, functional programming (nearly) elminates this whole class of bugs.<p>That may be the intent of purely functional code in general, but in Clojure one regularly interoperates with the host, to take advantage of their rich ecosystem. Many of the libraries one will interop with will involve some imperative or stateful things! So I think it&#x27;d be better if the author had mentioned the idea of &quot;functional core, imperative shell.&quot; That is a pattern most Clojurists would agree with, and it is what people really do in a code base, or at least attempt to design.<p>Lastly, while Clojure allows one very naturally to mock up a finite state machine with a single map and writing a few functions to describe transitions given the current state of the machine, I want to emphasize that not much Clojure codebases I&#x27;ve seen ever used FSMs explicitly :-)<p>Overall, the article is okay, but my brain registers it as another &quot;Clojure portrayed with rose-tinted glasses and contrived examples&quot; article. Not that this is bad. It&#x27;s just not the kind of sales pitch I&#x27;d want to show to non-Clojure programmers, because they will likely want to ask questions about maintainability, testing, instrumentation, etc. And it <i>is</i> possible to show Clojure being nice for each of these things. At a previous job I got to see ClojureScript code dating back to 2014, untouched, and surviving 8 years worth of language <i>and</i> library updates.
twic大约 2 年前
I am not a Clojure programmer, and am pretty skeptical of LISPs and FP more generally, but i have to say that transducers are pretty great. The descriptions of them, and the way the interface is expressed, are a bit off-putting, but once you grok them they&#x27;re actually simple and useful.<p>They occupy the same space as Java&#x27;s streams, but manage to do the same stuff with a smaller, more generic, more extensible interface, that can do more stuff (eg intermediate stages get told when input is finished). I&#x27;m jealous.<p>EDIT: IIUC, in Java terms, Clojure&#x27;s &quot;reducing functions&quot; are like Collector, and a transducer is a function which takes a Collector and returns another one. There is then a little bit of top-level sugar so you can give a sequence of transducers, terminating in a concrete reducing function, and get back a reducing function which itself feeds things through the chain of reducing functions built by the transducers. You could probably replicate this in Java, but it might be too clunky even for Java programmers.
评论 #35457687 未加载
评论 #35455620 未加载
twawaaay大约 2 年前
&gt; Transformations over Instructions<p>Hell yeah! The problem with &quot;functional&quot; programming is that a lot of people simply don&#x27;t get you want to push as much of your program to be generic tools that transform things (ie FUNCTIONS!) When I start on a new problem I typically look at what kind of tools (functions) would make the solution concise and readable. Then create the tools and then write the solution with the tools.<p>Unfortunately most developers come with preconceived notions of how the program should be laid out and what the development process should be. If they come from OOP world, you will see code that pretty much looks like objects just without OOP machinery. If they come from scripting&#x2F;procedural then you will see long stretches of instructions just split into smaller &quot;functions&quot;.<p>&gt; Recursion over Looping<p>Recursion is elegant but it is also hard to understand for many people. I object putting anything into code when the only purpose is making the code look elegant &#x2F; more advanced at the cost of narrowing audience that can effectively work with it.<p>My goal is to make the code stupid simple. My main challenge is preventing my ego from trying to impress the reader.<p>Frequently recursion is more readable than looping. I don&#x27;t hesitate using recursion in that case. But I make sure that the reader should be able to instantly recognise the pattern and the pattern is not too complex.<p>Another thing to push recursion to be more manageable is just structuring your code to extract recursion from everything else. Make one or two very small functions that are only responsible for recursion, extract all other logic into other functions that are meaningful on its own (and just happen to be used in recursive context).<p>If the recursion would be too complex, usually iteration is going to be easier to represent the solution in manageable chunks without requiring the reader to take everything in in one go.
评论 #35451896 未加载
评论 #35451996 未加载
评论 #35454218 未加载
评论 #35460109 未加载