Mixing object-oriented and functional programming

前端 未结 12 1729
渐次进展
渐次进展 2020-12-24 01:33

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 fun

相关标签:
12条回答
  • 2020-12-24 02:07

    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?

    0 讨论(0)
  • 2020-12-24 02:07

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

    0 讨论(0)
  • 2020-12-24 02:13

    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.

    0 讨论(0)
  • 2020-12-24 02:14
    • OCaml
    • O'Haskell (and object oriented programming is somewhat possible even in normal Haskell)
    • Nemerle
    • Possibly newLISP

    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.

    0 讨论(0)
  • 2020-12-24 02:15

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

    0 讨论(0)
  • 2020-12-24 02:15

    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.

    0 讨论(0)
提交回复
热议问题