36

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?

Kai
  • 9,444
  • 6
  • 46
  • 61
  • 1
    I'm not really into languages that mix like that. It ruins the point for me. I'm learning Haskell right now, and then I'll be delving deeper into Clojure. My two favorite languages! :D – Rayne Jun 18 '09 at 22:58
  • I've used Clojure, it's great for an ex-Lisper! I may do it opposite from you and learn Haskell next. – Kai Jun 19 '09 at 02:39
  • 1
    +1 I totally agree with the idea of isolating state change to dedicated parts of a program, and have it functional elsewhere (or the other way round; Kent Beck calls them "mathematical micro-worlds"). – ThomasH May 09 '11 at 10:28

13 Answers13

23

Javascript: OO and functional.

Community
  • 1
  • 1
Evan Meagher
  • 4,517
  • 21
  • 18
22

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
  • 10,145
  • 1
  • 37
  • 41
  • 8
    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 Jun 19 '09 at 16:29
12

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
  • 26,504
  • 11
  • 85
  • 105
11

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?

Community
  • 1
  • 1
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
  • 10
    Why mix OO and FP? - Because that's what our world is like. Some aspects are nicely modelled with timeless relations, others need life-cycle and state. – ThomasH May 09 '11 at 10:24
9

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
  • 31,473
  • 17
  • 86
  • 101
7

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

Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
7

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
  • 24,561
  • 8
  • 60
  • 89
  • That last part was my idea of a joke. Generally I'm not one for "purisim". Aparrently, though some people took offense. – Scott Wisniewski Jun 19 '09 at 04:05
  • I wasn't trying to offend anyone. – Scott Wisniewski Jun 19 '09 at 04:06
  • 4
    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 Jun 19 '09 at 04:48
4

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
  • 34,834
  • 8
  • 106
  • 155
  • 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 Jun 19 '09 at 02:43
  • -1 for being schoolmasterly, and not really answering the question. – ThomasH May 09 '11 at 10:35
3

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

Svante
  • 50,694
  • 11
  • 78
  • 122
  • 4
    Unless you need static typechecking. – Apocalisp Jun 19 '09 at 06:44
  • I'm on the fence about Static vs Dynamic typing. I personally love Clojure, and Haskell. – Rayne Jun 21 '09 at 18:13
  • Well, static typing and real object orientation are contradictory. You can't have everything. – Svante Jun 22 '09 at 00:34
  • Revisiting this question after almost 2 years: static typing and dynamic typing are not mutually exclusive. No one hinders you from inferring and checking types at compile time, and that is what most modern Common Lisp implementations do (also supporting declarations by the programmer). Dynamic typing just means that you also have a type sytem working at runtime. – Svante May 09 '11 at 11:57
1

Python, javascript, common lisp, ruby, smalltalk, haskell, and ocaml, off the top of my head. It's not really an exotic combination.

Marcin
  • 48,559
  • 18
  • 128
  • 201
0

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
  • 214
  • 2
  • 6
  • 14
    Because trying to program functionally in a language like Python, is like trying mow 10 acres of land with a pair of scissors. – Rayne Jun 18 '09 at 23:23
  • 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 Jun 18 '09 at 23:48
  • 3
    How is C or Objective-C functional? – Nosredna Jun 18 '09 at 23:56
  • After reading the bit about haskell and now have some idea of what is being meant by functional. – Toby Jun 19 '09 at 00:40
  • Toby, maybe you were thinking "procedural" like C or Pascal. I can see how that could get mixed up with "functional." – Nosredna Jun 19 '09 at 01:37
  • 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 Jun 19 '09 at 11:15
  • Python, Ruby and Perl have good support for functional programming techniques. They are not so good for *pure* functional programming, but then neither are classic functional programming languages like Scheme. – Marcin Aug 18 '12 at 03:55
0

in my experience, C# is very good for this purpose. in c# u can delegate Functions and even with the extension methods you can mimic OOP behavior but with FP in mind.

I usually build a class with behaviors that I needed, then build some functions to do my dirty works around it with FP practices and this works well, and as u mentioned Debug Heaven.

in my opinion, Pure FP and Pure OOP both bring some shortcomings that the others won't. OOP tends to complicate the code vs FP makes it harder for some businesses to code especially when the state is essential.

Hadi Bazmi
  • 121
  • 6
-1

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

perimosocordiae
  • 17,287
  • 14
  • 60
  • 76
  • 3
    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 Jun 18 '09 at 23:03
  • 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 Jun 18 '09 at 23:11
  • 6
    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 Jun 18 '09 at 23:21
  • 1
    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 Jun 18 '09 at 23:48
  • 4
    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 Jun 19 '09 at 09:42
  • 1
    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 Jun 19 '09 at 09:44
  • @JörgWMittag In what sense does CLOS not have methods? It is all about methods. – Marcin Aug 17 '12 at 22:48
  • 1
    @Marcin: That's simply an oversight on my part. I have never used CLOS myself. I just knew about generic functions, and completely forgot that they are implemented by methods. – Jörg W Mittag Aug 18 '12 at 01:51
  • 1
    @JörgWMittag They are the same thing, except that CLOS generic functions are "multi-methods" which may have vary by multiple types. – Marcin Aug 18 '12 at 03:54