Live data from Hacker News

Ask HN: When is pure functional programming beneficial?

news.ycombinator.com

31–40 of 86 posts

Re: Ask HN: When is pure functional programming beneficial?

#31

Earlier quoted context omitted.

... or working in an environment or on a problem for which functional patterns apply. Suppose you are writing a "CRUD" app that writes to a relational database, how do you apply functional programming to that? The whole point of an application like that is that it makes side effects. In some cases you can break those problems down into functional pieces. Consider Python drivers for a product like https://www.arangodb…

I never really understood what a "monad" is but for a program like that I'd have all the "side effect" stuff separated from all the "data manipulation" stuff. It could all be organized as functions, which I generally do because I find it much easier to reason about mentally than objects. "Pure" functional programming is an extreme which probably isn't suitable for many real-world programming tasks. Functional style i…

Code like the above can be restructured with monads in various ways.

In general there is the pattern of building systems that do execution control such as the methods used to create control structures in Lisp, the Executor in Java, etc. One way to use monads is to accumulate a list of operations that need to be done and actually does the operations when you "unpack" the monad at the end. Such a monad could do many of the things a compiler does since it has the operation list at the outset and doesn't just blast from one statement to the next. This blends into the techniques used in Fluent DSLs.

Re: Ask HN: When is pure functional programming beneficial?

#32
I mean, there's different ways to describe this but it comes down to how you want to think about state. The more pure you get the more you can focus on operations or transformations, as functional programming is really about pipelines of transformation of code & data. By having such a constraint over state it allows one to potentially find it more easier to reason about whatever domain they are solving for. However in order to achieve that there is a certain set of hoops one has to mentally go through and maintain in order to think clearly about said problems. For some that is worthwhile for others not. I generally trend towards code that is functional regardless of the language.

Related concepts that are relevant at a high level is the comparison between declarative code and imperative. Code that is functional tends to be declarative and in a way "is what it seems to be." There is also a transitive property that allows the makeup of a thing to also be its own runnable representation which makes portability or idempotent things easier to achieve.

I dare say that original "OO" in terms of "message passing" is functional in nature, but object-oriented somehow became something it is not.

Erlang is probably the best representation of a functional language ideal in my own personal opinion when weighing in the ecosystem and capabilities of the language and tooling.

Re: Ask HN: When is pure functional programming beneficial?

#33
post #7

Earlier quoted context omitted.

>Anything you could unit test without mock objects You can unit test without mock objects because you're following functional patterns.

... or working in an environment or on a problem for which functional patterns apply. Suppose you are writing a "CRUD" app that writes to a relational database, how do you apply functional programming to that? The whole point of an application like that is that it makes side effects. In some cases you can break those problems down into functional pieces. Consider Python drivers for a product like https://www.arangodb…

Usually your database connection is a function that is passed as a parameter to the functions using it.

That way you don't need to Mock, but you can input a "connection" during testing that does what you expect.

If you say "well now I have to type extra parameters everywhere", this is usually solved with partial application. I.e. you create findings from your functions that have the Db Connection already baked inside.

https://fsharpforfunandprofit.com/posts/low-risk-ways-to-use...

https://fsharpforfunandprofit.com/posts/partial-application/

Re: Ask HN: When is pure functional programming beneficial?

#34

Mutable State is to Software as Moving Parts are to Hardware :-) [link redacted] I find personally that there are some areas where FP shines -- anything that is or could be a CLI that takes some input and returns some other output is well suited. A GUI program is the manipulation of state, and it is poorly suited, though many sub parts might not be.

> A GUI program is the manipulation of state, and it is poorly suited, though many sub parts might not be.

If you're manipulating state that doesn't drive any outputs, then your program doesn't do anything useful. I think they're not all that different.

Re: Ask HN: When is pure functional programming beneficial?

#35
post #14

Earlier quoted context omitted.

Not necessarily. For an easy counter example, build a Sierpiński triangle. Easy to unit test. Easier to build using imperative code than functional. (This goes for a ton of fractals, honestly.)

It burns me up that Fibonacci numbers are used so frequently as an example of functional programming because it is a clear case of malpractice, particularly because it performs terribly without memoization. Even in the 1980s CS profs were trying to tell us how BASIC sucks but efficient Fibonacci is so easy to code up in BASIC. (I'd really be impressed with a system that could figure out the closed form based on the d…

Or you can write the tail-recursive version which starts from 1 and builds to n; no need to memoize.

Re: Ask HN: When is pure functional programming beneficial?

#36
post #28

Structure and Interpretation of Computer Programs section 3.1, Assignment and Local State, gives an overview of the costs and benefits of adding the concept of mutation to an otherwise referentially transparent language. It's from the opposite perspective: we know functional programming, now what's so great and so dangerous about imperative? http://sarabander.github.io/sicp/html/3_002e1.xhtml

A lot of people in the imperative world have learned that it’s much easier to test pure functions, and many more people know how badly global shared state affects scalability on the human factors side (everything, everywhere, has access to changes this variable, so good luck figuring out why it gets a bad value).

Functional core, imperative shell is the compromise where you collect state in shallow parts of the code, where they are easier to spot and thus easier to reason (correctly) about.

Re: Ask HN: When is pure functional programming beneficial?

#37
post #14
post #7

Earlier quoted context omitted.

>Anything you could unit test without mock objects You can unit test without mock objects because you're following functional patterns.

Not necessarily. For an easy counter example, build a Sierpiński triangle. Easy to unit test. Easier to build using imperative code than functional. (This goes for a ton of fractals, honestly.)

> Easier to build using imperative code than functional.

I am a bit confused about this. The following code is pure and I can't think of an easier way to do it imperatively:

    function triangle(n) {
        if (n  (p[i - 1] ?? 0) + (p[i] ?? 0))
    }

Re: Ask HN: When is pure functional programming beneficial?

#38

Systems where you need to prove correctness at least informally. It’s much easier to prove correctness of a functional program than imperative one.

Depends on your definition of "correctness". In MISRA, JSF, and similar areas, memory usage (including stack!) is part of the correctness. If you overflow the stack because of recursion, it doesn't matter how "correct" the rest of the program was, it's still broken.

And the same for time. Some programs require proving that you meet the timing constraints. Recursion does not make that easier. (Neither does a language that can allocate behind your back, nor one that can run a garbage collector.)

So the rules for such environments usually say: No recursion, and no allocations after initialization. They say this because those rules make it easier to prove correctness.

Re: Ask HN: When is pure functional programming beneficial?

#39

I wouldn't say there is any threshold where purely functional programming shines less. Fewer regressions and the system being more likely to "just work" makes it more fun to develop. So for interactive programs, servers, CLI tools, parsers et.c. purely functional programming is amazing. An elm developer reported that the prototype they wrote in elm ended up with less bugs than the actual production system. I personal…

F# is one of the best professional experiences in my career writing software. Unfortunately now Im back in a dynamically typed world dealing with null checks and exceptions..

Re: Ask HN: When is pure functional programming beneficial?

#40
IMHO the benefit of fp is it's easier to prove things. The downside is that it's more work: being able to show you've done something correctly is extra work on top of the actual doing of it correctly.

There's a point of view that if you haven't done it correctly, you haven't done it at all. There's another point of view that you just fix bugs as they found.

Post reply on HN