News for hackers. Stuff that matters.
Re:A Year of Functional Programming by noelwelsh on Monday June 09, 2014 @07:47AM (#7867948)
Wow, I sure have the opposite reaction to you. Makes me wonder what kind of code you wrote. Some specific points:

"One is code reuse: yes FP code is definitely more reusable. The problem is that the kind of code it enables reuse of more than OO is very small, simple loops."

Combinator libraries sure are pretty useful, and they're more than small simple loops.

"Writing the same 4 line method -- that could have been abstracted with a monad -- 10 times in a 50-150K LOC project is never a big problem"

All our goes code through a bunch of monads (mostly Future + Writer). Very puzzled how you're using monads if they only appear 10 times in 50KLOC.

"Another is state management"

I think you're referring to STM here. That's been extensively explored in the Haskell community.

Re:A Year of Functional Programming by pron on Monday June 09, 2014 @08:25AM (#7868064)
> Combinator libraries sure are pretty useful, and they're more than small simple loops.

Useful -- yes. Useful enough to pay the price -- IMO, no. OO is decent at code reuse, and the most effective, most important code reuse, is that of big libraries with rich functionality, which is done rather well in many programming paradigms (i.e. pure, statically typed FP has little to no advantage here).

> Very puzzled how you're using monads if they only appear 10 times in 50KLOC.

I'm not. But OO code that refactoring with monads would have saved me does not amount to much.

> I think you're referring to STM here.

I was talking about more limited transactional (or "concurrently mutable") data structures like Clojure atoms and agents, or restricted STM like Clojure's refs and in-memory DBs (general purpose STM is pretty much dead). I know Haskell explores STM. I'm just saying you can enjoy the same benefits in Clojure or even Java.

Re:A Year of Functional Programming by noelwelsh on Monday June 09, 2014 @08:44AM (#7868153)
I don't agree with your definition of FP then. Erlang is broadly the same as Clojure: pure in the small but stateful in the large (mailboxes in Erlang store state).

Your basic argument seems to come down to Haskell (the only pure statically typed language with any widespread adoption) vs any other language. That's not an argument I'm particularly interested in.

As for OO vs FP in combinator libraries -- the same pattern is often called a fluent interface in OO. This gets to the larger point that FP is as much a programming style as a language that supports that style, and you can program in a FP style in any language. The question I'm interested in is "is FP style useful?" to which I give a resounding "yes!"

Re:A Year of Functional Programming by seanmcdirmid on Monday June 09, 2014 @10:10AM (#7868515)
All Erlang, Clojure, Scheme sport pretty nifty object systems, even if they don't resemble the Java kind. I find it funny when someone espouses the benefits of functional programming with...objects? On the other hand, these languages are quite pragmatic and I believe such blending is the future.

The only language that is purely FP is Haskell. It doesn't have anything that we could mistake for an object system, its functions all the way down!

Fluent interfaces should die a horrible death; it is a confusing style in OOP and quite easy to make lots of errors without the equational reasoning that supports combinators in FP.

Re:A Year of Functional Programming by tel on Monday June 09, 2014 @12:27PM (#7869120)
Haskell completely has something you can mistake as an object system! The entire typeclass machinery works that way less classes and inheritance. There's even a highly functional subtyping relation.
Re:A Year of Functional Programming by seanmcdirmid on Monday June 09, 2014 @12:55PM (#7869248)
Type classes work strictly over values, with none of the naming machinery to give these values names and identities. Type classes only resemble classes in their unfortunate label; subtyping and polymorphism isn't really exclusive to OOP (with the exception of name-oriented nominal subtyping). The only way to create an object in Haskell is through giving it a GUID of some kind.
Re:A Year of Functional Programming by tel on Monday June 09, 2014 @07:17PM (#7871059)
I'm not claiming typeclasses are classes, but more like modules and that modules achieve at least some of OO. Entity-like identity is a notably more difficult thing to pull off, but still is easily embedded in IO or ST.

I'm not saying it's a complete OO implementation, but instead that it's not so far as to be completely unconfusable, though it might be hard to, say, do Smalltalk-in-Haskell.

Re:A Year of Functional Programming by seanmcdirmid on Monday June 09, 2014 @09:38PM (#7871489)
Typeclasses don't give you any specific to OO: they are abstractions over values and that's about it. You wouldn't be able to emulate an existing OO system in Haskell, and anyways, it wouldn't bring you closer to "talking about" and "thinking in terms of" objects.

My point that OOP is essentially "thinking in terms of named objects" whereas most of what Haskell focuses on is "expressing math by composing anonymous values." I believe this starting from a design perspective is the best way to highlight the real differences between OOP and pure FP.

Re:A Year of Functional Programming by tel on Monday June 09, 2014 @10:45PM (#7871634)
I disagree, but I think we'd have to go to definitions to make headway on this conversation, so I'll back out. As final thoughts: while type classes give abstraction over values, (a) those values encompass HKTs so they can also be effectual in various ways and (b) universally quantified bounded types are a basically final algebras and codata goes a long way toward modeling objects. Between those two I think you can give OO in Haskell a run.
Re:A Year of Functional Programming by seanmcdirmid on Tuesday June 10, 2014 @10:15AM (#7872581)
We then have to argue about what objects really are, and there isn't much consensus :) I don't think my design-oriented linguistic definition is very popular, but it contrasts nicely with pure functional programming; otherwise there is a lot of overlap and I don't think we can say anything very interesting about their differences.
Re:A Year of Functional Programming by tel on Tuesday June 10, 2014 @10:47AM (#7872797)
Yep, exactly :)

I don't find definitional wars interesting, and I'd, in the stream of everyday things, be very sympathetic to "OO and FP are different because they were built from different POVs". The only reason I am being a stickler is that I'm pretty interested in the idea that both FP and OO have a representation in type theory/category theory/logic and thus can be put into common representation.

Haskell definitely isn't the best OO language, but if you squint hard enough you can see OO in it much like you can see FP in C's function pointers: painfully. Seeing FP in C helps you examine the implementation challenges of FP. Seeing OO in Haskell helps you pick apart the different semantic pieces which build to form OO.

Re:A Year of Functional Programming by seanmcdirmid on Tuesday June 10, 2014 @11:47AM (#7873226)
I would go farther and say Haskell is the only popular general purpose language that can get away without exposing programmers to object-like abstractions in the general case. Of course, these languages are general enough that you could probably shove whatever paradigm you wanted on top of them, but they do optimize for a particular kind of paradigm.

A type theorist's definition of OO will be quite different from a generalist's. For type theory, various kinds of polymorphisms mostly overlap between paradigms (e.g. generics and structural subtyping are useful in OOP), but nominal typing is both commonly associated with OOP and relatively useless in FP languages to be a distinguishing feature. This also dovetails with my named-oriented definition of OOP: objects have identity, but their types have identity also. If you come at it from the FP perspective, identity for objects AND types aren't very desirable...there are no meaningful names in math!

Re:A Year of Functional Programming by tel on Wednesday June 11, 2014 @10:13AM (#7878073)
That's an interesting claim—that Haskell doesn't need object-like abstractions. I think it's really worth examining. I've said in the past the Haskell (and the ML-family) is possibly the only language which takes seriously initial data. That said, it still needs jerry-rigged abstract types to be efficient and I'm happy calling codata "object-like".

So really what I believe is that Haskell does a better job of being OO-FP than Scala does. I think there's still a happier medium between the two, though.

I'm also really eager to explore the type theorist's definition of OO in order to tease and bend the generalists definition. I'm not sure I completely agree about the idea of names that you outline, though. Existential types (and their dualized universal presentation) present unique types which could be named. State-transformer style (and the state-like monads) present a notion of identity-as-entity.

Again from the perspective of Haskell being a stab in the direction of a type theorists FP-OO language I find all of these features really interesting.

Re:A Year of Functional Programming by dllthomas on Wednesday June 11, 2014 @10:24AM (#7878133)
I feel like the nearest thing to OO in Haskell in common use is the records-of-closures pattern. Still doesn't have persistent identity, of course.
Re:A Year of Functional Programming by tel on Thursday June 12, 2014 @12:41AM (#7881843)
Records-of-closures is pretty much what I'm talking about. There is a notion of identity when those records are recursive, though the state management is still manual (as you'd expect).

    data Complex = { rotate :: Degrees -> Complex }

    -- thus
    -- rotate :: Complex -> Degrees -> Complex
where `rotate` is a state transformer and thus produces a notion of identity.
Re:A Year of Functional Programming by dllthomas on Friday June 13, 2014 @08:29PM (#7891584)
I think it's a stretch to say this, itself, provides any notion of identity. You could certainly implement identity with any of the myriad ways of maintaining references with state, of course.
Re:A Year of Functional Programming by tel on Tuesday June 17, 2014 @04:31PM (#7906810)
Of course you're right, but I think a function like

    Complex -> Radian -> Complex
offers two points of view. You can see it as a function which transforms complex numbers or a function representing an update to the internal state of a Complex object. The second highlights the need to keep track of identity. Throwing an STRef into the mix just gives you a particular kind of history—one which only retains the "now".

Which is all sort of obvious and meaningless, but I still think it's interesting to think about. I think coalgebras tend to force thinking in terms of identity in this kind of way.

Re:A Year of Functional Programming by pron on Monday June 09, 2014 @08:48AM (#7868169)
I agree with every word. Alas, FP does not have a definition, so, when I was saying "FP", I meant "FP as the article's author practices", which means "statically typed, pure FP". FP using Java streams, FP in Clojure/Erlang, and FP in Haskell mean very different things.
Re:A Year of Functional Programming by noelwelsh on Monday June 09, 2014 @09:09AM (#7868252)
Agreement with someone on the Internet? o.O

;-)

[To the downvoters: this is me replying to the conversation I've had in the previous posts in this thread. If you can't handle my surprise and happiness at reaching some agreement hit up 4chan.]

Re:A Year of Functional Programming by seanmcdirmid on Monday June 09, 2014 @10:20AM (#7868558)
STM applies to OOP just as much as it does to FP. There is nothing magical about monads that make STM possible, they just make building the infrastructure for STM easier.