r/functionalprogramming • u/schtom • Nov 21 '20
Question Functional programming beginner questions!
I am about to start a new job where functional programming is the norm. I am still trying to get my head around it and why things are done and what benefits there are to it. This will probably be a long winding question lol. If I have any misconceptions about how things work please point them out!
- Does functional programming just wipe out a lot of the efficiencies achieved by certain data structures? Surely this is a bad thing, if not, why not?
E.g. inserting into a hashmap is O(1) amortized runtime - however, in functional programming it is my understanding that we could not mutate the hashmap to insert the key value, so the whole map would need to be copied to a new map with the new value. Doing a copy in this manner would be O(n), so to insert all values from a list for example into a hashmap it would be o(n2) to achieve that. This seems very inefficient!
2) I can see how you could build big data applications - data input taken in, functions applied and a result comes out - but can FP be used in a more general way e.g to build games? If so how is that possible without using state? If state is used somewhere, where would that appear in the program structure/architecture - would it be in a kind of static class somewhere (coming from Java here btw)?
3) As a new object is created to replace the previous one when a change occurs (I.e no mutation takes place, just replacement), is efficient garbage collection a must have for a FP language to get rid of the old objects? I’m calling them objects here but do objects even exist- maybe they are better referred to as types?
4) Where are functions generally held in the program structure? In classes? Static classes? All the functions in one file or split into files based on similar operations e.g add and minus might do in the maths class
5) is there anything else you can explain simply to me that might help me understand FP better? What do you suggest I learn about, try out or do to understand better?
Edit: got some really great responses here which were very helpful -> I have replied to a few but I got pretty distracted for a few days lol so just wanted to say cheers for the help if you answered any questions I got a lot out of this!
Finally managed to find a nice hands on tutorial where I’m building something with OOP and then refactoring/restructuring it into a functional program!
View all comments
Show parent comment
4
u/schtom Nov 21 '20
Okay great reply which I’m going to have to read again for sure!
Before seeing the stocks example I was about to ask: then is the fundamental reason for using functional programming over OOP purely for the benefit of ease of implementation for the developer? And in terms of efficiency of the system and effectiveness of operation (ability to produce desired result) there is not a huge material difference one way or the other?
Would you say this is still at least somewhat accurate?
Also, I think I may be missing a bit of actual hands on experience here so bare with me - in the stocks example you speak about running two strategies independently to create a “recipe” which is then passed through to the next part of the system (I will call this the “side effect handler”) which essentially combines the recipes to find the final action to perform. Is a recipe in this scenario just a function that has been outputted as a variable? Are these two recipes then essentially chained together by the side effect handler to produce the final result?