News for hackers. Stuff that matters.
Re:FP vs. OO, from the trenches by cygx on Monday July 22, 2013 @02:04PM (#6085370)
Actually, FP and OO are mutually exclusive to some degree:

OO comes from the imperative end of the spectrum (after all, objects are an abstraction over mutable state), whereas FP comes from the declarative end (a pure function is a relation between sets and mutability is a foreign concept).

However, most programming languages are impure and support both paradigms, so you can get away with thinking about OO and FP as orthogonal ways to reason about and structure your code; language syntax and semantics may of course favour one concept over the other...

Re:FP vs. OO, from the trenches by andolanra on Monday July 22, 2013 @02:53PM (#6085743)
You can have pure objects, depending on how you define 'object'. If your conception of object-oriented programming involves mutable objects changing state in response to messages... then yes, FP and OO are strongly at odds. However, if your conception of object-oriented programming involves information hiding via packaging functionality into units called 'objects' that expose public operations which are written in terms of some manner of implicit or explicit 'self' parameter... why not? That's what OCaml has, after all, and there was even an O'Haskell at one point[1].

Unfortunately, discussions on the merits of FP versus OOP tend to be asinine just because every participant has their own personal definition of both 'functional programming' and 'object-oriented programming', often based on implementations in a particular language. How does Haskell's idea of FP compare to Java's idea of OOP? What about Agda versus Python? Erlang versus Smalltalk? Does FP require purity, or types, or pattern matching?[2] Does OOP require inheritance, or private members, or classes? Without knowing these details, who can even tell what schools are being advocated or why?

[1]: In OCaml, objects can contain mutable values but they must be explicitly marked as such. An object with no mutable values is effectively a pure object, and there are many good reasons to use such an object—analogous objects are useful even in other languages not traditionally considered functional, e.g. new PureObject().someMethod().otherMethod() creates a series of objects which themselves needn't expose a stateful interface at all.

[2]: I'm not asserting that FP requires these things, but I have seen discussions where people clarify that languages can't be functional without function composition or algebraic data types or typeclasses or what-have-you, usually working off an informal definition of 'functional language' based on their language of first exposure.

Re:FP vs. OO, from the trenches by shadowfox on Monday July 22, 2013 @03:40PM (#6086024)
Now you have made me curious. Do you have any pointers to some source material about "pure objects"?

I find it especially interesting because under normal circumstances an object with no mutable state is no different from a collection of methods (which can be encapsulated in different ways of course; say as an OCaml module).

So I am curious as to what advantage an object system that allows only pure objects provide.

Re:FP vs. OO, from the trenches by andolanra on Monday July 22, 2013 @05:12PM (#6086484)
The William Cook paper 'On Understanding Data Abstraction' gives a basic formulation of pure objects. Crucially, an 'object' needn't be what the language itself calls an object, e.g. I could write up a 'set object' in Haskell, which does not have objects:

    data Set elem = Set { contains :: elem -> Bool }

    insert :: (Eq elem) => elem -> Set elem -> Set elem
    insert key set = Set { contains = c }
      where c key' = if key == key' then True else contains set key

    union :: (Eq elem) => Set elem -> Set elem -> Set elem
    union set1 set2 = Set { contains = c }
      where c key = contains set1 key || contains set2 key

    emptySet :: Set elem
    emptySet = Set { contains = (\ _ -> False) }

    -- mySet corresponds to {1,2,3}
    mySet :: Set Int
    mySet = insert 3 (insert 2 (insert 1 emptySet))

    -- myVal is True
    myVal :: Bool
    myVal = contains mySet 2
Depending on your definition of object-oriented programming, this could be considered an object or something else. William Cook argues that it's an object because access depends only on a public interface, i.e. I could add another implementation

    fromList :: (Eq elem) => [elem] -> Set elem
    fromList list = Set { contains = c}
      where c key = key `elem` list
which uses a different representation underneath the surface, but the previous implementations of union and insert will work with it because it exposes the same interface... which is not the case with ML/OCaml modules, which must select a single implementation. This is a simplified version of the example Cook himself uses, so I urge you to read the paper if you want to understand more. (On the other hand, people in the Smalltalk/Ruby/&c camp will say, "No, of course that's not object-oriented—you're not sending messages!" So... it's a complicated debate.)

[1]: http://www.cs.utexas.edu/~wcook/Drafts/2009/essay.pdf also linked by https://news.ycombinator.com/item?id=6084465

Re:FP vs. OO, from the trenches by silentOpen on Monday July 22, 2013 @04:36PM (#6086305)
http://caml.inria.fr/pub/docs/manual-ocaml/manual005.html#to...

If you have only pure objects, you can still perform open recursion and leverage structural polymorphism (including row variables). Additionally, your objects are just values and, because they aren't mutable, can be held for backtracking.

For instance:

```ocaml class virtual biggable x = object val x = x method x : int = x method virtual embiggen : biggable end

let o = object (self) inherit biggable 0 method embiggen = {< x = x + 1 >} end

let q f o = object (self) val x = o#x method x = x method embiggen = f {< x = x * 2 >} end

let a = q (q (fun x -> object inherit biggable x#x method embiggen = {< >} end)) o#embiggen

;; # a#x;; - : int = 1 # a#embiggen#x;; - : int = 2 # a#embiggen#embiggen#x;; - : int = 4 # a#embiggen#embiggen#embiggen#x;; - : int = 4 ```

Re:FP vs. OO, from the trenches by Fishkins on Monday July 22, 2013 @04:44PM (#6086350)
It's the topic of the third week of Martin Odersky's Scala FP course: https://www.coursera.org/course/progfun
Re:FP vs. OO, from the trenches by sampo on Monday July 22, 2013 @06:28PM (#6086887)
> In OCaml, objects can contain mutable values but they must be explicitly marked as such.

Or in Scala, you have to mark object's variables either as `val` (immutable) or `var` (mutable).

Like /u/Fishkins said, the Scala course in Coursera gives some examples of the use of immutable objects ( https://www.coursera.org/course/progfun ). I am sure they are used in a lot of places in Scala code, not only in Odersky's course.

Re:FP vs. OO, from the trenches by bad_user on Monday July 22, 2013 @02:38PM (#6085631)
OOP is not about hiding / dealing with mutable state, although it can be used that way, but that's a perverted idea of what OOP is.

OOP is about subtype polymorphism [1] by means of runtime single-dispatch [2] and modularity, as in abstract modules or components that help with decoupling, a notion supported explicitly by SML, or exemplified beautifully in the Cake-pattern used in Scala.

[1] https://en.wikipedia.org/wiki/Subtyping

[2] https://en.wikipedia.org/wiki/Single_dispatch

Re:FP vs. OO, from the trenches by cygx on Monday July 22, 2013 @02:55PM (#6085752)
For me, OO means stateful objects communicating by message passing, and same as asdasf, I consider it a way to structure imperative code. I don't consider this a perversion - it goes back to Kay.

Your definition is of course an equally valid one that might be more appropriate in certain contexts - it just doesn't fit my mental model of computation equally well.

Re:FP vs. OO, from the trenches by chipsy on Monday July 22, 2013 @03:30PM (#6085964)
I've developed the opinion, based in large oart on Kay's thinking, that the biggest wins of OO are better represented as a form of protocol design. The point of impedance comes in when you discover that not all protocols map cleanly to OO systems - some are synchronous and others asynchronous, some require more polymorphism than others, etc. Async message passing happens to be one of the more flexible mechanisms for protocol design, but it's only been relatively recently that industry languages have been considering it more carefully, and then it's framed as "concurrency" and not a general architectural consideration.

Likewise I see FP as a reactionary mechanism to write fewer and simpler protocols, because when the data is immutable the problem can usually be greatly simplified.

Re:FP vs. OO, from the trenches by munificent on Monday July 22, 2013 @03:33PM (#6085982)
> OOP is about subtype polymorphism [1] by means of runtime single-dispatch

I believe CLOS would beg to differ.

Re:FP vs. OO, from the trenches by cgore on Monday July 22, 2013 @04:54PM (#6086405)
Other than that I would agree with him though. Subtype polymorphism + runtime (single or multiple) dispatch.

Multiple dispatch is one of the things I really miss about Common Lisp. I do like OOP, but often the most intuitive way to think about something is multiple dispatch, and trying to shove it into a single dispatch model can produce some really ugly code. I'm pretty sure that is the main reason there are so many anti-OOP zealots in the world.

Also, I didn't realize Dylan had multiple dispatch. I should take a look at it sometime.

Re:FP vs. OO, from the trenches by andrewflnr on Monday July 22, 2013 @08:57PM (#6087523)
Clojure does, too.
Re:FP vs. OO, from the trenches by seanmcdirmid on Monday July 22, 2013 @04:35PM (#6086302)
OOP is most definitely defined by encapsulation of state into objects that have identities. There are many points to deal with messaging, on the other hand, as well as polymorphism, which are all compatible with what OO is from a design perspective. Otherwise, the anthropomorphic properties of the object are no longer sustainable, and it's noun-ness disappears.

The cake pattern has as much to do with encapsulation as it does crosscut composition.