10 ms·
Don’t Be Scared Of Functional Programming
- slapresta 12y agoSmashing Magazine on functional programming, getting most of it wrong. This is the very moment functional programming jumps the shark. It's too mainstream for me now, I'll have to switch to reactive programming.
- platz 12y agoToo late, Erik Meijer already declared the term reactive was washed out.
- boothead 12y agoThere's still dependant types!
- pacala 12y agoReactive programming is functional programming for events. You can run but you can't hide.
- lightblade 12y ago"Functional" reactive programming is the functional programming for events. Reactive programming is actually pretty broad.
- jonsterling 12y agoNo, Functional Reactive Programming is not about events.
- seanmcdirmid 12y agoWell, some signals can be event streams if they aren't continuous behaviors. But event streaming systems like Rx provide none of the determinacy guarantees of FRP.
- slapresta 12y agoIsn't it? It's about event streams, right?
- jonsterling 12y agoNah, FRP is fundamentally about continuous time.
- seanmcdirmid 12y agoImperative reactive programming via Esterel has been a thing since the 80s, you know.
- doctorKrieger 12y agoone of the points of the article is wrong, fp isn't stateless at all it just tends to represent state in a different way - look at monads in haskell.
- wyager 12y agoWell Haskell can still "stateless" in some sense, even if your program seems to take advantage of state. The State monad (and other monads), for example, just facilitate a bit of clever syntax to sneak computational context, which can include state, into the lambda calculus. The IO and STM monads (which deal with side effects) are a bit different, and can introduce true state into a program (with a bit of cleverness to make the existence of state mesh well with the lambda calculus).
- innguest 12y agoNo, it is indeed stateless. The fact that it represents state is proof of that. FP uses representations for things instead of the real things. For instance, OO purposefully causes changes in RAM to make things happen, where in FP changes in RAM are incidental and the programmer need not manage them. So it is stateless because you program indirectly by using these conceptual representations that are free of implementation.
- dllthomas 12y agoModulo IORef and similar.
- innguest 12y agoNo, even IORef is stateless. You will know if there's state in a language if you can build a function that returns different results given the same values over time; IORef does not allow you to do that.
- dllthomas 12y agoThe bit that I was intending to modify was your statement that "in FP changes in RAM are incidental and the programmer need not manage them". With IORef, changes in RAM are not incidental, and the programmer is explicitly managing them. If IORef isn't direct enough for you, substitute Ptr. The fact that it happens in the runtime system does not necessarily mean that it is incidental or that it does not need to be managed by the programmer. As to your broader claim here, I while I understand what you're getting at (and probably wouldn't have bothered responding, but for the above) I think it's most precise to say that IORef is an encoding of state. This does not have to mean that state gets updated by means of side effects.
- droithomme 12y ago"Don't be scared" rhetoric is generally used to promote arguments not primarily through logic, but by painting the audience as fearful luddites, scared of progress and the inevitable better future based on whatever the speaker is advocating. The goal is to encourage the listener to convert to the new religion or paradigm, or be considered backwards, behind the times, and ignorant or stubborn. It's an effective tactic because any push-back is pre-framed as evidence the listener is among the ignorant backwards people.
- notacoward 12y agoHear, hear. I had exactly the same reaction. Maybe the author is pitching the article toward novice programmers and is genuinely trying to reassure. OTOH, such phrasing is far more often used as a pure rhetorical ploy. Personally, since I work in storage which is where we have to store all the "state" that the FP folks have defined into Somebody Else's Problem, I have little tolerance for that particular flavor of the month.
- slapresta 12y agoYou seem to be confusing state with data. Storage is supposed to store data, not state. Still, even if you store your internal state, that's hardly what functional programming means when opposing state.
- notacoward 12y agoThat kind of response starts to sound like "no true Scotsman" pretty darn quick. How do you distinguish between state that is on the stack (which FP still has), state that is on the heap, or state that is on external storage? Are those the same distinctions/evasions that I'd get from the next three FP advocates I asked? I keep hearing about how FP saves us from all that evil mutable state, but every time I look at a program written in a functional language I see plenty of mutable state. Half of it is entangled with control flow, which I do not see as an unalloyed win. When that doesn't suffice, those same programs often resort to externalizing their state (e.g. into a database), often incurring a significant and unnecessary performance penalty just so they can give it a name and treat it as something outside of their program. Aristocrats never like to get their hands dirty, and that's exactly how most FP advocacy comes across. "Let them eat monads." Can't wait for the guillotine.
- grayrest 12y agoIf you're interested in FP in javascript, you should check out the relatively new Ramda.js [1]. It switches from the underscore style collection first call pattern to a collection last pattern and automatically curries its functions. The combination allows for easier function composition. [1] https://github.com/CrossEye/ramda https://github.com/CrossEye/ramda More info on the topic: http://fr.umio.us/why-ramda/ http://fr.umio.us/why-ramda/ http://www.youtube.com/watch?v=m3svKOdZijA http://www.youtube.com/watch?v=m3svKOdZijA https://speakerdeck.com/raganwald/javascript-combinators https://speakerdeck.com/raganwald/javascript-combinators
- vqc 12y agoI believe Javascript Allonge takes the same approach w/r/t the call pattern: http://allong.es/ http://allong.es/ The book is also very good: https://leanpub.com/javascript-allonge https://leanpub.com/javascript-allonge
- yxhuvud 12y agoTo be honest, this looks more like an explanation of the concept of abstraction than of the concept of functional programming.
- chris_mahan 12y agoI'm not scared. It just does not make sense to me. Just because I speak French and English fluently does not mean I can speak Japanese. My wife is Japanese, and speaks it fluently. We've been together 19 years. I've tried to learn. it just does not make sense. It would be nice, because I have relatives there, and I could go live there, legally, but it just doesn't make sense in my brain. What can I do? Functional programming is the same. Every couple of years I take a look, and I recoil in confusion. It also doesn't help that people make fun of me for seeming dumb.
- jeffasinger 12y agoWhat worked for me was taking functional programming concepts, and applying them in languages where I was already comfortable. That way, I wasn't learning a new syntax and a new programming paradigm at the same time.
- sanderjd 12y agoWhich concepts did you find the most applicable? For me it is applying functions to collections in different ways, eg. map, fold, etc.
- innguest 12y agoCan you say a few things about the parts that don't make sense to you? That would help me identify what beginners struggle with, as I'm developing tutorials for that reason.
- chris_mahan 12y agoIt's simple: the very concept of the program. I want to write a program that reads one text file, and outputs another. Let's say the first file contains a list of emails, one email per line. The second file should contain the list of email domains in the first file, one line per email domain. Procedurally, I would simply declare a list, read the file of emails, line by line, split each one on the @ symbol, and if the part on the right was not already on the list, I would add it to the list. At the end, I would write the list to a new file, one line per item in the list. How would one do that using functional programming?
- cessor 12y agoI am not so much scared by functional programming as I am annoyed by functional programmers. I feel that they often come over like mathematics professors that just fill the chalkboard with lightning speed and look at you like "seriously, how dare you ask this dumb question". They go ahead explaining to you how your language is crap for all its sideeffects, smugly explaining how their language doesn't have those and then explain to you how Simon Peyton Jones shoehorned them into haskell with a monad. Also you always appear to fail to understand monads and they always fail to explain in an understandable way (although they're not that hard to get). Functional programming is great and feels right, I am amazed how knowledge of fp affects my js, C# and Python up to this day. But from a social perspective: Loose the smugness, and then we'll talk fp.
- cbp 12y agoSo you're unwilling to learn something potentially better because you're were offended by a (surely) tiny portion of its userbase? Lose the childish attitude. You owe it to your craft and users to keep learning and using better tools.
- innguest 12y agoThe reason you might be perceiving this is because once you're past the learning curve hump, the concepts become extremely obvious and the definitions stick to your head. It takes me a few seconds to rewrite foldl from rote understanding: foldl f z (x:xs) = foldl f (f x) xs, because it takes a function, a 'zero', a list of head and tail, and calls itself again with the same function, a zero 'going up' (so accumulating) and a list 'going down' (being exhausted). So as you see this is an almost a visual way to understand it, which is natural to those that already are familiar with these abstractions, but is meaningless to those who don't. If someone then says "how dare you ask that" then they have the wrong approach to things. Socrates asked every question and thought they were all valid. By asking the basics you learn the abstraction as if you're proving it to yourself, and that's more powerful. So it boils down to familiarity, is what I'm trying to say. I usually come across as more passionate than arrogant about it. If you stick to it, it will make sense. It took me 4 years of Haskell exploration for it all to click really hard and now I'm trying to shorten other people's paths. In my opinion the gap is around mathematical abstractions, how they're created, what do they mean, how they actually relate to reality, and what work do they accomplish for us that we then don't need to do anymore.
- badman_ting 12y ago"Functional programming is the mustachioed hipster of programming paradigms." Well, that's not helping. But I agree that devs should learn FP, and I now write in a much more functional style than I used to. I resisted for a long time, and that was a mistake.
- peaton 12y ago> The literature relies on somewhat foreboding statements like “functions as first-class objects,” and “eliminating side effects. I personally find this view a little overbearing. In "lay programmer's" terms, functions as first-class objects can often come down to being able to pass functions to other functions or function composition. Most of us learned about function composition in Algebra 2... So that's pretty straightforward even at its worst. Eliminating side effects is equally straightforward in that all it means is that any variables outside the scope of a given function are not changed by the function.
- mneary 12y agoFunction composition is very different from passing one function into another and the latter is definitely a concept that most Algebra 2 students would not be able to grok.
- peaton 12y agoI knew I was making a strong assertion when I mentioned function composition. I'm grateful for your feedback, but perhaps you could elaborate? I don't see why they have to be "very different". I think you called me on a certain case, but an example like this demonstrates that they are not so "very different": def a(b): m = 0 return b(m) Or even: def a(b): m = b(1) return m+7 I don't see how these are not a form of function composition. I'm definitely no expert. But I'd really appreciate an elaboration so I can learn from any mistake I'm making.
- mneary 12y agoFunction composition is a single, special case of a function accepting functional arguments. You could define the compose operator as below: compose : (a -> b) -> (b -> c) -> (a -> c) a `compose` b = \x -> b(a(x)) However, there is a whole spectrum of additionally possible functions which can be built to accept functions as arguments. Here's a couple of example: partial3 : (Integer -> Integer -> Integer) -> (Integer -> Integer) partial3 f = f 3 map : [a] -> (a -> b) -> [b] map [] f = [] map (x:xs) f = (f x):(map xs f) The first one takes a function and applies an argument, 3; the next one takes a list and a function and maps the list using the provided function. The idea of functions as arguments is a very powerful one, and from my understanding one with which people sometimes struggle.
- innguest 12y agoIf you're interested in learning FP but struggling and would like to beta test didactic material for beginners, I urge you to get in touch with me. I'm making a series of videos explaining the concepts and abstractions of functional programming in a socratic way and from scratch (so not assuming even that numbers exist). I'm 'uploaded' at google's email service.
- valarauca1 12y agoMaybe its me being thick, but I can't honestly wrap my head around what the fundamental good thing in functional programming is. When ever I work in a functional language I just find myself missing objects. OOP just feel more eloquent to look at personally. Struct.function(arg1, arg2, arg3); feels more atheistically pleasing to my mental model of programming then function(struct, arg1, arg2, arg3); This means more complex function definitions. While I can define multiple .close(); functions, that only operate in relation to their connection type I.E.: tcpConnection, serialConnection can each call their own close(). In FP I have to actually state either: fn close<T>(&:<T>){ if(<T>::isType(tcpConnection)){ tcpConnectionClose(<T>); }else if(<T>::isType(serialConnection)){ serialConnectionClose(<T>); }else{ //throw exception } } Or I just make primitive calls to each connection type tcpConnectionClose(*); This just doesn't stroke me the right way. It feels like bulky, over necessary coding when you take into account objects exist. I'm interested in FP, I feel it has to offer something for people to jump on it, but I just don't get it.
- deadghost 12y agofwiw, clojure supports polymorphism. http://clojure.org/multimethods http://clojure.org/multimethods I encountered this recently but I'll try to give a (bad) explanation. You'll call `function(thing, arg1, arg2, arg3);`. Another function will run on the arguments and return a dispatch value. For example, it will check what `thing` is and return `struct`. The `struct` version of that function is then run on the args and gives you your value. In this way you can define several `close` functions based on dispatch value instead of one monolith nested if/else `close`. I'm on the opposite side of the fence, OOP has never ever appealed to me. "Why would anyone want to use this crazy mess?" kind of thing. I'm sure I can learn to appreciate it with time but it's not a native paradigm to my mind. I only have experience with lisps when it comes to functional programming but the reason I enjoy it is that it's simple. You have functions and you have data. Functions transform data to more data and the two aren't tightly bound. If you pass a function a value it will always return the same result if you pass it the same value. THE clojure video: http://www.infoq.com/presentations/Simple-Made-Easy http://www.infoq.com/presentations/Simple-Made-Easy (I don't think he even says the word clojure in the entire hour presentation.)