News for hackers. Stuff that matters.
Re:Why study functional programming? (2012) by philipkglass on Tuesday August 02, 2022 @05:08PM (#32324322)
It's not possible to do always but it feels like it should be possible in special cases. If you can prove that a value V2 is ultimately derived from a modification of V1, and V1 is never referenced after creating V2, then it should be possible to invisibly convert this to an in-place mutation [1]. In the case of applying a bunch of text normalization rules to a string, for example, I care about the final string but not the intermediate states. It could be mutation underneath and I'd be none the wiser, except that the runtime performance would be better.

In Scala you can add a "@tailrec" annotation to recursive functions. The annotation means that the function is guaranteed to recurse efficiently (without growing the stack) OR that the compiler will give an error telling you why it can't be guaranteed. I could imagine a similar annotation like "@withmutation" for functions where you want to guarantee efficient in-place mutation at runtime. As it is, when I run into performance pain points in Scala I redo the critical bits with (manually written) while-loops and imperative mutation.

[1] Unless you later attach a debugger and expect to be able to observe V1 and V2 simultaneously. But I presume this sort of issue already comes up and is somehow dealt with regarding other compiler optimizations.

Re:Why study functional programming? (2012) by cassepipe on Tuesday August 02, 2022 @06:34PM (#32325138)
I think this is the idea behind Richard Feldman's Roc programming language https://www.roc-lang.org/
Re:Why study functional programming? (2012) by riwsky on Tuesday August 02, 2022 @07:32PM (#32325666)
And just like that, you’ve invented Rust!
Re:Why study functional programming? (2012) by frostwarrior on Tuesday August 02, 2022 @07:59PM (#32325911)
AFAIK this is the idea behind ImmutableJS: https://immutable-js.com/
Re:Why study functional programming? (2012) by lyxsus on Tuesday August 02, 2022 @11:32PM (#32327350)
Isn’t it similar to linear types in haskell?
Re:Why study functional programming? (2012) by eru on Wednesday August 03, 2022 @04:28AM (#32329084)
That's basically exactly the same idea, yes.

Haskell didn't invent linear types. Eg Clean had them for ages to do IO. (They didn't use Monads in Clean.)