10

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:

  • does functional programming use objects?
  • does it like prototypal inheritance?

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.

10
  • 1
    When formatting code, for large blocks use four spaces. For single line snippets, enclose the code in backticks. Don't mix the two for the same block of code. Commented Dec 14, 2017 at 20:52
  • dragon !== dragons Commented Dec 14, 2017 at 20:53
  • 2
    If you want to really learn functional programming, pick up a functional language such as Scheme, LISP or Haskell. Functional programming does not mean "uses functions" Commented Dec 14, 2017 at 20:54
  • Yes, you can use objects with functional programming. Redux is an example of a JavaScript library which does so. Commented Dec 14, 2017 at 20:55
  • funny thing, Object.keys(fireDragonJoe) returns the keys without element. Commented Dec 14, 2017 at 21:12

2 Answers 2

17

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.

  • Functional JavaScript: a great book that could help you a lot since you're already working with JavaScript. Its chapter 9: "Discover how to code without using classes", seems to be what you're looking for.
  • Learn You a Haskell for Great Good: in my case, when I started with functional programming I preferred to use a language that would not let me use object-oriented features, I wanted to use a language that would force me to think in a functional way, that's why I started with a bit of Haskell and this awesome book.
  • Programming Languages: a magnificent course in Coursera. When I took it, it was a single semester course, but they have divided it in multiple courses these days. The course covers fundamental functional programming concepts in SML and Racket and Ruby. So a perfect combination for what you want to achieve: learn the differences between FP and OO.
  • Functional Programming Principles in Scala: also a great course from Coursera.
  • Introduction to Functional Programming a course from edx.org that covers fundamentals of FP using Haskell.
  • Functional Programming in C# one of the best books I found out there.
Sign up to request clarification or add additional context in comments.

9 Comments

One might argue that Clojure's protocols are the purest form of OOP, nothing wrong with calling record instances "objects" :-)
Thanks for the answer. Unfortunately I am stuck using javascript, HTML, or excel visual basic. I am working on a project that I can install any new software. So I guess prototypes are the way to go?
@JustinGagnon even if you're stuck with JavaScript, you have plenty of options, for example you create your programs in Elm and transpile them to JavaScript. These days you have even other options, e.g. Kotlin. Under the hood even these will probably use whatever JavaScript offers today to provide their functional programming features. As I said in my answer, I don't see where you see a conflict between prototypes and functional programming. Honestly, I still don't get that part of your question so I'm not sure I can't help you on that one.
@JustinGagnon Prototypes on their own are not a solution to anything. They are not "the way to go", they're just a useful tool that you can apply when it fits. In fact JS has pretty powerful OOP through duck-typing and dynamic method dispatch even without any prototypical inheritance.
I guess my question is/was since prototypes and their inheritance are mutable doesn't that go against the idea of immutablilty of functional programming. If that makes sense. I am still very new to the concept of FP so I'm trying to figure it all out.
|
3

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.

2 Comments

what do you mean that FP is great for Tree? When inserting a new leaf node into a huge tree, we'll have to do a lot of rotations which will mutate the tree, i'm not sure i follow this one sentence "That’s why OOP is a great tool for matrix, because of mutability need, and FP is great for Tree."
@Aranha tree is defined recursively. So recursivity is the natural approach to implement tree algorithms. FP language implement recursivity appropriately in order to doesn't have stack overflow. So for all kind of recursive datastructure FP is great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.