views:

933

answers:

11

What languages are available that promote both object-oriented and functional programming? I know that any language that supports first-class functions can be considered functional, but I'm looking for a syntax that's specifically targeted for both coding styles.

Using such a language, I'm imagining isolating all state changes to a single portion of code and having the rest of the program be purely functional. Just thinking of it makes me drool (debugging heaven!).

So far I've discovered Scala although I've only just heard of it (and it looks amazing). Are there any big contenders in this "mixed style" paradigm?

+9  A: 

Also, many scripting languages such as Python, Ruby, Lua, etc. have this capability, but lack many of the nice features of functional languages such as algebraic data types and pattern matching.

Zifre
+9  A: 

OCaml and F# are the most popular languages that mix OOP and FP, as far as I know.

Most languages, like Ruby, mix functional programming in, but a lot of people don't even realize it. I find languages like that leave a lot to be desired on the syntax front and such.

Rayne
+12  A: 

The best known are OCaml and F# (which can be vaguely described as OCaml for .NET).

There are many other multi-paradigm languages, like Oz, but they have mostly pedagogical value. By contrast, OCaml is very practical. It's almost as fast as C and almost as beautiful as Haskell :)

The popular scripting languages like Python and Ruby let you program in functional style as well. However, they don't provide one of the strongest facilities that "classical" functional languages (as well as OCaml) have: pattern matching (don't mistake it for regexp).

Igor Krivokon
OCaml and F# are certainly almost as pretty as Haskell syntax-wise, but they do not let you abstract over type constructors (no higher-kinded types). Scala isn't quite so pretty, but it does give you higher kinds. If you just want to write prettier code, that's fine, but if you want more powerful abstractions, the Caml variants won't get you there.
Apocalisp
A: 

Ruby! In Ruby, everything is an object (even literals), and it also has full functional programming support.

perimosocordiae
Everything being an object isn't necessarily OOP, is it? In JavaScript, just about everything is an object (including functions so you can do some functional programming stuff).
Nosredna
...and JavaScript is also OO and functional...
DeadHead
JavaScript is certainly object-centric, but when people say "promotes OOP," I think they're talking about classes. I know, the terminology is weird. BTW, JavaScript is my favorite language ever (due to its flexibility), so I'm not dissing it.
Nosredna
OO has nothing to do with classes. A lot of OO languages don't have classes: Smalltalk-71 (which is, after all, the language for which the term "OO" was invented), Self, Io, Ioke, ECMAScript (aka JavaScript), NewtonScript. Alan Kay has made it very clear that he considers having added classes to Smalltalk a mistake, and since he *invented* the term "object-oriented", we could even go so far as to say that languages with classes are *not* OO!
Jörg W Mittag
I don't think Alan Kay gets to decide anymore what OO is and is not. I did find this on wikipedia: 'Armstrong, The Quarks of Object-Oriented Development. In descending order of popularity, the "quarks" are: Inheritance, Object, Class, Encapsulation, Method, Message Passing, Polymorphism, Abstraction'
Nosredna
According to that list, Self (one of the *most* object-oriented languages ever invented), Simula (the first OO language ever), Smalltalk-71 (the language for which the term OO was invented), JavaScript, Io, Ioke and NewtonScript aren't OO, because they don't have classes. C++, Java, C# aren't OO because they don't have Message Passing. CLOS and Eiffel aren't OO because they don't have Methods. In fact, *no* language in existence today, with the exception of Oz, has these features. Are you claiming that Oz is the only OO language?
Jörg W Mittag
Also, Alan Kay invented the term "Object-Orientation". Thus, he gets to decide what it means. If you want to invent your term, that's fine, then you can define it to mean whatever you want. But arbitrarily redefining well-defined, widely used technical jargon is *not* a good idea, because it makes it pretty much impossible to have a meaningful discussion.
Jörg W Mittag
+7  A: 

Javascript: OO and functional.

Evan Meagher
Sorry, Accidentally voted down the wrong answer. :p
Rayne
+4  A: 

JavaScript, Python, and Ruby could be used that way, but Scala bumps up a notch by typing the function statically and working under JVM.

eed3si9n
Python is a cool place to learn some FP principles.
Jared Updike
Why would anyone ever use Python to learn functional programming? :\
Rayne
A: 

I'm wondering why you are looking for a language that specifically encourages mixing it up rather than just doing it anyway with a language that does functional programming and OO programming well? It could be easily brought into effect with Python, Ruby, Perl or similar interpreted languages. In addition C based OO languages tend to mix pure C with OO features, for example Objective C could easily be written in such a way should you choose.

EDIT: I have been made aware I'm incorrect, I've left this answer here incase someone can learn from my mistake - see comments.

Toby
Because trying to program functionally in a language like Python, is like trying mow 10 acres of land with a pair of scissors.
Rayne
Yeah fair deal, it can be done but its not the ideal approach. I'm still not sure that C extension is that bad an idea though, I'm less familiar with c++ but the notion of doing such a thing in objective c doesn't seem that bad. You can program functional C to your hearts content and use objects too if you want, I can't see why c++ wouldn't do it easily. I think my stupidity might be blinding me ;)
Toby
How big are the scissors?
Nosredna
How is C or Objective-C functional?
Nosredna
After reading the bit about haskell and now have some idea of what is being meant by functional.
Toby
Toby, maybe you were thinking "procedural" like C or Pascal. I can see how that could get mixed up with "functional."
Nosredna
@Nosredna Surgical small.
Rayne
Nosredna - Thats exactly what i was thinking. I mostly asked because I'm fairly new and couldn't see what he was getting at, I knew I must be missing something
Toby
+1  A: 

You're really asking the wrong question. Your question is premised on there being a distinction between "OO" and "functional" programming. This distinction isn't interesting or relevant. In fact, according to the "supports first-class function" criteria, even Java is functional.

But you hit the nail on the head with "purely functional". That's the interesting bit. What languages offer you referential transparency and purity like that? Well, most of them, if you're very careful. But not many of them actually guarantee that your functions are pure. I can only think of a couple of languages that offer you that.

One of them is Haskell. In Haskell, you write programs that are purely functional from beginning to end. Side-effects are delegated to a data structure called IO, and state is handled by passing it through pure functions or encapsulating it in monads. So you have your "debugging heaven" where only a small portion of your code interacts with global state and the rest of your program is pure, and purity is enforced by the language.

Apocalisp
I'm not as concerned with purity as much as readability. I know that many languages support many programming styles but a language's syntax largely determines which style is widely accepted. I think Scala is the first I've seen that says, "We're trying to do both!"
Kai
+2  A: 

C#. It's imperative, which can be handy, but also has a lot of functional features. Lambdas, iterators, and LINQ are all functional.

It probably won't appeal much to purists, but it works for me.

Scott Wisniewski
Nice attitude there.
Rayne
That last part was my idea of a joke.Generally I'm not one for "purisim".Aparrently, though some people took offense.
Scott Wisniewski
I wasn't trying to offend anyone.
Scott Wisniewski
I don't find it offensive (to anyone), but this has attracted a few "offensive" votes; I recommend you re-word the final point. Which is fairly pathetic (not of Scott, but on the part of the "offended"), as this post clearly isn't "offensive". But there we are.
Marc Gravell
*sigh* I updated the post to something less "offensive".
Scott Wisniewski
+1  A: 

As long as you don't insist on "purity", Common Lisp supports all your needs.

Svante
Unless you need static typechecking.
Apocalisp
I'm on the fence about Static vs Dynamic typing. I personally love Clojure, and Haskell.
Rayne
Well, static typing and real object orientation are contradictory. You can't have everything.
Svante
+1  A: 

Haskell: Pure functional ,pretty much no OO, but go ahead, take the dive. :D

Scala: Beautiful mix of OO and FP, could possibly overtake java as premier language on the JVM in a decade or 2. I like it because it brings functional programming to the java platform, something that's sorely need IMHO.

C#: Awesome support for OO, as well as getting more functional (first class functions already, we'll see what improvements .net 4 brings)

F#: .net language Built to be functional specifically, as opposed to C#, which was originally conceived for OO stuff.

Python: Great for OO, but not at all suited to FP

Javascript: Supports first-class functions, but not specifically designed for FP like Scala and F#. Still slightly better than python IMHO.

Why do you want to mix OO and FP? As a stepping stone?

CrazyJugglerDrummer