4 Replies - 1958 Views - Last Post: 19 August 2010 - 10:54 PM

#1 deemeetar  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 66
  • Joined: 22-January 10

FP and OOP together. The way it should be done.

Posted 14 August 2010 - 05:57 AM

For some time i'm exploring functional programming and as i learn it(clojure mostly) i see that functional programming can have some challenges in practice. I think OOP is a must for some fields, for example game developing.

So do you think that the most efficient way to programm is combining OOP and functional. That is why i like clojure, it encourages you to FP but it doesen't erase the OOP completly.

I think with OOP and FP together, the sky is the limit.

Is This A Good Question/Topic? 0
  • +

Replies To: FP and OOP together. The way it should be done.

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5760
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: FP and OOP together. The way it should be done.

Posted 14 August 2010 - 07:54 AM

Pure functional programming is declarative. Part of what makes a pure functional language is a lack of side effects. Calls to functions are stateless.

In contrast, OOP in more traditional imperative languages implies state. An object is an abstraction that combines state ( attributes ) and the methods that logically influence that state. The entire encapsulation paradigm is to protect access to that state.

All computer languages impose some level of abstraction and force the user to consider the problem based on the design philosophy of the language. Some languages strongly adhere to a particular approach; some are kitchen sinks.

In a kitchen sink language, it's usually up to the programmer to decide on a consistent approach. This usually means that poor programmers can write even poorer code in such a language.

The language is, ultimately, irrelevant. Programmers can write good code or crap regardless of language. The best a language can do is be consistent and reliable. I'm not sure combining two divergent perspectives will help with that.
Was This Post Helpful? 0
  • +
  • -

#3 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: FP and OOP together. The way it should be done.

Posted 14 August 2010 - 01:50 PM

Well, the first important step is to realize that OOP is not really necessary for *anything*. C isn't a traditionally OOP language (not that OOP can't be done in ways), but how many games have been created with it over the years? I've seen games developed in Haskell, a purely functional language, with little problem.

Once you're accustomed to the new way of thinking, it really isn't that hard to see how things can be done. OOP is a nightmare for concurrency and doesn't really mix with FP's focus on immutability and no side-effects. Scala tries to combine these two paradigms, but ends up losing a lot of the benefits that come from functional programming languages. OOP and FP are generally conflicting philosophies. It's hard to mix them together without wholesale without one or the other suffering in problematic ways.

Even Haskell offers stuff like type-classes for polymorphism. You don't need classes and such to get the benefits of object orientation while keeping away the problematic parts. As you've pointed out, Clojure has stuff like multimethods, protocols, and records. Not to mention ad-hoc hierarchies with isa? relations with derive and stuff. It wouldn't be hard to actually write your own object system in Clojure. People have done it to demonstrate a point: an object system would hinder Clojure much more than it would help it, because what Clojure offers right now is much more powerful and flexible, and is perfect in conjunction with it's functional philosophy.

Object orientation is great for some tasks, of course. It was created for simulation, so it's definitely suitable for that. There is no reason that you can't accomplish in Clojure (or Haskell) what you can accomplish in <insert OOP language here>. OOP is not a must for game development. There is no reason you can't write video games in a functional language. It's been done. The important thing is that you teach yourself to think functionally. Once you "get" it, and you will, you'll be able to figure out how to do things in a functional style that you're only used to doing in an OOP style.

Clojure doesn't "erase OOP completely" because not all of OOP is bad. Clojure takes what it thinks is important, useful, and not a hindrance to Clojure's concurrent and functional philosophy in any way. In most cases, it takes object oriented concepts and makes them much more flexible than you'd expect in something like Java. When you're coming from an OOP background, learning FP can be a trying experience. Just try to program in a functional style. Eventually, things will click, and you'll wonder why you ever thought that a language without traditional OOP was limited.

This post has been edited by Raynes: 14 August 2010 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 pchapin  Icon User is offline

  • New D.I.C Head

Reputation: 34
  • View blog
  • Posts: 43
  • Joined: 23-July 10

Re: FP and OOP together. The way it should be done.

Posted 19 August 2010 - 04:02 PM

Scala combines functional and object oriented paradigms on the JVM. F# does something similar on the CLR. Objective Caml (OCaml) also provides both functional and object oriented features. Combining these paradigms in one language is not too unusual. It is certainly true that the two paradigms are fundamentally different. As has been said before, functional programming emphasizes immutable state and side-effect free functions. On the other hand OOP is all about modifying the state of various objects.

I think the fact that the two paradigms are so different is a great reason to try and integrate them into a single language. Assume such integration can be done in a reasonable way, the resulting language might be more "powerful" and more "expressive" than a purely function or purely object oriented language could be by itself. On the other hand the programmer must also expend more time and effort to learn and use the language properly.
Was This Post Helpful? 0
  • +
  • -

#5 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: FP and OOP together. The way it should be done.

Posted 19 August 2010 - 10:54 PM

View Postpchapin, on 19 August 2010 - 03:02 PM, said:

I think the fact that the two paradigms are so different is a great reason to try and integrate them into a single language. Assume such integration can be done in a reasonable way, the resulting language might be more "powerful" and more "expressive" than a purely function or purely object oriented language could be by itself. On the other hand the programmer must also expend more time and effort to learn and use the language properly.


There are bits and pieces of OOP that aren't fundamentally bad or against functional philosophy. Clojure does things that the community calls "abstraction-oriented programming". It has things like records, protocols, and multimethods. It doesn't totally abandon OOP wholesale, but takes what is deemed useful and fits in with Clojure's functional and concurrent philosophy.

And you're totally right about those other languages. It's feasible to offer both, but when you do, one side is always going to miss out. They are conflicting philosophies, so you can only go so far. Not that they don't do a good job at it. OCaml in particular is pretty excellent at it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1