15 ms·
Where FP meets OO
- mlawlerau 12y agoInteresting article Chris.
- thesz 12y agohttps://parleys.com/play/51c1994ae4b0d38b54f4621b/chapter33/about https://parleys.com/play/51c1994ae4b0d38b54f4621b/chapter33/... After about 53-th minute Odersky gets lost in side effects in a very simple code. This is why there's nothing elegant about Scala.
- wernerb 12y agoThe OP points out that immutable code is the standard in Scala. You have picked an example where Odersky gets 'lost' in mutable code. If anything, this video (and your comment) makes the case for immutable and side-effect free code using Actors and FP, as apparently it can be confusing.
- mariusmg 12y ago"Scala is the only language that I know of that marries the world of FP and OO" You can do that in C#/F# too. There's nothing special about Scala from this point of view.
- wernerb 12y agoC# and F# are different languages.. Last I checked you could not mix the two easily [0] [0]: http://stackoverflow.com/questions/14280001/why-cant-we-use-f-class-within-a-c-sharp-project http://stackoverflow.com/questions/14280001/why-cant-we-use-...
- dfkf 12y agoI think the poster means that c# has many functional features and F#, while being functional language at heart has lots of OO features integrated in it. PS And the linked question only describes the limitations of Visual Studio where you can have only one language in a given project.
- ClosureSpin 12y ago> I think the poster means that c# has many functional features Yeah just like C++ or Java now have "functional features". That doesn't make them functional languages, they don't promote functional programming to the extent that Scala does. Aside from that, their type systems are much too unexpressive to allow for the style functional programmers are used to, not to mention they promote mutability.
- mercurial 12y agoOCaml was doing that before any of them.
- ufo 12y agoAlthough I find it interesting how Ocamlers only rarely reach for the OOP bits of the language. I think that this is in part due to the powerful module system that language has.
- mercurial 12y agoIndeed. Also, the object system doesn't have the reputation of being very fast. That said, it lets you use structural typing, which is extremely handy once in a while.
- ClosureSpin 12y agoNope. Scala allows for and promotes for functional programming with higher order functions all over the place, its more powerful type system, immutability, practices, standard library, and much more. Having lambdas makes you about as functional as C++ these days. Functional/non-functional is a fuzzy distinction but C# is definitely not a functional language.
- seanmcdirmid 12y agoThere be no true Scottsmen here. Scala is also great as an imperative language and an OO language. In fact, I would say Scala has one of the most powerful natively supported object systems out there (mostly because the rest of the statically typed language world can't be bothered to support mixins, grr...). If it wasn't for nerfed imperative collection classes (to encourage functional solutions) in the standard library, you could totally just ignore the fact that Scala was a functional language at all; it is not in your face about it like Haskell. It would probably be more correct to say that "Scala is the only language that does both FP and OOP very well." On marrying the paradigms....why not just use both as needed anyways? That is completely doable in C# as well, just not as well as in Scala.
- dschiptsov 12y agoIn CLOS.
- dschiptsov 12y agoWhy there are so many ignorant idiots in the world? According to Alan Kay, the essence of OO approach (or paradigm if you wish) is a synthesis of a few fundamental concepts (like a Lisp itself) where not a single one can be removed because harmony (or balance) would be destroyed, and an ugly, mediocre construction would emerge instead (Java). The concepts are: * First-class closures - isolated, share-nothing abstractions. * Nesting of closures, so they could have an internal structure. * Message passing as the only way of communication among closures. As long as you follow these three principles, you have "generics" for free, because all the operations are mere messages. These are three of four principles upon which Smalltalk has been built upon. Here is an illustration, assuming that we are implementing message passing as simple as a procedures call. (define (kons x y) (lambda (m) (cond ((eq? m 'car) x) ((eq? m 'cdr) y) (else (error "bottom!"))))) (define (kar x) (x 'car)) (define (kdr x) (x 'cdr)) Notice that using only these abstractions we could implement a unified list structure of Lisps (recall Escher's hands). This is (surprise!) the "core" of Erlang - extending of closure-based language with immutable data with explicit message-passing, to create first-class agents (servers, etc). Where is inheritance? Inheritance is just a protocol which specifies what to do with unknown message instead of signaling an error or returning a bottom like in Haskell. What is MOP could be found in Wikipedia. Smalltalk was its ancestor. What is CLOS? It is a DSL embedded in a Lisp (based on structures made out of closures) which implements some or other subset of MOP. MIT Scheme, for example, has SOS. So FP meet OP in CLOS.
- gclaramunt 12y ago> Where is inheritance? Inheritance is just a protocol which specifies what to do with unknown message instead of signaling an error or returning a bottom like in Haskell. Care to explain? Doesn't make any sense to me...
- amelius 12y agoI'm not sure that OO-ness is a desired property. Alexander Stepanov, original creator of the STL: "I find OOP technically unsound. It attempts to decompose the world in terms of interfaces that vary on a single type. To deal with the real problems you need multisorted algebras - families of interfaces that span multiple types. I find OOP philosophically unsound. It claims that everything is an object. Even if it is true it is not very interesting - saying that everything is an object is saying nothing at all. I find OOP methodologically wrong. It starts with classes. It is as if mathematicians would start with axioms. You do not start with axioms - you start with proofs. Only when you have found a bunch of related proofs, can you come up with axioms. You end with axioms. The same thing is true in programming: you have to start with interesting algorithms. Only when you understand them well, can you come up with an interface that will let them work." More discussion: http://www.quora.com/Was-object-oriented-programming-a-failure http://www.quora.com/Was-object-oriented-programming-a-failu...
- seanmcdirmid 12y agoOO is more about naming and relating things than the fundamental truthiness of math (and likewise FP). They are just as much about communicating with humans when coming up with a solution as they are about actually getting the computer to act on the solution. Algorithms don't even come into much play in many typical programming tasks, so the premise of Stepanov's observation is probably flawed (or assumes certain tasks).
- ufo 12y agoI don't agree with that. Naming things and communicating well is something you have to do in every programming paradigm and is not specific to OOP. IMO, if you want to see what OOP is really about you should focus on its unique features: inheritance, subtype polymorphism, etc.
- seanmcdirmid 12y agoYou mean, objects aren't really about objects, since every programming paradigm has them? Well yah, that is kind of the point: it is hard to not think in terms of them, that is what 100k years of evolved linguistic capabilities have given us. If you want to focus on unique features, then do that instead; Stepanov's argument was focused on thinking, not checkmarks.
- josephlord 12y agoScala is the only language that I know of that marries the world of FP and OO and thus does not need to “throw the baby out with the bathwater”. Have you had chance to try Swift yet. It could do with Tail Call Optimisation but I do think it can already be used for functional programming.
- k__ 12y agoI think LiveScript does a good job too. But it's a bit of a niche product, not a LISP and more like Python.
- ClosureSpin 12y agoIf it doesn't have tail call optimizations then it's about as far away from functional as you can get. They're essential for basic functional abstraction.
- tel 12y agoWhile I'm reasonably open-minded about using some parts of OO at higher levels of organization (or at least just genuine modules), I'm not sure Akka is the right place to land there. Functions which are just typed as `a -> ()` have lost (or hidden) all their interesting structure.
- saryant 12y agoThen it's a good thing Akka 3.0 will be typed.
- virtualwhys 12y agoCertainly Haskell, for example, is one of the few languages richly endowed with a non-existent module system, but I personally don't know of a distributed computing environment that does preserve types across machine boundaries in any language. I must be missing something, please enlighten. If type preservation in distributed computing was a trivial engineering problem it would have already been solved and widely used. As it stands Akka is enjoying fairly widespread enterprise adoption in the distributed computing market, untyped-ness notwithstanding. Interestingly, as saryant points out, Akka 3.0 apparently will be typed, although I was under the impression that the typed actor project was experimental/a long way off/not a sure thing. Guess things have changed for the better...
- saryant 12y agoI don't think the typed actors in 3.0 are related to the old typed actors feature. It's still in a state of flux but there's a very preliminary preview in Roland Kuhn's github repo.
- tel 12y agoI'm not claiming it's a solved problem; I'm claiming that the current solutions are lacking. You can take it as standing on the outside and throwing stones, but it's not malicious. I want something better.
- virtualwhys 12y ago
- hawleyal 12y agoRuby is pretty FP/OO friendly.
- Pxtl 12y agoAm I the only one who finds the goalposts gave moved for FP? it used to be lambdas/lexical closures that were the big must-have FP feature, then it was immutability. Now that we're seeing traditionally-OO languages sport a raft of immutable types and good support for lexical closures, now it's all about higher-kinded-types. For a while, the big feature was homoiconicity and tail-call-optimization until lisp fell out of favour with language geeks. Does FP just mean "has features of my favorite self-proclaimed-functional-language"?
- tel 12y agoFP isn't a technical definition—it's a social phenomenon. As soon as you see it that way you'll never be confused by the use of the term again.
- tracker1 12y agoI've just tried not to be dogmatic about it... You can use a functional style without relying on immutable types or tail call optimization. I think it comes down more to a personal style... we tend to conflate programming to engineering, which it rarely is. More often it is a craft discipline. It's more style, taste and personal preference. That said, when you have to grow something past a few million users a day, or start to hit C10K/C10M barriers. Even then, there are ways to do that... to break up the load.
- Denommus 12y agoFunctional Programming is just composing a program with pure functions, nothing else. Immutability and closures are required for that, because mutability may break homoiconicity (which is required for purity), and lack of closures means you can't pass state around. Higher-kinded types are very useful for helping with lots of functional abstractions, but they are a type system feature and have nothing to do with FP. If someone claims that, this person doesn't understand FP or type systems. OCaml and Standard ML are two examples of languages without higher kinded types but are just as good as Haskell for FP. (Although they have a powerful module system to compensate for that).
- Denommus 12y agoOCaml also has both, and its object system is probably more powerful.
- ClosureSpin 12y agoHow come?
- Denommus 12y agoBesides the object system, OCaml has a powerful module system, with first-class modules and functors (which is already basically very equivalent to what you'd expect from objects). Besides that, objects have structural typing, and you can even create objects without classes (somewhat similarish to anonymous classes, except instead of interfaces you have the structural typing to give you something like statically typed duck typing). OCaml also has multiple inheritance and friend classes.
- otas 12y agoAlan Kay was inspired by the real world, mostly biology back when psychotropic substances were rather popular. That's were the problems start. The real world systems are: 1) unpredictable - except for those who can forecast the future. 2) Unreliable 3) Slow 4) Uncoordinated - except if you believe someone runs the world. 5) Unmaintainable - just ask someone on human maintenance (drug research etc). 6) Naturally autonomous Point 6 is very critical, because in von-neumann architectures you will end up having a shared memory and trying to give an autonomous memory to each agent, will end up very suboptimal, and no zipper, COW of any other FP trick can change that. You just cannot afford to write your software like the real world, and you don't even have intelligent agents of human ability as building blocks...
- seanmcdirmid 12y agoFor point 6, that wasn't Kay but the simula crowd who invented OOP before Kay. FP is like math: it isn't based on biology or natural human thought, but rather the truth of math. OOP is much more fuzzy, you can say things that you can't nescessarily prove are true via biased naming and relating (just like you could say and think as a person). Math is great and useful, but there is a reason we haven't evolved into straight up Vulcans yet.
- deleted 12y ago[deleted]
- otas 12y agoWell seems that simula OOP was a bit more like java/c++. It was Kay who had the message passing idea, and the smalltalk people still don't consider java/c++/simula-derivatives as true OOP languages because they miss that. http://c2.com/cgi/wiki?AlanKaysDefinitionOfObjectOriented http://c2.com/cgi/wiki?AlanKaysDefinitionOfObjectOriented http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay...
- seanmcdirmid 12y agoI think you have it backwards: OOP in Simula was more bizarre since all objects basically ran as co-routines. It isn't until Smalltalk where we get the non-autonomous objects that we are familiar with today.