5 ms·
Genuine question, and this seems to be as good a place as any to ask it. I understand the concepts behind functional programming, and trying to implement them i
by crazygringo 13y ago
Genuine question, and this seems to be as good a place as any to ask it. I understand the concepts behind functional programming, and trying to implement them in JavaScript (recently read Functional JavaScript too).
But from my experience, functional programming is mainly useful for parallelization/distribution of computation, because it's essentially mathematical -- functional code tends to be harder to write, understand, and debug, but you do it when you need to be able to split up computation. Kind of like assembly is harder than C++, but you do it when you need 100% optimized computation in a section of your program.
But because there is obviously no multithreading in JavaScript, I've never found a compelling reason to use the FP paradigm in JavaScript. Can anyone enlighten me as to real-world situations where FP makes sense in JavaScript, either client- or server-side? Particularly in web-based projects, which is obviously what JS is for?
- sluukkonen 13y agoSmall steps. Just using some simple functional constructs like `map` and `filter` will do wonders for your code. You don't have to start writing everything in a purely functional manner to benefit from it.
- crazygringo 13y agoOh, for sure -- I use map/filter/reduce/etc. whenever I can when they result in cleaner/shorter code. They're great. I'm asking more about things like tail-call recursion/trampolining, function decoration -- making the "architecture" of your program more functional, rather than just a few lines here or there.
- cgag 13y agoRecursion isn't really hard once you get used to it, and often leads to shorter/cleaner code. Map/filter/reduce are themselves very clearly expressed using recursion.
- mpyne 13y agoIndeed, I've started using many functional concepts in a Perl program I maintain. Functional code isn't just good for concurrency, it also helps when trying to reason about lists and associative containers at a higher level.
- daxelrod 13y agoFor anyone interested in applying functional concepts to Perl, I highly recommend Mark Jason Dominus's Higher Order Perl[1], the full text of which is now available online for free. It starts by teaching a reader moderately familiar with Perl functional concepts, and then it deep dives into these concepts and iteratively applies them to example programs to make them better. [1]http://hop.perl.plover.com/ http://hop.perl.plover.com/
- theseoafs 13y agoThat's a poor characterization of why functional code is "useful". People write functional code in languages that have no support for multithreading all the time. (Further, functional programming isn't often used for cases where optimization is super important -- imperative code can often yield faster solutions.) Functional programming makes code simpler in a lot of situations. Statelessness is one of the big draws of the functional paradigm. If I'm debugging in a functional language and I find a variable has the wrong value, I can rule out stateful problems -- for example, I know I didn't accidentally reset the variable, and I know there's no code off in another function (or worse, another source file) that's messing with the state of the objects I'm managing. If you're debugging in a functional language, you can basically be certain that you got your algorithm wrong, which is helpful.
- lmkg 13y ago> because it's essentially mathematical -- functional code tends to be harder to write, understand, and debug Many proponents of functional programming (including myself) believe exactly the opposite--that functional programs are easier to read and write, and tend towards being more modular which makes it simpler to test and debug. This is mostly a product of experience and familiarity. My background is in mathematics, and I came to programming later. Many people come to functional program without as explicit of a math background as mine, but it's still mostly a matter of getting used to it.
- mattmanser 13y agoBut they're obviously not. We've now has 5-10 years of this new batch of FP languages and none of them have 'stuck', they've still not caught on. It's not cause we're all dummies, it's because, generally speaking, for most problems FP sucks ass. And that's the obviousness of it. If it were better, it really would have serious traction by now. Instead people are integrating the best bits of FP into OOP languages. And at that at least they've been a wild success, it's made OOP languages a lot easier to program in. And will make Java easier to program in. Poor old Java programmers. Java 8 will be like a revelation...
- dragonwriter 13y ago> But they're obviously not. They are obviously not for a substantial subset of the people who started programming with other-than-functional languages, which includes most current programmers. That's not the same as whether or not they are easier to read and write and debug and reason about in general. > It's not cause we're all dummies, it's because, generally speaking, for most problems FP sucks ass. Examples?
- mattmanser 13y agoLisp used to be the lingua franca of universities. So, please, let's cite examples... But seriously, state is inherent in most enterprise apps, which is to say, 95% of apps in existence.
- jamesbritt 13y agofunctional code tends to be harder to write, understand, and debug, I've heard just the opposite. It's likely harder to write if you've being doing procedural code for a long time but the elimination of state, the referential integrity, makes things easier to reason about and easier to debug in the long run. The ability to compose larger methods from numerous small, state-free methods s a big win as well. I've never found a compelling reason to use the FP paradigm in JavaScript. My motivation to write more FP-oriented code is to reduce the number of WTF moments.
- crazygringo 13y agoDo you (or anyone) have any links to web-centric JavaScript programs written this way? My confusion comes from the fact that I feel like most of the web-based stuff I deal with has to do with state -- the state of the DOM, the state of the database, the state of a connection, etc. All the FP I read about always uses simple algorithmic examples (like factorials, Fibonnaci, etc.), and I've never been able to find a good example of a real-life webapp, say, that is written in a functional manner, that is a good example of how a program can be easier to write/maintain/debug/etc. Pointers?
- tif 13y agoSorry, I don't have any links to share off hand. But I do have some comments: Of course we all have to deal with state, otherwise our programs are essentially useless. As a casual FP practitioner, I try to make web-based design decisions that minimize the number of places where I handle state. The majority of the functions that I write just take values and return some referentially transparent transformation of their inputs. So for me it's really more about black-boxing and forgetting (once it's thoroughly tested) about 90% of my code, thus hopefully limiting the scope of my state related wtf moments to the other 10%. There was a time in my life where I thought that this sort of design was obvious and everybody was doing it. I've since learned, through lots of painful maintenance and debugging, that this is not at all the case :/
- dxbydt 13y ago
- c0mpute 13y agoAs somebody who has written a lot of Java code before (read imperative) and now have had the opportunity to write more JS (which makes it a bit easier to write Functional) and also Scala, I think the process to "think" functionally takes time. Sure, its easier to apply it to the mathematics domain and harder to something that many of us might think in OO. I guess it comes with taking small steps (as someone already said in a comment). Also, making conscious effort to keep questioning how functional our code is helps. I saw this talk on functionatalks.org by Paul Chiusano (author of Functional programming in Scala) http://functionaltalks.org/2013/06/20/paul-chiusano-how-to-write-a-functional-program-with-io-mutation-and-other-effects/ http://functionaltalks.org/2013/06/20/paul-chiusano-how-to-w... If you see he repeatedly asks how can we make it more functional. I think that is kind of thinking we need to practice. Its hard after too much of imperative thinking I guess. One more thing I have heard is read SICP and forget OO for a bit :).