9 ms·
My "functional programming epiphany" came in a talk by Martin Odersky, who remarked that imperative programming is like thinking in terms of time, whereas funct
by rooundio 10y ago
My "functional programming epiphany" came in a talk by Martin Odersky, who remarked that imperative programming is like thinking in terms of time, whereas functional programming is like thinking in terms of space. Don't think about the sequence of how to achieve things, but the building blocks needed to do so. That nailed it and made me a Scala convert ever since.
- junke 10y agoWhy not think both in terms of space and time?
- zdkl 10y agoClojure core.async!
- anewhnaccount 10y agoLike this? http://db.cs.berkeley.edu/papers/datalog2011-dedalus.pdf http://db.cs.berkeley.edu/papers/datalog2011-dedalus.pdf
- kazagistar 10y agoI mean, thats what imperative programming ends up being to some extent. But the idea is that the less things you can think about, the better.
- throwaway7645 10y agoBest example I heard was in an F# talk. The guy used a bar tending analogy: FP => I'll have a Sazerac Imperative => Excuse me sir, could you take some ice, add rye whiskey, add bitters, add absinthe, shake, strain into a glass, and add a lemon garnish before bringing it to me
- owyn 10y agoFP is more like: drink(sazerac(garnish(strain(shake(absinthe(bitters(whiskey(ice(glass))))))))) Ultimately it's the same result? The difference is when you can reuse and compose functions.
- threeseed 10y agoExactly. FP you start with the methods and just keep composing. With OO you start with classes and objects.
- Chyzwar 10y agoIn OOP you compose nicely as well. It is also easier to understand because it is just conversation. Me(Drinker)->drink( Sazerac(Cocktail) ->garnish() ->strain() ->shake() ->absinthe() ->bitters() ->whiskey() ->ice() ->glass() )
- sooheon 10y agoIf you want to create a bar food (Finger Food object, not Cocktail), which also could use a garnish() method, which inherits from which? Or should both objects have a father, Garnishable object to inherit from? It's clear to me that object composition is less flexible than functional composition.
- kuschku 10y agoYou just have the method as an aspect that you import into the class ;)
- Chyzwar 10y agoYou can have trait/interface. But since garnish will be doing different thing it is OK to have two implementation. FP looks nice in theoretical examples in real world not so much. In FP you will end with garnishFingerFood and garnishCocktail because you need to encode somewhere a specifics of garnish action. In OOP you will have garnish methods on Coctail and FingerFood and specifics and related knowledge how you need to perform garnish will be on object itself. OOP is really powerful concept but failing in languages that have shit implementation. Java, C++ forsake OOP principles for "performance" or are made by people that do not understand concepts (Python, PHP).
- junke 10y agoThe example is just about abstraction. The imperative equivalent would be: "Give me a Sazerac!" (imperative, hehe)
- dllthomas 10y ago"It is imperative that you give me a Sazerac!"
- junke 10y agoSome time ago at University, we had to develop a puzzle solver in FP. In the report's introduction, I wrote something like "In this project, we must code imperatively in the functional programming language OCaml". The joke was well received.
- watt 10y agoWell, isn't this nice - outsourcing the knowledge what makes Sazerac, and how to make it to somebody else, and just declaring that you want it? Would you mind actually making Sazerac in your FP "analogy" as well?
- throwaway7645 10y agoHaha...didn't think so many people would respond. There is a Lisp example below. F# is similar, but with pipes and arrows.
- virmundi 10y agoI like Clojure, but stopped using it due to not having a good way to define DTOs at a service level (I prefer noisy statically typed languages apparently). Best guess. (-> {} ice (rye :2-fingers) bitters absinthe shake strain garnish) Now I think that strain flipped the returned type from drink to a glass with the drink. All this shows is that OO and FP are duals [1]. I don't claim to get FP perfectly, but my moment of zen was realizing this. 1 - http://wiki.c2.com/?ClosuresAndObjectsAreEquivalent http://wiki.c2.com/?ClosuresAndObjectsAreEquivalent
- doublerebel 10y agoThank you, I had not seen that c2 page. Many in the JavaScript community have fervent arguments for/against closures/objects (which I do not share). The educated debate in that link is a quality resource on the subject.
- sankyo 10y agoforgot the sugar!
- unsoundInput 10y agoImperative (especially Java-style OO-imperative) programming will just about always win over non-OO declarative programming in an analogy like that (i.e. a Simulation of a real world process) by nature of them being focused on step-by-step "world manipulation" (and object interaction in case of OO). The value for FP comes from proper abstraction over these processes in functional terms, at which point they can be trivially implemented (few bugs, few iteration cycles to get right). This can probably be done for every problem space, question is at which the abstraction costs outweight the gain. Considering that FP becomes more and more mainstream it's probably more viable than thought in the past, still I imagine system-driven games or complex real-world simulations, with lots of side effects, would lose more from FP than they'd gain.
- vog 10y ago> Imperative/OO [...] programming will just about always win over non-OO declarative programming in an analogy like that (i.e. a Simulation of a real world process) Some time ago I thought that, too, but I'm no longer convinced. Directly mutating values (OO style) feels more natural at first, but then you have trouble with side effects and order of execution matters more than it should, you start keeping snapshots of the whole state just to get a consistent world state during computation, otherwise this whole mess produces a whole class of bugs on its own. These problem drive you more and more into FP direction, and the FP style definitely does have its merits in this regard. I think the following article articulates this very well: "A Worst Case for Functional Programming?" http://prog21.dadgum.com/189.html http://prog21.dadgum.com/189.html
- imagist 10y agoFirst of all, that's not how you make a Sazerac. You rinse the glass with absinthe and toss the absinthe, you don't add it to the mix. There's also an ordering dependence: the rye and bitters can be mixed in either order, but adding ice, shaking, and straining should happen in that order with nothing in between, because the longer the warm ingredients are in contact with the ice, the more you're watering down the drink. I'm not going to go so far as to say the lemon garnish is wrong, but an orange peel rubbed on the rim and then garnished is better IMHO. Second, that's not functional programming, that's calling a library function.
- TheCoelacanth 10y agoI think a better analogy would be: FP => I'll have a Sazerac Imperative => Serve me a Sazerac Imperative programming isn't devoid of abstractions; it just has different ones.
- agumonkey 10y agoHickey himself based clojure on a different interpretation of time (place vs value). He talked about it a a conf long ago. Going away from falsely linear time and shared mutation is one strength. You can rely on what has been assumed much stronger. Very good for concurrency.
- jeremiep 10y agoThe epochal time model of Clojure was an enlightenment moment for me when I was learning Clojure. I now sorely miss it when I have to work in any other language :)
- JMStewy 10y agoThat talk was "The Value of Values," which I enjoyed as well. Links for anyone interested: https://www.infoq.com/presentations/Value-Values https://www.infoq.com/presentations/Value-Values (1 hour version) https://www.youtube.com/watch?v=-6BsiVyC1kM https://www.youtube.com/watch?v=-6BsiVyC1kM (1/2 hour version)
- coltonv 10y agoThe book "Learn you a Haskell for great good!", Which is in my opinion an essential read for someone wanting to get into functional programming, describes it as "imperitive is when you tell the computer a sequence of operations to get a result. FP Is when you tell the computer what thingsare. I think it should basically be required reading for any programmer, it's a very easy to follow look into the functional paradigm. It's also free to read online! http://learnyouahaskell.com http://learnyouahaskell.com
- throwaway7645 10y agoI own a copy and can't help but disagree. It goes over a few things like list comprehensions, types, etc, but I couldn't even stumble through writing a basic Haskell program when finished.I'm looking forward to Manning's Grokking Functional Programming if it ever comes out. The Haskell Book is also popular these days.
- codygman 10y agoHighly recommend http://Haskellbook.com http://Haskellbook.com
- coltonv 10y agoReally? It gave me a great grasp of the language and syntax and I was able to write some basic things immediately after, and anything I didn't know how to do (like communicate over a socket, etc.) I could google search.