I have been doing a lot of research on functional programming and I am really liking the idea of thinking of code as functions. The thing that I am not really gasping, and can't seem to get a clear answer to without directly asking are:
If it doesn't, then how do you structure your data?
An example might be like:
let dragons = {
name : "default",
age : 0,
element : "fire"
}
let fireDragonJoe = Object.create(dragons);
fireDragonJoe.name = "Joe";
fireDragonJoe.age = 3009;
Maybe I am just overthinking all this and the answer is just super simple. If anyone knows of a good reference material that actually teaches the why and how of functional programming, preferably for Javascript that would be great.
Many functional programming languages make use of objects.
A few examples:
The point being that object-orientation and functional orientation are not mutually exclusive concepts.
In these days, more and more object-oriented languages incorporate functional programming concepts. For example, C# and Java have support for functional types on top of their first class support for objects.
As such, I would say that there is nothing about prototypal inheritance that precludes a language from supporting functional programming features and vice versa.
Now, objects are not the only data structure that you can use. There are languages in which there are no concept of objects as in object-oriented programming and so you use other mechanisms to define complex types and data structures.
For example, in SML you can define tuples, records, or other forms of abstract data types. Haskell supports all these and more e.g. algebraic data types and type classes. Clojure supports a range of other data types like records, protocols, etc. Most functional languages have powerful support for lists, maps and other fundamental collections and composite data types.
So there's a plethora of other options out there. You just have to go out and play with a few other languages that are not object-oriented to find them out.
Functional Programming Resources
There are simply too many resources out there to learn functional programming. I can certainly recommend you a few of my favorites, but I'm pretty sure anyone would tell you something entirely different. You will have to start experimenting in order to find your own path.
These are some resources I have used myself in the past to learn FP.
OOP and FP are tools for making abstraction on problem solving. They are Independant. Programming language is like a tool box. You can use both tools in hybrid languages like ocaml and others, they don’t require one another, it’s up to you.
The principles used behind the conception of both tools are Von Neumann architecture for imperative and OOP, and lambda calculus for FP.
There is a link between your algorithm and your data structures . OOP manage states, due to the von Neumann roots. States are handled through mutability, and use for example loops. In FP, loops and assignments operator doesn’t exists. In FP you use recursive data structure heavily, and use recursion functions for exploring this data structure.
That’s why OOP is a great tool for matrix, because of mutability need, and FP is great for Tree.
Data structure will guide your choice of abstraction tool. You mix the tools, but you generally prefer to use the right tool for the right data structure, in order to avoid heavy code and performance issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With