News for hackers. Stuff that matters.
Re:Functional programming is finally going mainstream by kstenerud on Tuesday July 12, 2022 @11:33PM (#32078761)
My experiences with the encroachment of FP into otherwise imperative or OO languages has mostly been:

1. Write a bunch of functional code.

2. Marvel at how clean and neat things are.

3. Get customer reports of performance problems.

4. Spend a lot of time ripping out FP components and replacing them with imperative code.

Re:Functional programming is finally going mainstream by ncmncm on Wednesday July 13, 2022 @12:35AM (#32079074)
The big mistake is in believing there is any value in this- or that-oriented programming, or in a this- or that-oriented language.

An engineer uses what is available to produce an economically balanced solution. Done well, that may involve a few OO-ish bits, some functional bits, data-oriented bits, reactive bits, plus hybrid combinations of those and others. The problem dictates the form of the solution.

Every big-enough problem decomposes into a collection of very different subproblems, each of which so decomposes further. The right approach for the top level rarely matches that for most of its parts, nor each for the others, and likewise down the line.

Unless you want to use a different language for each level and subproblem, you will want support for all "orientations" in your language. Thus, a "functional" or "OO" language is an absurdity; likewise, a "functional", "OO", or what-have-you program. They make as much sense as a saw-oriented carpenter's toolbox or table, or a screwdriver-oriented machinist's toolkit or engine.

Programs should be like water, fluidly matching the shape of the problem solved. If that is 20% functional, 10% O-O, and the rest imperative, so be it. Specialization is for insects.

Everything is Turing complete: you can code a solution in any restricted vocabulary. But that means succeeding proves nothing and benefits nobody, but wasted your time and others'.

Use a language that does it all, and use it all. Not necessarily all on every program; but all styles get consideration for each part.

Re:Functional programming is finally going mainstream by danielscrubs on Wednesday July 13, 2022 @02:15AM (#32079528)
“Everything is Turing complete” isn’t true. You can get quite interesting languages by making sure it isn’t. See strong functional programming for examples.

https://en.m.wikipedia.org/wiki/Total_functional_programming

Re:Functional programming is finally going mainstream by ncmncm on Wednesday July 13, 2022 @02:27AM (#32079580)
That you can code certain solutions in a non-Turing-complete language is of identically null benefit.
Re:Functional programming is finally going mainstream by tadfisher on Wednesday July 13, 2022 @04:03AM (#32080098)
Aside from being able to prove that the solution halts, you mean? You get a lot of side benefits as well with a halting program, such as provable limits in time and space for the totality of the program instead of just the functional core bits.
Re:Functional programming is finally going mainstream by ncmncm on Wednesday July 13, 2022 @07:45AM (#32081350)
Maybe you are unable to prove properties of programs you have coded in Turing-complete languages. Many of us do, though. Routinely.

Quick, does this program terminate:

  int main() {}
? Can't prove it?
Re:Functional programming is finally going mainstream by tadfisher on Friday July 15, 2022 @02:36PM (#32111385)
That's not a very useful program.
Re:Functional programming is finally going mainstream by ncmncm on Saturday July 16, 2022 @12:54AM (#32115324)
It is the first stage of every useful program.

Add to it, and show that it still terminates, uses finite memory, introduces no data races or undefined behavior, and maybe computes a step toward something useful.

Repeat until done.

Re:Functional programming is finally going mainstream by AlchemistCamp on Wednesday July 13, 2022 @01:22AM (#32079270)
I completely disagree with the kitchen sink language approach. The benefits in many coding paradigms really kick in once they're used 100%.

For example, consider GOTOs. If 99% of your functions don't use any GOTOs but they other 1% does, you can't be sure how the flow of logic lead to any one function. Immutability works in a similar way. I have a far easier time debugging in concurrent code I'm unfamiliar with if it's written in Elixir than if it's written in JS/TS, even if the project in question uses Immutable.js heavily.

Re:Functional programming is finally going mainstream by yowlingcat on Wednesday July 13, 2022 @09:20AM (#32082282)
> have a far easier time debugging in concurrent code I'm unfamiliar with if it's written in Elixir than if it's written in JS/TS, even if the project in question uses Immutable.js heavily.

How much of that comes from belief rather than reality, and potentially some room to grow on your end? I can debug well written code in any language I jump into, but I've found poorly written and hard to debug morasses can be written in any language.

Poor naming, branch/loop structuring, scoping, data modeling/normalization, module structuring, logging, and dependency selection/integration have all contributed to debugging hassles far more in my production experience than any particular language.

Re:Functional programming is finally going mainstream by AlchemistCamp on Wednesday July 13, 2022 @12:59PM (#32085462)
> How much of that comes from a belief rather than reality, and potentially some room to grow on your end?

You know… suggesting that someone that they’re delusional and uninformed isn’t a very charitable way to discuss anything online.

The belief came from personal experience from 2016-2018 while I was much more experienced with JS/TS and relatively new to Elxir. It definitely influenced me to keep exploring and learning things outside the JS ecosystem I’d previously focused on. I’ve grown as a dev since and so has my appreciation of guaranteed immutability.

Also working with other languages, particularly Rust, in the past few years has only further underlined the point. It makes a strict delineation between variables, references and arguments that can be mutated and those that can’t. The language went with 100% guarantees instead of 95% guarantees for a good reason.

> I can debug well written code in any language I jump into

The phrase “well-written” may be doing a lot of lifting here. Different languages and ecosystems make different practices easy and tend to nudge users in different directions. People who don’t like learning often enjoy the idea that any Turing complete language is equivalent from the programmer’s perspective but it’s just not true.

Re:Functional programming is finally going mainstream by yowlingcat on Wednesday July 20, 2022 @09:23AM (#32165444)
> You know… suggesting that someone that they’re delusional and uninformed isn’t a very charitable way to discuss anything online.

I think there's no reason to exaggerate what I said just because I don't love your pet principle as much as you do. Here's what I explicitly describe as being part of well-written (or poorly written) code, which has very little to do with FP vs OOP vs procedural:

"Poor naming, branch/loop structuring, scoping, data modeling/normalization, module structuring, logging, and dependency selection/integration"

>The phrase “well-written” may be doing a lot of lifting here

I think if you read the entire comment, you'd see it doing a lot less lifting because I explicitly detail what "well-written" is. And I think it is cross-language.

Re:Functional programming is finally going mainstream by lvass on Wednesday July 13, 2022 @12:57AM (#32079167)
"Subproblem" is likely an exaggeration here, otherwise I vehemently disagree. Take Erlang for example, it's theoretically a general purpose language but highly specialized. Would WhatsApp reach a billion users with 50 engineers without this specialization? I doubt it. It's absolutely fine for things to be heavily specialized, as it is fine for our entire civilization to rely division of labor. As the number of developers increase, so should the number of specializations. While a non-turing-complete language doesn't make much sense, web servers, linear algebra and GUIs for example are sufficiently different so that using the same language is unwise.
Re:Functional programming is finally going mainstream by ncmncm on Wednesday July 13, 2022 @02:15AM (#32079526)
There are very successful insects.
Re:Functional programming is finally going mainstream by lvass on Wednesday July 13, 2022 @08:58AM (#32081990)
No creature is limited to the use of a single programming language, but a lot of programs are. You can learn more than one language and choose one that is appropriate for the task, unlike an insect whose tools are limited so he must have only a few to do everything it needs to. Multi-paradigm is the insectisation here.
Re:Functional programming is finally going mainstream by ncmncm on Thursday July 14, 2022 @02:33AM (#32092718)
From the original post:

> The right approach for the top level rarely matches that for most of its parts, nor each for the others, and likewise down the line. Unless you want to use a different language for each level and subproblem, you will want support for all "orientations" in your language.

It is grossly impractical to use a different language for each fragmentary part of a problem. People do not, as a rule, do that. Trivial exceptions include Python and niche-language programs calling out to C-ABI libraries, programs sending SQL to databases, and web servers sending Javascript to browsers. But those are not about "orientation".

You might use two or more different general-purpose languages for different, independent problems, but that does not contradict my remark about the folly of languages that attempt "purity".

Re:Functional programming is finally going mainstream by pjmlp on Wednesday July 13, 2022 @02:22AM (#32079564)
I like very much how Wirth puts it, Programs = Algorithms + Data Structures.

Everything else is syntactic sugar.

Re:Functional programming is finally going mainstream by ncmncm on Wednesday July 13, 2022 @02:30AM (#32079595)
If that were true, the past 30 years of progress in programming languages would be meaningless. But it is not: a more powerful language enables expressing what a weaker language does not.

A language meant for use by professionals should get powerful features, without regard to any spurious notions of "purity".

Re:Functional programming is finally going mainstream by pjmlp on Wednesday July 13, 2022 @03:33AM (#32079937)
Understanding what that equation means in practice is the difference between having a degree and a six month bootcamp as X Developer.
Re:Functional programming is finally going mainstream by ncmncm on Wednesday July 13, 2022 @08:06AM (#32081516)
Do you really think you want to go there?
Re:Functional programming is finally going mainstream by pjmlp on Wednesday July 13, 2022 @09:08AM (#32082113)
I have a thick skin.
Re:Functional programming is finally going mainstream by ncmncm on Wednesday July 13, 2022 @07:03PM (#32089830)
That is not the thickness we are concerned with, here. :)
Re:Functional programming is finally going mainstream by benrbray on Wednesday July 13, 2022 @05:43AM (#32080587)
Can you give any examples of useful non-terminating programs?

It seems to me all useful programs are terminating (or, we truncate some non-terminating procedure by some convergence criteria). The question is only whether we have the logical tools to prove termination at compile time.

Re:Functional programming is finally going mainstream by ncmncm on Wednesday July 13, 2022 @07:34AM (#32081264)
Programs running in microcontrollers routinely do not terminate; they run as long as there is power. Even many ordinary programs run until they are killed.

You seem to confuse algorithms, which by definition must terminate, with programs, which do whatever the hell you like (and sometimes things you don't).

Re:Functional programming is finally going mainstream by ParetoOptimal on Wednesday July 13, 2022 @07:47AM (#32081366)
That's because step 4 is really:

> 4. Spend a lot of time ripping out FP components i'm less familiar with improving the performance of and replacing them with imperative code I have years of experience improving the performance of.

The cause of mist if these kinds of things will be dominated by familiarity for a long time.

Re:Functional programming is finally going mainstream by throwaway234232 on Tuesday July 12, 2022 @11:45PM (#32078825)
I've ran into this before, but to be fair I was using Monad Transformers.
Re:Functional programming is finally going mainstream by kaba0 on Wednesday July 13, 2022 @06:54AM (#32080994)
Would you mind describing what sort of platform have you experienced such? Was it actually measured that the FP part was responsible for the slow downs?

Let’s be honest, most typical software written by the industry is not the like which has a single hot loop. They do a bunch of different things, usually with small number of entries. I don’t see how FP would be a hindrance to this typical workload.

Re:Functional programming is finally going mainstream by Akronymus on Wednesday July 13, 2022 @09:10AM (#32082146)
> Spend a lot of time ripping out FP components and replacing them with imperative code.

Or use the functional code as a form of tests?

Re:Functional programming is finally going mainstream by ithrow on Tuesday July 12, 2022 @11:49PM (#32078843)
That's the common theme even when using clojure too.
Re:Functional programming is finally going mainstream by peheje on Tuesday July 12, 2022 @11:44PM (#32078822)
What languages? Did you performance profile before to see what was slow?
Re:Functional programming is finally going mainstream by kstenerud on Tuesday July 12, 2022 @11:53PM (#32078868)
C++, Python, Kotlin, some others I can't remember anymore.

The most common issue is hidden O(n) situations, which don't show up except in production. So what we end up with is a mix of various paradigms worthy of Dr Frankenstein, and a debugging nightmare.

I've been in engineering long enough to see most trends repeat at least twice (Javascript UIs are ALMOST back to the point of the 90s for example). After awhile you learn to distrust anyone who evangelizses a "new" way that was actually invented in the 60s (and is likely being introduced as a panacea instead of with the original caveats).

Now that I'm a greybeard myself, I finally understand why greybeards were always so grumpy.

Re:Functional programming is finally going mainstream by yobbo on Wednesday July 13, 2022 @02:27AM (#32079585)
> hidden O(n) situations

How are these faster in imperative code?

Re:Functional programming is finally going mainstream by occamrazor on Wednesday July 13, 2022 @02:35AM (#32079622)
They are usually not hidden, so they are at least easier to identify.
Re:Functional programming is finally going mainstream by kstenerud on Wednesday July 13, 2022 @03:01AM (#32079756)
This is the crux of the matter. Every paradigm is argued about using small program examples, but the real-world issues come as the codebases become large (1M LOC or so). Then the cracks become worriesome.

Imperative non-OO codebases become swamps of maybe (hopefully?) layered code, where each layer contains its own sinkholes and completely bizarre structure (or lack thereof). It's basically a bunch of mini-codebases that meet in an ad-hoc manner, making it very difficult to understand what the hell is going on.

OO codebases encapsulate a LOT better, but working around its straitjacket design becomes harder and harder as you go through rituals like dependency injection, interfaces, adapters, facades, class clusters, and byzantine inheritance hierarchies. Now, rather than a swamp of despair, you have pebbles of functionality. And while these were great when your codebase was 50k LOC, it's now a gravel pile where you can't figure out where anything starts or ends.

FP codebases like to push the details out of the way, making it easier to see at a high level what's going on. But as the codebase grows, you now have multiple iceberg problems, where the devils in the details begin to derail your code in production, and suddenly you find yourself needing to delve down rabbit hole after rabbit hole, chasing issues that weave and bob in utterly chaotic ways due to the lazy evaluation. Reasoning about what it will actually do in what time becomes more and more difficult (putting it in vulgar terms: You're basically exchanging topical complexity for temporal complexity).

TANSTAAFL

Re:Functional programming is finally going mainstream by peheje on Tuesday July 26, 2022 @04:08PM (#32243302)
I haven't worked with a big FP codebase, but I actually thought about this when doing even small toy programs in FP. Sometimes I'm like... do they merge the multiple-sequential-maps that's possibly being done here? Does the code re-use the elements of map that doesn't change? Whereas was it C# or Go, I could just read it.

OTOH isn't that just - you need to know what the function you are calling is doing and how, and read the doc. Whereas in imperative you would implement it yourself, with the pitfalls of that.

If you are coding in Python and you are using a lot of numpy you need to know how to feed data in, how to get data out, etc. so maybe FP programming should treat the STD-lib more like an external library because the building blocks are a bit more high-level?

Re:Functional programming is finally going mainstream by Capricorn2481 on Wednesday July 13, 2022 @09:57AM (#32082793)
Do you pick the lesser of 3 evils, or are there solutions to these cracks?