3

I grew up being taught java, and I've started to learn a lot of PHP over the last few years using popular open-source CMSs. I really love the natural-feeling of OOP, but I've more recently discovered the concept of functional programming, which appears to be a difficult but elegant way of doing things.

In rtperson's great answer to the question "What is functional, declarative and imperative programming? [closed]", he says that "Then there's Object-oriented programming, which is really just a new way to organize data in an imperative program."

I think I understand what he means by that, but is it strictly true? Can OOP co-exist with functional programming?

Community
  • 1
  • 1
Dane Rossenrode
  • 188
  • 1
  • 7
  • 1
    Yes. Examples are Scala and OCaml. – ReyCharles Jun 26 '15 at 07:06
  • 1
    Some more examples are F# and Swift. – molbdnilo Jun 26 '15 at 07:32
  • 1
    yes, but OOP can never be [purely functional](https://en.wikipedia.org/wiki/Pure_function) since objects per definition contain state. for a well-designed purely functional language, I would recommend taking a look at Haskell – mb21 Jun 26 '15 at 10:57
  • btw, I find the [second answer](http://stackoverflow.com/questions/602444/what-is-functional-declarative-and-imperative-programming/8357604#8357604) to the question you linked to more precise than rtperson's which isn't really accurate. – mb21 Jun 26 '15 at 11:14
  • They are not "incompatible" but it's very hard to get the "best of both worlds" in a single program. You'll have to make compromises. I think selecting the way you want to do this is as much a design decision as any other. Object oriented design is better at growing in a certain way. Functional is good at accommodating a different kind of change. So, depending on how you expect your software to develop, you'll have to pick a style. – Noufal Ibrahim Jun 26 '15 at 12:05
  • @mb21 Objects don't need to *change* their state so they can be purely functional (eg. Java `String` is purely functional), it's only much faster to allow them to do that than to be immutable and return copies on each modification. – StenSoft Jun 26 '15 at 12:09
  • 1
    @StenSoft: it’s not always faster… – Holger Jun 26 '15 at 17:57
  • Just want to chime in that functional programming is not difficult, it's different. – shmish111 Dec 18 '15 at 13:57

3 Answers3

1

Yes, there is a term of "object functional programming". Basically in those languages a function is a "first class citizen" - an object.

I guess most agree it is not so easy to get there just because you have to know about all concepts - functional, OO and imperative.

Examples for such languages are:

  • Scala (I like it very much)
  • Boost::function , Boost::bind in C++
  • .NET F#
  • javascript (aka ECMAScript)
Martin Meeser
  • 2,784
  • 2
  • 28
  • 41
1

Yes it's compatible. You can program in a functional way in any language. An example would be Java String which is immutable and returns a new object if you do altering methods such as change case etc.

If you think about it o.something(y) is just osomething(o, y) and if you don't mutate o or do other side effects not related to OO it's functional.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
0

Yes. There is something called a 'functional object', which is basically an object where the mutator methods, instead of changing the state of the object, return a new object with modified state. Clean combines that idea with uniqueness types to keep the modified states single-threaded, which allows the compiler to implement methods by modifying the storage for the object behind the scenes.

Furthermore, there's nothing about mutable state that makes it 'not purely functional'; what's impure is when ordinary expression evaluation mutates state that's visible to the program. So you can combine OO and purely functional programming by making your object's methods return actions in the IO monad (or any other stateful monad) that mutate a common set of underlying state (not available to the rest of the program).

Jonathan Cast
  • 4,569
  • 19
  • 34