And where does your state go? How would a list of products be represented in FP?
The essence of FP is functions working on immutable data.
In FP, you have an immutable list of immutable products. This is basically the same as how when processing strings, you typically have an immutable array of immutable characters. If you concatenate two strings, you get a new immutable string. If you edit a string, you just get a new string. You don’t just edit the array itself directly.
Because everything is immutable, though, you can be smarter about updates. All copies can be shallow. Adding something to the front of a linked list is just a matter of making a new node that points to the old list. Arrays are harder, but you can use some clever tree-based structures like a Hash Array Mapped Trie to get fast updates.
So your state goes into the arguments of your functions.
demesisx@infosec.pub 1 year ago
I edited my comment to talk about imperative vs FP rather than OO vs FP because FP can actually be OO. What I meant was imperative.
Anyway, in most functional implementations, state is usually handled by a minimal top layer. Functional paradigms are helpful in keeping the complexity to a minimum.
I like to use the functional core, imperative wrapper design style.
Lmaydev@programming.dev 1 year ago
Right yeah.
Many OO languages lean into FP heavily as well.
Pipoca@lemmy.world 1 year ago
There are really very few OO really lean FP.
The essence of FP is working with pure functions that simply map their input to their outputs without mutating anything.
To do that, you need immutability by default. Very few OO languages do that in their standard libraries. Scala comes to mind as one of the few exceptions. There’s also a bunch of FP languages that bolted an object system into them, like Ocaml and F#.
There’s a lot of OO languages that take some features like closures from FP languages, but it’s typically a royal pain to actually do anything functionally in those languages. Java and python come to mind here. Javascript used to be a huge pain, but with spread syntax it’s gone down to only moderately painful.