4

I just looked up a few examples/definitions of the differences between Functional Programming and OOP. I found one definition that I thought was interesting and I understood the OOP part but needed a bit of clarity/example for the Functional Programming part.

"Functional programming tends to reuse a common set of functional utilities to process data. Object oriented programming tends to colocate methods and data in objects."

What is meant by functional utilities? I get OOP in general and so that definition makes sense to me. Thanks in advance

sgx
  • 1,265
  • 3
  • 19
  • 36
  • 1
    You can't understand an apple by understaning an orange. You still have to examine the apple. –  Dec 03 '19 at 10:04
  • Can you elaborate upon this comment? I think the responses and comments below have helped clarify my understanding of the original statement – sgx Dec 03 '19 at 10:27
  • You should look into typeclasses/ad-hoc and parametric polymorphism that greatly differs from subclassing and method overloading in OOP. You should look into algebraic data types and how they allow to structure your data through alternatives instead of hierarchies in OOP. You should look into treating effects like values and how to compose pure and impure functions instead of sending messages from one object to another, etc. –  Dec 03 '19 at 11:16

3 Answers3

3

There are a few core utility functions that really make functional programming feel different: map, filter, fold (also called reduce), and unfold. In addition, most functional languages make use of generic data structures that can be targeted by these operations, and data is represented by simpler types. Whereas in object-oriented programming you might have a MovieList object that contains Movie objects and has methods that iterate over or operate on those movie objects, you're more likely to just have the Movie objects in a functional language and use them in the context of a List of Movie data structure.

For example, imagine that you wanted the uppercase version of the movie title in an OO language. It might look something like this:

// This would, presumably, be a method on MovieList.
public List<String> getUppercaseTitles() {
    List<String> uppercaseTitles = new ArrayList<>();
    for (Movie movie : this.getMovies()) {
        uppercaseTitles.append(movie.getTitle().toUpper()); 
    }
    return uppercaseTitles;
}

In a functional language, a similar operation is more likely to look like this:

uppercaseTitles :: [Movie] -> [String]
uppercaseTitles movies = map (toUpper . title) movies

In other words, instead of giving step by step directions with a for loop and imperative method calls, you are declaring that uppercaseTitles consists of a map of the composition of two functions (toUpper and title) to a list of movies.

asthasr
  • 9,125
  • 1
  • 29
  • 43
  • 1
    I feel that your example is a bit disingenuous, since you picked one of the worst OO languages and one of the most elegant FP languages in your comparison. It would be more fair to e.g. use Java's `Stream` API as your FP example, or use a better OO language. For example, in Smalltalk, the OO example would look something like this: `movies collect: [:movie | movie title asUpperCase ]`. – Jörg W Mittag Dec 03 '19 at 04:27
  • 3
    My intent was not to pick a 'bad' language or an 'elegant' language. I was going for purity of concept, and I think the "plain" Java example with its imperative methods fits the _normal expectation_ of "OO code." I wanted to avoid things like Smalltalk's `collect`/`inject` because those are higher order functions, so I think they're confusing in this context as they're "functional" in nature. – asthasr Dec 03 '19 at 04:34
  • Thanks for the responses....so does that mean all FP include some form of map, filter, fold, unfold methods? Is that essentially a necessary condition (maybe not "officially", but in general more or less) – sgx Dec 03 '19 at 04:52
  • 1
    Yes, I'd say that's a base expectation. I'm not aware of any "functional" language that doesn't have those; in fact, as the discussion above shows, most modern OO languages have also adopted these and other functional idioms ([including Java](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html)). – asthasr Dec 03 '19 at 05:33
  • Thanks - so you're saying those are idiomatic for FP languages? Are they generally named the same as what you provided or do they have differing names sometimes. I think my last question would be: can you restate what the difference is between OOP and FP? Is it that one uses the concept of objects to accomplish things and the other doesn't have objects and uses various types of functions/methods? – sgx Dec 03 '19 at 05:40
  • 1
    Yes, those, and other higher-order functions, are idiomatic for FP languages. They are usually named the same, although they can have some differences: `map` can be `collect`; but `filter` can be `select` or `reject`; `fold` can be `reduce`, `inject`. The most basic difference is as stated in the answer: FP favors functions operating on simple data types, whereas OO joins data types and procedures together. – asthasr Dec 03 '19 at 06:20
  • Awesome thanks very much. – sgx Dec 03 '19 at 06:24
3

This is a false dichotomy because functional programming is about how you organise the computation and OO is about how you organise the data.

Scala is a good example of how the two can sit very happily together. It has strong functional features that make it easy to create and compose functions as first class objects. It also has a class system that makes it easy to create objects that combine data with the functions that operate on that data.

Tim
  • 26,753
  • 2
  • 16
  • 29
  • Ok - so FP can have both objects/classes and functions but the main difference is more about how to perform tasks efficiently (FP is more efficient by using various functions/methods and OO is more efficient at the same task in general by using objects/classes)? – sgx Dec 03 '19 at 07:02
  • 3
    I think you are really contrasting functional programming with imperative programming. In that comparison, functional is typically slower but more robust; imperative is faster but more prone to errors. Functional is also faster to write and easier to re-use (assuming the same skill level for both). – Tim Dec 03 '19 at 07:25
1

I think it can help you to see the simple difference, you can look on Java examples.

You can create method in classic way:

public int multiply(int a, int b, int c){
return a * b * c;
}
public int sum(int a, int b, int c){
return a + b + c;
}

But you can implement it in functional way:

@FunctionalInterface
interface Operation<T, R> {
    T count(R a, R b, R c);
} // or you can use Java 8 functional interfaces, if its usefull for you.

Operation<Double, Integer> divide= (x,y,z) -> x / y / z;
sout(divide.count(12,4,5); // returns quotient in Double type.
Opertion<Integer, Integer> sum = (x,y,z) -> x + y + z;
sout(sum.count(1,3,3) // returns sum in Integer type.

You are working with only ONE method - count(). On this way you can avoid duplicate of methods and just using functional way to operate what you need with. Anyway you are getting returned object(not primitive variable) because of generics.

But it is still not clear functional programming. If you are good in Java, you can try Scala(but it is not clear functional TOO. Good examples of functional languages are: Haskell, JavaScript, PureScript

andrew17
  • 851
  • 2
  • 10
  • 25