News for hackers. Stuff that matters.
Re:Why study functional programming? (2012) by eatonphil on Tuesday August 02, 2022 @01:37PM (#32321860)
I think the best reason to study FP is to get out of the OOP mindset.

But ideally in the real world you don't go crazy with either.

Objects are excellent for data but OOP is a mess for control flow.

Immutability is great but everything-is-recursion is not.

And so on.

When performance comes up all of this goes out of the window, maybe. But I'm talking about good defaults for correct, readable programs.

Re:Why study functional programming? (2012) by rr888 on Tuesday August 02, 2022 @02:23PM (#32322442)
> OOP is a mess for control flow.

I used to think this until my new project where everything is functional, everything is reactive, everything is a lambda. Each function is very simple but the whole thing is impossible to trace.

Re:Why study functional programming? (2012) by freedomben on Tuesday August 02, 2022 @03:25PM (#32323143)
I've seen functional code like that as well, and it's truly awful. If all FP was that way I'd avoid touching it with a 1,000 foot pole. Most of the time though it's a developer who writes bad code no matter which language/paradigm they're in, but FP does give them tools to make it even more unreadable
Re:Why study functional programming? (2012) by pllbnk on Thursday August 04, 2022 @04:02PM (#32347601)
I have seen this before, but I think the problem is the code, not the paradigm. If you bend your mind a little and think of stateless functions as a chain of (OOP) method calls, function arguments as class instances and namespaces those functions as classes themselves, then there is little reason to think that it should be impossible to trace, given equal familiarity with the tooling.
Re:Why study functional programming? (2012) by jrvarela56 on Tuesday August 02, 2022 @01:59PM (#32322144)
| Objects are excellent for data

Check out DOP https://blog.klipse.tech/dop/2022/06/22/principles-of-dop.ht... and Rich Hikey's critique of OOP (~'objects are custom languages on top of data'; they force you to learn specific semantics to manipulate plain data structures and make it tough to reuse your code).

Re:Why study functional programming? (2012) by jcelerier on Tuesday August 02, 2022 @04:00PM (#32323552)
> (~'objects are custom languages on top of data';

how is that a bad thing - that's literally the point of the activity of programming, creating domain-specific sublanguages to manipulate data in a way that is readable by domain experts of this specific data

Re:Why study functional programming? (2012) by jrvarela56 on Tuesday August 02, 2022 @06:06PM (#32324874)
The argument is that this default forces you into creating DSLs for basic data manipulation (it's all [] and {} in the end).

You end up with 'custom' logic with custom naming that increases the overhead of using your codebase. That's a long way of saying your code is less reusable.

In DOP/FP land, you create functions to manipulate basic data types and your domain-specific knowledge ends up encoded as basic data types (say you want validations on a model, you'd encode those as a {} and create functions to validate vanilla []/{}).

Re:Why study functional programming? (2012) by dkarl on Tuesday August 02, 2022 @02:57PM (#32322838)
> Objects are excellent for data

Objects are great for modeling data as long as you still think of it as data, and you can write good data processing programs using OO language constructs.

OOP as an approach to programming gets it backwards, though. OOP encourages you to think of the objects as primary and the data representing them (on the wire, in a datastore) as a secondary, inferior manifestation. This is the opposite of true. The software's value is created by its handling of data on the wire, reading data from other systems and writing data to other systems. OOP objects provide no value except by reading and writing data. If you start believing that the objects provide the value and the data exists to support the objects, as OOP encourages, then you start to suffer from all kinds of delusions.

Re:Why study functional programming? (2012) by lostcolony on Tuesday August 02, 2022 @03:41PM (#32323322)
Once you start thinking that way, you start wondering why Objects, rather than (from Java) records, or (from most every other language) structs/tuples.

That is, why bundle functions with the data and encapsulate it as a new type that ties them together? It invites the very thing you're saying not to do; if you want to think of it as just data...then make it just data. Nouns only; no verbs. Even if you have a verb as data, treat it as a noun (i.e., a higher order function).

It's all translatable; you can break an object into functions and data, and create an object bundling functions and data, but, as you say, it's about how you think...and objects do not encourage you to think about the underlying data, but about the abstraction. And abstractions are leaky, don't translate well into new domains, and are much harder to communicate (both at an API level and between humans) than data.

Re:Why study functional programming? (2012) by vkou on Tuesday August 02, 2022 @02:45PM (#32322706)
The semantics of objects are pretty straightforward, but the assurances that they provide on top of your data is their main value add in this context.

Yes, things like protobufs, or equivalents thereof provide you with a list of auto-generated low-level assurances, but there are often bits of non-trivial business logic about data modification/reading that cannot be easily expressed outside of a object implementation in a Turing-complete language.

They also clearly express a concept of, and when used correctly, boundaries for data ownership, which for non-ephemeral data can be important.

Re:Why study functional programming? (2012) by pdpi on Tuesday August 02, 2022 @03:00PM (#32322865)
Funnily enough, I disagree — I find that the overwhelming majority of problems with OOP come precisely from trying to use it with data. The only thing it’s good for is precisely control flow at the macro level.
Re:Why study functional programming? (2012) by eatonphil on Tuesday August 02, 2022 @03:03PM (#32322911)
We'll just have to agree to stay away from each other's code. :)
Re:Why study functional programming? (2012) by oxff on Tuesday August 02, 2022 @02:01PM (#32322176)
> Objects are excellent for data

They are not. If your data is in objects, you have already demoted data as a first class member of your language (ie. Java)

Re:Why study functional programming? (2012) by cpursley on Tuesday August 02, 2022 @02:07PM (#32322254)
Just don't make the mistake of studying FP first...

I can't seem to wrap my head around OOP. There's too many concepts in OOP. In FP it's just data and functions for transforming that data.

Re:Why study functional programming? (2012) by Floegipoky on Tuesday August 02, 2022 @06:01PM (#32324822)
I started with mainstream procedural and OO languages like C++ and Java. I felt like I had a pretty good understanding of OOP by the time I left university. Later, learning a little about FP took my OOP to a whole new level. I really recommend reading Sussman and Steele if you haven't already, it's basically a constructive proof showing how to represent objects in a functional language. Unfortunately the title is so boring that generations of CS undergrads skipped reading the paper, so the zeitgeist forgot. Hewitt was another important researcher at the MIT AI lab at that time credited with discovering the Actor Model, which basically combines objects with a concurrency model. I've seen him post on HN occasionally =)

So many people think "OOP == classes", and I think it's really a shame that (in many ways superior) alternative representations were relegated to the sidelines for so long. I see Java's relatively recent inclusion of algebraic datatypes as a tacit admission that contemporary computing requires different primitives, and I expect to see a shift in best practices towards objects represented as immutable data structures combined with effectively-pure functions. Just like the FP folks have been saying all along!

> This work developed out of an initial attempt to understand the actorness of actors... Sussman suggested the experimental approach of actually building an "ACTORS interpreter"... When it was completed, we discovered that the "actors" and the lambda expressions were identical in implementation.

https://dspace.mit.edu/handle/1721.1/5794

Re:Why study functional programming? (2012) by cpursley on Wednesday August 03, 2022 @06:57AM (#32330057)
Thanks for the recommendations and link!
Re:Why study functional programming? (2012) by bitL on Tuesday August 02, 2022 @02:09PM (#32322283)
Can you explain monads to a 5-year old? Can you explain objects?
Re:Why study functional programming? (2012) by freedomben on Tuesday August 02, 2022 @02:18PM (#32322388)
I don't know if you're being serious or not, but you can learn a ton of FP without having to understand monads. Also asking to explain "objects" is too ambiguous. an "object" in json is very simple. OOP on the other hand involves a mountain of different concepts, design patterns, best practices, etc. I would guess GP is referring to that stuff.
Re:Why study functional programming? (2012) by LudwigNagasena on Tuesday August 02, 2022 @02:33PM (#32322574)
You can learn a ton of OOP without having to dive into a mountain of different concepts too.
Re:Why study functional programming? (2012) by yawaramin on Wednesday August 03, 2022 @07:13PM (#32337554)
You can learn a ton of FP without having to dive into a mountain of different concepts too.
Re:Why study functional programming? (2012) by pmontra on Tuesday August 02, 2022 @05:40PM (#32324622)
You can write OOP code without knowing a single pattern and whilst laughing at Factory classes you read in someone else's code. You might have to reinvent the wheel sometimes and face some difficulties talking with people that know every single pattern.

And there are best practices for every single language and paradigm.

Re:Why study functional programming? (2012) by unsafecast on Tuesday August 02, 2022 @02:59PM (#32322860)
Yes, I can definitely explain monads to a 5-year old.

See, a monad is like a box. You can put something in the box and close it very easily. But once it's closed, you can't open it anymore. The action of closing the box is called the wrap operation.

What you _can_ do though, is tell someone else to do something to the thing inside the box. For example, if you put a toy in and close the box, you can ask a friend to go ahead and add a new one in. Or remove the toy. These are monadic functions.

That's it. That's literally it. It's just a pattern of hiding the data and letting monadic functions be the only ones that deal with it.

In the case of the Maybe monad for example, you'd have the unwrap function that tries to get whatever is inside out, but it might not be able to (if the box is empty).

Re:Why study functional programming? (2012) by taeric on Tuesday August 02, 2022 @03:23PM (#32323133)
Not going to lie, I don't understand that explanation. It honestly sounds more like you are describing a form of encapsulation, as the contents of the box are hidden and someone else has the ability to open said box to get access to it.

I also find the appeal to a physical object to explain a functional concept is rather amusing.

Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @03:30PM (#32323210)
Not sure if you are looking for an explanation of Monads, but Monad tutorials are a long running joke in the haskell world. They are almost exclusively written by people for whom the concept just clicked and they either fail provide context for someone without that experience or they fail to actually describe the concept correctly.

I'm not gonna give you an explanation but I will say that IMO the best way to actually learn about them is to just look at the typeclass and a bunch of instances of it and how they are used. If you ignore all the theory (and the "theory") and just look at the code you will find that they aren't that hard to sort out from a usage perspective.

Re:Why study functional programming? (2012) by taeric on Tuesday August 02, 2022 @03:33PM (#32323244)
Apologies, I seem to have deleted a sentence where I think I can make my understanding fit that metaphor. I don't think they are completely unapproachable, but I am not at all clear that a physical metaphor works. And, I really wanted to shine a light on the amusing appeal to physical objects in a thread that is about the short comings of object oriented programming. :D
Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @05:45PM (#32324672)
No need for apologies! I 100% agree that the physical metaphor doesn't work and said so in another nearby comment.

If you go back to Phil Wadler's original paper on implementing monads in haskell, he doesn't talk about category theory or boxes or anything. He lays out a handful of common things you might do in programming but which seem totally unrelated. For each one he implements a solution and then reveals that all these solutions fit the same interface.

Re:Why study functional programming? (2012) by trebbble on Wednesday August 03, 2022 @02:43PM (#32335359)
At some point, it dawned on me that my problem wasn't that I didn't understand monads, it's that the idea's so simple I couldn't convince myself I understood it, and that what I actually didn't understand was why anyone gave a shit about them.

Like if a bunch of smart people keep going on and on about something called a "floobiz", and every time they try to explain it to you, it just looks like a fucking cup. The problem isn't that you don't understand what a cup (floobiz) is, it's that you don't get why they keep going on about them like they're something special.

Re:Why study functional programming? (2012) by ModernMech on Tuesday August 02, 2022 @04:10PM (#32323684)
> I also find the appeal to a physical object to explain a functional concept is rather amusing.

The challenge was to describe it to a 5 year old. 5 year olds are better with concrete thinking than abstract reasoning. This is what made using the turtle in Logo a genius move, as it allowed younger children to write programs by conceptualizing a physical turtle that can move, rather than thinking in terms of an abstract function that mutates data.

Re:Why study functional programming? (2012) by taeric on Tuesday August 02, 2022 @04:19PM (#32323772)
Fair, and I don't necessarily disagree. I think this leads rather close to object oriented software being easier to reason about concretely than functional programming, though. And since most people are trying to get a concrete job done, that feels like a significant claim.

For turtle geometry, it is also important to acknowledge the metaphor and shortcomings of a more traditionally analytic framing of drawings. Specifically, X and Y coordinates for a drawing are surprisingly hard to work with. For example, I challenge you to describe the fractal snow flake in an easier way than using directions as a metaphor. Same for the dragon curve. This can be seen akin to picking a different coordinate system, I presume?

Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @03:25PM (#32323145)
Unfortunately the container metaphor only works for a subset of monads. It fails with `IO`, `Reader`, `Writer`, `State`, `Parser`, etc.
Re:Why study functional programming? (2012) by AnimalMuppet on Tuesday August 02, 2022 @03:36PM (#32323272)
Well, for IO, it seems to me that maybe it still works. You're putting the entire external world in the box, but you're still (kind of?) doing the same thing.
Re:Why study functional programming? (2012) by unsafecast on Wednesday August 03, 2022 @03:48AM (#32328793)
You might be right. I'll try a different way:

You want to assemble a 16-piece puzzle, but there's too many of your friends around for you all to work on it at once, it would be a disaster. So you invent a game. The rules are:

- Everyone only gets to put a single piece. There's 16 of your friends, so it works great.

- You all sit in a row on the floor, and the puzzle gets passed from the last to the next.

- Nobody can talk. All you can do is put a piece in and pass the puzzle.

You then realize that there's actually 17 of you (you have 16 friends and forgot to count yourself) so every turn one of you sits at the end, receives the finished puzzle and checks it.

It works great the first few times (you have _a lot_ of puzzles), but then something happens. One of your friends lost a piece. They can't talk, so they just panic and the turn ends.

What you need to do is change the game a bit so this doesn't happen again (it's better to get no puzzle than a panicked friend). So you add two rules:

- If you can't add your piece for some reason, you pass a piece of paper saying why instead of the puzzle.

- If you receive a piece of paper, pass it to the next person.

With everyone equipped with the paper and the piece, the next turn starts. Your friend lost their piece again, but there's no problem: you've got a lot of puzzles, and more importantly, your friend knows what to do. They write ‘I lost my piece’, and give the paper to the next person.

You, sitting at the end, receive the piece of paper and know exactly what happened.

Congratulations, that's the Either monad with a puzzle as the Right, and a piece of inscribed paper as the Left, and the ‘bind’ operation is the rule set that explains how you communicate with the next person.

You can use the same kind of explanation for State, except you come up with a game that uses the same object as the state and every ‘friend’ makes something different that the next one needs.

Note: Saying you lost your piece out loud would be like throwing an exception. I guess you can re-use this as an explanation for that.

Re:Why study functional programming? (2012) by solomonb on Wednesday August 03, 2022 @02:12PM (#32335081)
I'm sorry, I know you put a lot of effort into this but I find it rather convoluted.

I honestly think the best way to explain the Monad typeclass is by just showing the code. It really isn't that complex:

    instance Monad (Either e) where
      return = Right
      Left err >>= _ = Left err
      Right a >>= f = f a
And then show how you can use `>>=` to chain operations on `Either` values. Then you show a second instance, such as:

    instance Monad (State s) where
      return a = State $ \s -> (s, a)
      ma >>= f = State $ \s -> let (s', a) = runState ma s in runState (f a) s'
Then explain how you can use `>>=` here to do something seemingly completely different.

Damnit, now I've accidentally written a monad tutorial :(

Re:Why study functional programming? (2012) by unsafecast on Thursday August 04, 2022 @12:15PM (#32344829)
> I'm sorry, I know you put a lot of effort into this but I find it rather convoluted.

Don't worry about it! I'm very happy to receive criticism.

I tried to do it in this way because the task was to explain it to a 5-year old. I haven't (yet?) met a person that young who could understand an abstract language like Haskell. It's just not happening.

My explanation is probably indeed way too convoluted, but I still believe monads can be intuitively explained to a 5-year old somehow.

Re:Why study functional programming? (2012) by peoplefromibiza on Tuesday August 02, 2022 @03:43PM (#32323331)
how about we call the box wrapper and the operations on it computations?

metaphors are good for beginners, to grasp the concept, they don't need to be perfectly valid.

for example the popular OOP metaphor a car is a vehicle becomes useless pretty much immediately.

Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @05:47PM (#32324693)
IMO the metaphor is already getting too frayed at this point.

I'm not opposed to metaphors in general, but metaphors are lot like abstractions. If you don't get the right one then the details leak everywhere and you may as well have not used it to begin with.

Re:Why study functional programming? (2012) by peoplefromibiza on Tuesday August 02, 2022 @02:47PM (#32322732)
monads are a very simple pattern

it takes 5 minutes to understand it, the jargon is complex, the implementation is not.

case in point

https://youtu.be/C2w45qRc3aU

objects are a very leaky abstraction that usually brings people to build taxonomies, mostly unrelated to the parent.

reuse of code, which is the selling point of OOP,also comes often short with a miriad of very specialized subclasses that have nothing in common anymore.

OOP is not horrible, but it requires a good amount of discipline to get it right, while FP has less concepts and you can (usually) silo the "and now for the tricky bits" (cit. Robert Virding) in a small core.

the rest is simple pure functions that have also the benefit of being stupid trivial to test.

Also, you don't have to be purely functional nowadays, and that helps a lot.

If only I learned FP sooner, my life as a programmer would have been so much better.

Re:Why study functional programming? (2012) by freedomben on Tuesday August 02, 2022 @03:29PM (#32323202)
You're point about testability is a good one that I feel is often missed. Trying to test functions in OOP code is often impossible because they presuppose some state in the "object" and if it's not initialized properly, it doesn't work. Then they also often make side effects. So you end up building a huge test harness to build up some state, run one test, and tear it all back down again only to rebuild it from scratch for the next test. It's doable of course, but it's easy for a simple CRUD app to have a 15 minute test suite and for tests to become flaky because of some unexpected state change done in some unrelated test on the other side of the code base. That's a special form of hell
Re:Why study functional programming? (2012) by goostavos on Tuesday August 02, 2022 @05:03PM (#32324282)
Monads are indeed way simpler. Just a few beautiful laws. The problem is that they take a loooooooooooong time to really internalize. Saying it takes "5 minutes" is empty in the same way that "learn C++ in 24 hours" claims are (or you're hella smart!).

There's a quote that floats around that's like "as soon as you understand monads, you lose the ability to explain monads." It takes a ton of leg work to make them click. They're simpler, but definitely not easy (in the Rich Hickey sense).

Re:Why study functional programming? (2012) by bitL on Tuesday August 02, 2022 @05:23PM (#32324453)
Ok, so you understood flatmap and therefore monads in 2 minutes. Now you need something that needs nested monads. That's where major complexity suddenly appears - your code is no longer simple and is full of artificial constructs like transformers just to do simple things. Moreover, anyone who reads your code must know all language constructs you are using or they are lost and can't fix any issue in your code.
Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @05:50PM (#32324719)
> Moreover, anyone who reads your code must know all language constructs you are using or they are lost and can't fix any issue in your code.

Does this not apply to every language ever?

Re:Why study functional programming? (2012) by peoplefromibiza on Tuesday August 02, 2022 @05:08PM (#32324326)
5 minutes is the time it takes to understand how the pattern works.

Be good at it?

More like a thousand hours.

there is nothing that is really simple, but before being simple, everything looks difficult.

p.s. I love Rich Hickey and especially his keynotes at Clojure conferences.

I've watched simple made easy at least 10 times.

Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @02:17PM (#32322378)
I don't think that is a fair comparison. Monad is a typeclass so it would be like explaining some specific Java Interface (and all the context for Interfaces) to a 5 year old.

The Haskell (since you brought up Monads) equivalent to explaining Objects would be like explaining Records or Sum types, which would be very easy to explain and don't require talking about inheritance like Objects would.

Re:Why study functional programming? (2012) by justinhj on Tuesday August 02, 2022 @09:15PM (#32326505)
Some functions return plain values. Other functions return fancy values with the plain values inside. There are different types of fanciness. One simple kind is called option. An option may have a plain value inside it or it may be nothing. Imagine looking up a phone number in the phone book. You get the name wrapped in an option or nothing because the person is not listed. Another fancy kind of thing is a Future. It runs some operation like looking up something in a database. When you call the function it just returns this fancy thing and at some point the database sends a response and that response is a plain value inside it. If you want the plain value you have to wait for the future. Well quite often you have functions that return fancy things and you would like to chain them together; lookup some user and then lookup their account. Instead of waiting for the first one and getting the plain value out then passing that to the second one and waiting for that, you could write some code that chained the two functions together for you. That composing of functions that return fancy things is called join or flatmap. If you also have a function that can put any value into a fancy thing (this is called unit or pure) you now have what is called a monad.
Re:Why study functional programming? (2012) by mejutoco on Tuesday August 02, 2022 @03:48PM (#32323405)
I’ll try.

A monad is a description of actions to do and their order, in place of the actual actions. This description can be passed around and eventually acted upon.

Objects (not Alan Kay’s original ones but say Java) are abstractions that model state and behaviour together. Objects can share common behaviour through interfaces

Re:Why study functional programming? (2012) by mdcds on Tuesday August 02, 2022 @03:49PM (#32323426)
I heard following explanation of monads somewhere online: "monads allow us to write imperative code in the functional setting. e.i. chain computations".

But that explanation wouldn't make sense to a 5-year-old

Re:Why study functional programming? (2012) by magicalhippo on Tuesday August 02, 2022 @05:21PM (#32324423)
> In FP it's just data and functions for transforming that data.

In OOP it's just data and methods for manipulating that data.

Re:Why study functional programming? (2012) by trebbble on Tuesday August 02, 2022 @03:18PM (#32323070)
> I can't seem to wrap my head around OOP. There's too many concepts in OOP.

Which ones? You're familiar with structs, right? OO is just structs with a little magic & sugar. Not even that much.

    Objects, methods, properties, instances, classes:
Imagine if, when defining a struct type, you could put references to functions on it, such that any struct of that type would contain those same fields with references to the same functions you put in the type definition. Then, if you create a struct of that type, the compiler and/or runtime will helpfully and magically appends an extra argument to those function signatures, assigning it some conventional name ("this", perhaps) and, if you call such a function "on" a struct of that type, the compiler/runtime will quietly, in the background, pass a reference to the struct you called the function "on" in that last argument slot, so that within the function you can make use of the function's "parent" struct (as, perhaps, a variable named "this").

The struct type with slightly-magical function references is a class.

The fields on the struct containing references to functions with the magical "this" argument appended when invoked, are methods.

A struct of that struct-type is an object, or instance of the struct type, if you will.

Fields on the struct are properties or members or whatever you like to call them.

    Static:
What if you could tell the compiler/runtime not to bother appending that "this" argument to some of those functions you attached to a struct type definition? Or to have a given field on a struct type definition always point to the same location for every single struct of that type, so that they all essentially share a single variable? That's what "static" means.

    Inheritance:
What if you could tell the compiler/runtime that it should associate one or more other struct type definitions with the struct type you're currently writing, and that if it can't find a given field (including ones that are refs to functions, aka methods) on a struct of this type, it should check an associated struct of the other type(s) and only error if it can't find it there, either. With the result that a struct type so constructed effectively contains all the fields of the structs associated with it, unless a duplicate exists on that child struct type, in which case that takes precedence.

That's basically inheritance. It's all about setting up and manipulating those kinds of relationships & precedence for lookups. That's all.

    Abstract, et c.:
Just ways to have the compiler enforce constraints and requirements on a struct type definition.

    Final
I do solemnly swear this is a constant, not a variable.

Now, there are implementation details under the hood for all this, but that covers actual usage, terminology, and concepts pretty well. You don't need to dig into the details of e.g. vtables (one tool for efficiently settling those inheritance-leveraging field lookups) unless you're implementing OO itself.

Re:Why study functional programming? (2012) by cpursley on Wednesday August 03, 2022 @06:58AM (#32330064)
Thanks! Your struct analogy was very helpful.
Re:Why study functional programming? (2012) by bitL on Tuesday August 02, 2022 @02:05PM (#32322221)
Immutability is slow, so if you are somebody squeezing the maximum FLOPS out of a CPU, you might want to stay away (sorry, 1.3x slowdown in your best case is not acceptable, and "leave it out to the compiler" is not a valid argument).

It's like with project management - the iron triangle of speed, quality, price, pick two. With each programming paradigm you pick a different set of attributes that might or might not be the best for what you need and they enable you on one thing and constrain you on another thing.

Re:Why study functional programming? (2012) by freedomben on Tuesday August 02, 2022 @02:22PM (#32322433)
Immutability can be fast or slow depending on the implementation. There are tons of copy-on-write implementations that blaze past mutable implementations. When the compiler knows that a particular variable/piece of data will never be changed, it opens up a ton of optimizations that can be used to make it fast. With mutable memory it's on the developer to optimize, rather than the compiler. Most of the time, the latter is better at it.
Re:Why study functional programming? (2012) by bitL on Tuesday August 02, 2022 @02:41PM (#32322677)
With infinite memory and "no cost memory allocation" you'll be likely correct, but in real world production loads you'll hit those limits quickly and then the immutability is likely getting in the way. Imagine just keeping a large (possibly compressed) bitmap (TBs in memory) to keep track of subscribers in your queue which is what many distributed queues use - how would you do it in immutable fashion? Copy the whole bitmap on each small change? There are simply many problems where immutability is the wrong answer if you want anything resembling performance.
Re:Why study functional programming? (2012) by freedomben on Tuesday August 02, 2022 @03:07PM (#32322955)
Absolutely, that would be one of the cases where "most of the time" is not "this time." Although for most developers/applications, I would imagine keeping TBs of bitmap in memory isn't a common task. Fetching some rows from the DB and transforming them into some sort of HTML or JSON probably is
Re:Why study functional programming? (2012) by kephasp on Tuesday August 02, 2022 @07:27PM (#32325625)
Do you have any examples? Tons of system use immutability with extremely high throughputs, most notably WhatsApp.

I don't need infinite memory to make a finite number of modifications to an optimized persistent data structure.

Re:Why study functional programming? (2012) by guelo on Tuesday August 02, 2022 @02:39PM (#32322661)
The problem is that developer has no idea if the implementation is going to be performant for their use case. The mutable compilers normally have less magic and thus are easier for the developer to understand and optimize. FP is really just a higher level language, that's why they don't teach algorithms classes using FP languages.
Re:Why study functional programming? (2012) by duped on Tuesday August 02, 2022 @02:17PM (#32322374)
Immutability is an API not an implementation. Immutable code often outperforms mutable, it depends on the use case.
Re:Why study functional programming? (2012) by bitL on Tuesday August 02, 2022 @02:36PM (#32322618)
There is often some "redefinition of terms" going on with dedicated functional programming folks in order to assert their claims as you just demonstrated. There was some benchmark where mutable Java was shown to be around 30x faster than immutable OCaml and the discussion went to something like "no, no, that's a wrong comparison, compare it with immutable vs mutable OCaml, that is then just 2x slower! And the next compiler revision will remove it! 100% sure about it!" etc.

Why can't other modes of thinking coexist with your view? FP is OK, so is OOP, so is imperative, low-level machine code, "loop programs" (see Dennis Ritchie dissertation), logic programming etc. I like them all but hate it when somebody tries to push me one way telling me all other ways are wrong.

Re:Why study functional programming? (2012) by freedomben on Tuesday August 02, 2022 @03:18PM (#32323066)
> Why can't other modes of thinking coexist with your view? FP is OK, so is OOP, so is imperative, low-level machine code, "loop programs" (see Dennis Ritchie dissertation), logic programming etc. I like them all but hate it when somebody tries to push me one way telling me all other ways are wrong.

Why do you think GP is saying or doing that? I think you've put a lot of words into their mouth that aren't there, and then got yourself worked up about it, enough to accuse them of ignorance and small-mindedness (at best).

I see absolutely nothing in their comment that even weighs in favor of FP, let alone saying other modes of thinking can't coexist. For all we know GP is a rabid OOP fan but sees the value in immutability (which FP certainly does not have a monopoly on).

Re:Why study functional programming? (2012) by bitL on Tuesday August 02, 2022 @03:32PM (#32323224)
OK, my apologies, that comes as a baggage from previous discussions with FP experts that routinely dismissed any opposing ideas and the OP's answer struck the same chord.
Re:Why study functional programming? (2012) by duped on Wednesday August 03, 2022 @12:36AM (#32327702)
Sorry I don't mean to sound like an evangelist, I just meant that there are misconceptions out there about immutable data structures (functional programming aside: the two are not so intimately involved that they're equivalent).

I hope you can understand why benchmarking OCaml against Java isn't necessarily fair, since they have two completely different implementations (the latter being possibly the most mature set of JIT compilers and runtimes ever built). A more interesting comparison are immutable DAs in languages like C++, for example the immer library which has some very impressive performance characteristics for the problems it solves. And other applications like immutable ropes used in text editors which have extremely high performance compared to mutable variations. At the same time it's not a silver bullet, plenty of other immutable DAs have issues that are non-negligible and applications domains where in-place mutable DSs are required like hard-realtime.

Software engineering is about tradeoffs and not dogma, and that's what I was getting at. A take like "immutable is slow" is the counter dogma to "immutable is perfect."

Re:Why study functional programming? (2012) by mrkeen on Tuesday August 02, 2022 @02:45PM (#32322715)
Awesome, I'd love to read it!
Re:Why study functional programming? (2012) by jstx1 on Tuesday August 02, 2022 @02:10PM (#32322295)
I mean there's other options too. Go for example is mostly imperative(?) - it doesn't encourage heavy OOP or pure functional programming. Rust has more functional features but it's still neither purely functional nor heavily OOP. Julia isn't OO at all but it doesn't emphasize FP either.

Those are just three languages that are all (1) modern, (2) generally well received, (3) neither OO nor heavily functional and (4) still very different from each other.

Re:Why study functional programming? (2012) by kalekold on Tuesday August 02, 2022 @03:20PM (#32323086)
> I think the best reason to study FP is to get out of the OOP mindset.

> Objects are excellent for data but OOP is a mess for control flow.

http://nomad.uk.net/articles/developers-who-hate-on-oop-don'...

Re:Why study functional programming? (2012) by cardanome on Tuesday August 02, 2022 @04:00PM (#32323545)
> If OOP is causing complexity in your program it's either the wrong tool for the job or you are using it poorly.

No.

The whole point about OOP is that it increases complexity. It uses higher levels of abstraction to organize the program.

Sure you might end up with a program that is possibly easier to understand and maintain and maybe even shorter but always more complex than the strict procedural equivalent. Simply cause you increased the levels of abstractions one needs to go through to understand the program.

That is, of course, not OOP specific. Introducing any form of abstraction introduces extra complexity. If you use the abstraction right, the problem becomes easier to handle but you always pay a cost.

Though the have a point with

> If you have used OOP well and the program still turns out more complicated than if it was written in a procedural style then you may as well use procedural code.

The art of software engineering is finding a level of abstraction that is appropriate for the complexity of the problem that is to be solved. Don't over-engineer but also don't under-engineer your software.

Re:Why study functional programming? (2012) by guelo on Tuesday August 02, 2022 @02:17PM (#32322381)
One of the bigger advantages of OOP that I rarely see discussed is the natural namespacing that you get. In other paradigms you end up polluting the global namespace more easily, harming autocompletion and discoverability.
Re:Why study functional programming? (2012) by pharmakom on Tuesday August 02, 2022 @02:42PM (#32322686)
Most FP languages have modules so not sure where this idea comes from.
Re:Why study functional programming? (2012) by kephasp on Tuesday August 02, 2022 @05:38PM (#32324589)
> Immutability is great but everything-is-recursion is not.

Why oppose a real pattern and a imaginary one?