TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

What's functional programming all about? (2017)

161 pointsby Ivoah9 months ago

18 comments

lihaoyi9 months ago
Author here. This blog post is actually kind of funny; I had a flash of clarity one afternoon that wouldn&#x27;t go away so I spent 8 hours in a manic frenzy banging it out in one pass with no editing. Not how most of my writing happens (typically its a tedious slog with multiple passes of editing and refinement)<p>Anyone who likes this article on my blog should check out this presentation on applying this philosophy to build tools<p><a href="https:&#x2F;&#x2F;youtu.be&#x2F;j6uThGxx-18?si=ZF8yOEkd4wxlq84X" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;j6uThGxx-18?si=ZF8yOEkd4wxlq84X</a><p>While the blog post is very abstract, the video demonstrates how to take the abstract philosophy and use it to help solve a very concrete, very common problem
评论 #41451495 未加载
评论 #41451410 未加载
yodsanklai9 months ago
As a programmer, I don&#x27;t know if it&#x27;s still relevant to make a strict separation between programming paradigms. You can use immutable types, pure functions, closures and so on in most languages. Conversely, you can define mutable types and imperative code in most functional programming languages.<p>I&#x27;m always surprised reading comments on these topics, people saying they don&#x27;t grasp FP. But don&#x27;t we use higher-order functions, closures, combinators all the time in most mainstream languages? How hard can it be to learn OCaml or Clojure for someone who use closures all over the place in JS?<p>Monads have a steeper learning curve, but besides Haskell, they aren&#x27;t that pervasive. And there are constructs with similar flavor in mainstream languages too (result types in Rust...)
评论 #41453485 未加载
评论 #41450912 未加载
评论 #41451589 未加载
评论 #41450788 未加载
评论 #41454038 未加载
评论 #41453635 未加载
评论 #41452098 未加载
评论 #41459546 未加载
评论 #41451328 未加载
评论 #41451078 未加载
gr4vityWall9 months ago
The article itself was well written, although I&#x27;d appreciate if the author was more &quot;to the point&quot; with the examples.<p>FP never resonated with me and never fit the mental model I have for programming. I tried learning Prolog and Haskell a few times, and I never felt like I could reason about the code. This line from the article:<p>&quot;[..] With functional programming, whether in a typed language or not, it tends to be much more clear when you&#x27;ve made a trivial, dumb error [..]&quot;<p>wasn&#x27;t my experience at all. In my experience, what made it clear when I made a trivial&#x2F;dumb error was either having good typing present, or clear error messages.<p>I do always try to use the aspects from it that I find useful and apply them when writing code. Using immutable data and avoiding side effects when possible being the big ones.<p>I&#x27;m glad FP works for the blog author - I&#x27;ve met a few people that say how FP makes it easier for them to reason about code, which is great.
评论 #41450522 未加载
评论 #41451609 未加载
评论 #41449751 未加载
评论 #41450065 未加载
mgdev9 months ago
I&#x27;m personally a fan of FP. It offers clear benefits: simplified parallelization, improved testability, and reduced side effects. These advantages often lead to more maintainable and robust code.<p>However, FP&#x27;s benefits can be overstated, especially for complex real-world systems. These systems frequently have non-unidirectional dependencies that create challenges in FP. For example, when component A depends on B, but B also depends on a previous state of A, their interrelationship must be hoisted to the edges of the program. This approach reduces races and nondeterministic behavior, but it can make local code harder to understand. As a result, FP&#x27;s emphasis on granular transformations can increase cognitive load, particularly when dealing with these intricate dependencies.<p>Effective codebases often blend functional and imperative styles. Imperative code can model circular dependencies and stateful interactions more intuitively. Thus, selectively applying FP techniques within an imperative framework may be more practical than wholesale FP adoption, especially for systems with complex interdependencies.
评论 #41450002 未加载
评论 #41449345 未加载
评论 #41453197 未加载
评论 #41452837 未加载
leoff9 months ago
Interesting how the post doesn&#x27;t mention the word &quot;side effect&quot; once.<p>To me, all of this could be summarized by &quot;no side effect&quot;.
评论 #41450239 未加载
评论 #41450590 未加载
评论 #41459717 未加载
评论 #41454255 未加载
评论 #41450270 未加载
评论 #41451550 未加载
dang9 months ago
Related:<p><i>What&#x27;s Functional Programming All About?</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=15138429">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=15138429</a> - Aug 2017 (139 comments)<p><i>What&#x27;s Functional Programming All About?</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13487366">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13487366</a> - Jan 2017 (2 comments)
评论 #41450158 未加载
yCombLinks9 months ago
My problem usually appears when rather than clearly laying out the types of each returned value like in the article, all the FP guys I&#x27;ve worked with want to build giant chains that look like : return beat(whip(mix....(eggs)))
评论 #41459774 未加载
评论 #41451278 未加载
kh9sd9 months ago
Very nice article, I liked it a lot! It personally resonated with me and my own conclusion that the core &quot;benefit&quot; of FP is (for lack of a better work, stupid Bitcoin) &quot;proof of work&quot;.<p>Writing functions FP is essentially all about returning results from a function, which is proof that that a computation has occurred. If you don&#x27;t have that return value, that proof, then obviously the rest of your code can&#x27;t and shouldn&#x27;t continue, and FP makes it obvious compared to more traditional imperative approaches.<p>And this idea extends into so many other things that people consider core, or at least originating from FP. Result&#x2F;Option types come to mind, making the possible failure of &quot;proof of work&quot; explicit in the type signature, so people are forced to consciously handle it. It also leads into the whole idea of type driven design, one of my favorite articles, &quot;Parse, don’t validate&quot;[1], describes this as well, making types that clearly restrict and set the expectations needed for the &quot;proof of work&quot; to always be returned from a function.<p>[1] <a href="https:&#x2F;&#x2F;lexi-lambda.github.io&#x2F;blog&#x2F;2019&#x2F;11&#x2F;05&#x2F;parse-don-t-validate&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lexi-lambda.github.io&#x2F;blog&#x2F;2019&#x2F;11&#x2F;05&#x2F;parse-don-t-va...</a>
评论 #41451079 未加载
Terr_9 months ago
&gt; Anything vertically separated can be done in parallel.<p>This assumes no contention on a limited number of bowls or having only one kitchen tool for beating or whisking etc. :P<p>I point that out not to demand that the metaphor be bulletproof, but because I think it may help explore something about state-handling and the FP&#x2F;imperative split.<p>How might this tiramisu-tree model change if we were limited to N bowls and 1 beater and 1 whisk, unable to treat them as statelessly-shareable or endlessly-duplicable?
评论 #41459844 未加载
评论 #41452838 未加载
ninetyninenine9 months ago
It’s hard to characterize what fp is. A lot of people think fp is a bunch of techniques like map, reduce, immutability or high level functions or monads.<p>None of those things are exclusive to fp. They are just tricks developed by fp.<p>Here’s how to get a high level characterization of what fp actually is:<p>You know how in math and physics they have formulas? Formulas for motion, for area, etc.<p>Functional programming is about finding the formula for a particular program.<p>Typically when you code you write an algorithm for your program. In functional programming you are writing a formula. Think about what that means.<p>The formula has side effect benefits that make it easier to manage complexity and correctness. That’s why people like it. These side effects (pun) are not evident until you programmed with fp a certain amount.<p>Obviously though people naturally reason about things in the form of procedures rather then formulas so fp tends to be harder then regular programming.
评论 #41452171 未加载
mbivert9 months ago
I believe that a good definition of the &quot;core&quot; functional programming is: it&#x27;s a practical way of using the λ-calculus. Similarly, imperative programming is a practical way of using Turing machines.<p>It&#x27;s always somewhat possible to express what are typically considered imperative features in a functional fashion (e.g. monad), or bend imperative languages to behave somewhat like functional ones: I think the differences become clearer once we reach out for the underlying theoretical models.
fulafel9 months ago
&gt; there are probably just as many people using FP without static types: in some parts of the Javascript community, Clojure, Scheme or one of the many other Lisps.<p>Erlang &amp; Elixir too.
wruza9 months ago
<i>It might look like a bit of a mess, but if you look carefully, you will see</i><p>I “grasp” FP, but that pretty much sums up my experience with it. Half of its promises it delivers in the form of “you got used to read a multi-faceted inside-out mess”. I think FP is actually harmful, because it masks the fact we don’t properly teach regular programming.
评论 #41454582 未加载
giovannibonetti9 months ago
I wonder if there is a Python PEP for adding a pipe operator |&gt; to the language. This could be pretty handy, as described in the article.
deepsun9 months ago
The article is not about FP.<p>&gt; Languages like Java encourage patterns where you instantiate a half-baked object and then set the fields later.<p>Maybe it did before 2010. For many years everyone prefers immutable objects (so that object and its builder have different types, or in simpler case -- no setters, only constructor initialization). You can see it in pretty much all frameworks.<p>I&#x27;m ok with both functional and procedural languages, I just think this article is not about functionality. Come on, FP is all about monads!<p>Moreover, the &quot;imperative&quot; code example would be impossible anyway with immutable objects without side effects. So what I think the article is about is immutable data types. Everyone agrees that immutable are better, we have to do mutables only when we&#x27;re optimizing for CPU&#x2F;RAM.<p>And BTW concurrency is typically easily achieved if variables were not plain objects, but Future&#x2F;Observable&#x2F;Flow&#x2F;Stream -- pick your poison. They all have passed the hype hill already, and became &quot;just a tool&quot; I think.
评论 #41451675 未加载
ryandv9 months ago
The classic dichotomy drawn between functional programming (FP) and object-oriented programming is the &quot;Expression Problem&quot; [0]; in the former approach you optimize for extensibility of the number of <i>operations</i> in your model, at the cost of reduced developer ergonomics when attempting to extend the number of <i>types</i> you can operate upon. In the latter, object-oriented approach, you have the inverse tradeoff.<p>In FP the fundamental unit of composition is the function; your solution is expressed as a composition of functions, which are treated as first-class objects (first-class meaning, functions can be manipulated via higher-order functions, or &quot;functions of functions&quot;), a feature not always seen in object-oriented or multi-paradigm languages. Polymorphism is most frequently achieved through parametric polymorphism, or &quot;generics,&quot; and ad-hoc polymorphism, or &quot;trait bounds&quot;&#x2F;&quot;typeclass constraints&quot;.<p>In OOP the fundamental unit of composition is the object; your solution is expressed as a composition of objects, whether through actual composition&#x2F;aggregation (objects containing and possibly delegating to other objects [1]), or subtype polymorphism, also known as &quot;inheritance.&quot; Parametric and ad-hoc polymorphism can often feature in OOP languages as well, but subtype polymorphism is a distinguishing characteristic of OOP.<p>Functions, particularly pure functions without side effects in the &quot;real world&quot; such as I&#x2F;O or hardware access, are akin to equations in which an expression can be replaced by its value - the &quot;left-hand side&quot; equals the &quot;right-hand side.&quot; Mutable state often does not enter into the picture, especially when programming in this &quot;pure&quot; (side-effect free) style. This makes functional programs easier to reason about equationally, as one can determine the value of an expression simply by inspection of whatever variables are in the function&#x27;s scope, without having to keep track of the state of the entire program.<p>Objects are distinguished by their often stateful nature as they bundle together data&#x2F;internal state, and operations over that internal state. Often such internal state is hidden or &quot;encapsulated&quot; from the client, and the internal state is only modifiable (if at all) via the object&#x27;s class&#x27; set of public methods&#x2F;operations. Objects with immutable internal state are more akin to closures from functional programming - that is, functions with access to a &quot;parent lexical scope&quot; or &quot;environment.&quot;<p>Between the two extremes exists an entire spectrum of mixed-paradigm languages that incorporate features of both approaches to structuring and modelling a software solution.<p>[0] <a href="https:&#x2F;&#x2F;wiki.c2.com&#x2F;?ExpressionProblem" rel="nofollow">https:&#x2F;&#x2F;wiki.c2.com&#x2F;?ExpressionProblem</a><p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Composition_over_inheritance" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Composition_over_inheritance</a>
dboreham9 months ago
Missed the key point: FP serves to make someone feel like they&#x27;re smarter than someone else.
评论 #41452934 未加载
Barrin929 months ago
&gt;The core of Functional Programming is thinking about data-flow rather than control-flow<p>That&#x27;s not right. The difference between data and control flow oriented programming is the difference between laziness and eagerness. The difference between imperative and functional programming is largely one of abstraction, not a feature in itself.<p>The genuine core feature of FP is the <i>separation of data and methods</i>, that stands in contrast not to imperative programming but object oriented programming, whose core feature is <i>fusion of data and methods</i>. Functional programming tries to address complexity by separation of concerns, pulling data and methods apart, OO tries to address complexity by encapsulation, pulling data and methods into purely local contexts.<p>This is also where the association between FP and static typing comes from that the post briefly mentions. Pulling data and functionality aside lends itself to programming in a global, sort of pipe based way where types act like a contract between different interacting parts, whereas the information hiding of OO lends itself to late binding and dynamic programming, taken to its most extreme version in say, Smalltalk.
评论 #41449744 未加载
评论 #41449590 未加载
评论 #41450420 未加载
评论 #41449447 未加载