9 ms·
Functional Programming using JavaScript
- debacle 12y agoI assume these slides are from a talk?
- openmaze 12y agoCorrect, but not mine. I think they are quite interesting and they are well understood outside the context of the talk
- CrossEye 12y agoNo video was recorded on the occasions I gave the talk. I'd apologize, but actually, you should probably thank me. :-)
- openmaze 12y agoThe author is here! Thank you, awesome slides and content!
- buzzdecafe 12y agoyou missed a golden opportunity to rickroll hacker news readers
- CrossEye 12y agoHasn't that meme died yet? Well, I for one won't help keep it alive by pointing to the recently discovered talk video found here:....
- swah 12y agoThere is also @fogus' book: http://shop.oreilly.com/product/0636920028857.do http://shop.oreilly.com/product/0636920028857.do I haven't read it yet.
- joshuacc 12y agoIt's a fantastic book for getting your mind used to the functional way of thinking. I'd also recommend JavaScript Allonge on the same topic. https://leanpub.com/javascript-allonge https://leanpub.com/javascript-allonge
- CrossEye 12y agoFor JS people wanting to learn FP, I'd recommend Allonge first. For FP people wanting to see how they can use JS in and FP manner, Fogus' Functional Javascript is a good book, and it's not a bad second book for the JS crowd.
- derengel 12y agoI'm curious what you think about Clojurescript? leaving aside that it may be harder for teams to move to Clojurescript than to improve their JS codebase. The reason for the question is that Clojurescript supports all the FP idioms of your slides more naturally and also has more FP features.
- CrossEye 12y agoI've just started learning Clojure, and I haven't spent any time yet on Clojurescript.
- sbochins 12y agoThis book is great, it is very upfront that javascript has significant limitations if you're interested in functional programming. But, I think it lays out a great way to write much cleaner javascript code and ways to avoid mutability. There are tons of great functions that Michael writes in the book and would be very useful if not only as a set of utility functions alongside underscore.
- atrilumen 12y agoI believe this is the author of Ramda, yes? I can't find the talk, anybody got a link? (Ramda is pretty bad ass.) http://ramda.github.io/ramdocs/docs/ http://ramda.github.io/ramdocs/docs/
- CrossEye 12y agoOne of the co-authors, yes. Mike Hurley and I have been developing the library for around 18 months. For the last six months, we've had many additional contributors, including some prolific ones, and David Chambers has recently joined the team.
- CrossEye 12y agoThere is no video for this. I gave the talk several times to small groups, but never recorded it. Sorry. But you don't want to see my ugly mug, anyway.
- belgianguy 12y agoThanks for that! It was a nice introduction, I have a colleague who usually made my eyes glaze over when he started about FP, but now that I have seen it in an environment that I understand and use in my day to day job, a lot of "a-ha" moments occurred in rapid succession. I also liked the humorous bits. It's all pretty self-explanatory and well put, a little Googling will fill in any gaps an interested pupil might face.
- roneesh 12y agoThis is a really nice presentation that introduces a lot of concepts well and also helps a person conceptually understand how functional programming fits in to the larger programming world. Book mark it and share it.
- pentlander 12y agoI agree, a lot of people tout how functional programming is easier to both read and write, but this really puts things in perspective. I especially like how he explains how the different standard functional functions work in very simple terms and immediately shows the advantages of using them.
- serve_yay 12y agoThese slides are very difficult to read on a touch device.
- CrossEye 12y agoI apologize. I really posted it on my site only as a convenience for myself, not to be published. I never really considered anything but projection at the time. Although I can view it on my fairly large phone, the experience is not great.
- killtheliterate 12y agoI put together a similar talk last summer: http://thedrearlight.com/functional-js-wut http://thedrearlight.com/functional-js-wut unfortunately, don't have the vid either, but the slides might be complementary to the ones linked by the OP. --edit It's formatted kinda weird, where both up/down and left/right arrows navigate through the different sections.
- CrossEye 12y agoVery nice talk! I've used that formatting too, and while it can be very useful when running a presentation by yourself, it's not so nice for slides shared with someone who doesn't know it.
- leeoniya 12y agofinally, a slide deck that uses `history.replaceState` instead of `history.pushState`. no more having to hit "back" 90 times to exit the deck. how the latter ever became a pattern is beyond me. [EDIT] it probably became a pattern because of browsers that didnt implement the history API, so all hashchange events were pushed onto the history stack.
- 9point6 12y agoTo be honest, using replaceState for this doesn't seem to make sense to me. A user would probably want to go backward and forward through this using browser history navigation (for instance using the navigation buttons on their mouse). If you're launching a link from a page you expected to come back to, surely opening the link in a new tab would be the way to accomplish this?
- leeoniya 12y agopersonally, i've always used arrow keys, swipes, pgup/pgdn to nav between slides. i want a single-action "exit slide deck to previous page", and the browser's back is the logical place for that to be, imho.
- jmathai 12y agoI think the lines between a single page app and full page reloads needs to be blurred. Most people won't know the difference and the back button needs to behave consistently. For that reason, I think that pushState here would make more sense. The state of the page does, after all, change.
- leeoniya 12y agoit's not a mental "context switch", though. i think the way android has the back button implemented is very sensible.
- lobster_johnson 12y agoYelp does this with its photo gallery, and it's really annoying. When the user is in a linear sequence such as a slide deck, photo gallery or similar, the "previous"/"next" buttons don't map to the browser's back/forward buttons, they navigate the sequence. Littering the history with the navigation history of this sequence does not make sense. However, it'd be nice if browsers could perhaps record the history (perhaps as some kind of sub-tree history), allowing me to see that I not only visited this slide deck, but that I also visited these specific slides.
- areski 12y agoCan we find the talk somewhere?
- lelf 12y agoFor those you want to learn the functional programming I'd suggest to immedeately commence looking at Ocaml/Haskell instead to see the real big picture. Because it's of course adorable that you can write map and compose in JS (it's 2014, it can do it everywhere), but FP = many more serious things.
- KurtMueller 12y agoEdx has a course in functional programming using Haskell. I haven't taken it yet but I'm going over Christmas.
- atrilumen 12y agoIntroduction to Functional Programming https://www.edx.org/node/2126#.VGrvnnVGh5Q https://www.edx.org/node/2126#.VGrvnnVGh5Q
- tel 12y agoStrong agreement. "Adorable" is exactly how I describe it. Even if it's already producing practical advantage in Javascript, it's mostly a cute toy given how it stacks up against "normal" usage. Learn Haskell/OCaml and you'll see how to really take this kind of reasoning many, many orders of magnitude upward. Learn Coq/Agda to see yet another meteoric jump.
- CrossEye 12y agoWhile I'm not upset that this has been posted here and has been garnering some attention, this talk wasn't designed for this kind of consumption. I have given this talk to two kinds of audiences: * In a large corporate office with heterogeneous skill-sets among developers, but where almost all had some experience with Javascript. * Javascript Users Groups In each case, the idea was to make the case that FP could offer them practical advantages. The goal was definitely not to teach them FP or to suggest the best FP tool. There are definitely better languages in which to do FP. But if for whatever reasons you're working in Javascript, I would suggest that a functional style could be useful.
- torkable 12y agoAnyone care to take a crack at this SO question? http://stackoverflow.com/questions/26811628/performance-of-nested-for-loops-vs-array-functions-in-javascript http://stackoverflow.com/questions/26811628/performance-of-n...
- sramsay 12y agoI'm a huge fan of lo-dash, but might switch to Ramda. Here's a rundown: http://bahmutov.calepin.co/lodash-to-ramda-example.html http://bahmutov.calepin.co/lodash-to-ramda-example.html
- bahmutov 12y agoTrying to ride the wave: I have written about functional programming in javascript, see blog posts http://bahmutov.calepin.co/tag/functional.html http://bahmutov.calepin.co/tag/functional.html - Ramda is great!
- CrossEye 12y agoI've read and commented on many of these posts (the latest one far too frequently! :-) ). Excellent stuff.
- seomis 12y agoI dislike these expositions on the "differences" between object-oriented and functional programming. The two concepts are not directly commensurable, and one can have objects with referentially transparent methods, to be used in a declarative style.
- rmrfrmrf 12y agoAgreed. Everything about this slideshow seemed like the author was slightly off the mark about what functional programming is and why it's useful.
- inglor 12y agoHis functional example in Bluebird and some ES6 (traceur) just becomes: ``` return fetchData().get("tasks").filter(x => x.member === memberName).filter(x => !x.complete).map(x => {x.id, x.dueDate, x.title, x.priority}).call('sort'); ```
- facorreia 12y agoBravo for promoting functional programming and showing how it can be done in JavaScript. One issue concerns me, though: using strings to reference attributes. This makes static checking tools much less useful and can lead to difficult to find errors due to refactoring and typos.
- blablabla123 12y agoPromoting FP using SQL as an example for encoding business logic doesn't look to me like a nice example...
- CrossEye 12y agoThe idea was just to show that, contrary to many people's expectations, OOP is not the only widely used paradigm in business programming.
- CrossEye 12y agoThat, I think, is working with the grain in Javascript. It's extremely difficult to do static checking in the language in any case, because in the language `foo.bar` is just syntactic sugar for `foo['bar']`. A FP approach in JS is by its very nature going to be different from one in something more strongly typed like Haskell; this is actually one of the easier differences to swallow.
- bodyloss 12y agoIntrestingly it seems that the functional version he proposed is much faster than the OO version. http://jsperf.com/oop-vs-ramda http://jsperf.com/oop-vs-ramda
- CrossEye 12y ago(Author of that talk (and co-author of Ramda) here. Your perf test only times the parsing of the function -- not exactly fair.
- derpface 12y agoHey man. Great work.
- pritambaral 12y agoFixed for actually running the tests instead of just parsing: http://jsperf.com/oop-vs-ramda/3 http://jsperf.com/oop-vs-ramda/3 TL;DR: Ramda is somewhat faster than OO one
- idlewan 12y agoFixed for running a proper imperative version instead of the functional-oriented 'imperative' version of the original author: http://jsperf.com/oop-vs-ramda/4 http://jsperf.com/oop-vs-ramda/4 TL;DR: Imperative is faster than functional. Functional is shorter (33% slower in chromium, 83% slower in firefox), but might be more readable depending on your preferences.
- pritambaral 12y agoPardon me if I read it wrong, but you just removed the promises, didn't you? Anyway, doing the same to the FP version yields almost equal speeds (Chromium): http://jsperf.com/oop-vs-fp http://jsperf.com/oop-vs-fp
- idlewan 12y ago
- torkable 12y agoIn my experience, FP in javascript leads to easy to read, stylish code that runs far slower than more traditional code. I have benchmarked the array builtins, underscore, lodash, and fastJS vs a simple set of nested loops and the for loops blow everything away. I love FP, I've been doing my side projects in Haskell for the last few months and love it. But when my Node server responds noticeably slower because I'm consuming a database query with functional patterns instead of loops, I use the loops. I still use FP where I believe it fits and provides benefits, but I'm not married to it when using JS. The point is, is I keep seeing people pushing FP in JS really hard and no one every says, "Warning, you are sacrificing performance". The performance hits don't matter much of the time, but sometimes it does and everyone pushing FP seems to push it like if you're not using FP in JS you're bad and you should feel bad and that's just not true.
- ktg 12y agoSee also fn.js (Functional JavaScript Library)| https://bitbucket.org/ktg/fn https://bitbucket.org/ktg/fn
- idlewan 12y agoThe author is misrepresenting imperative code [0]. Instead of the 42 lines of code he thinks is imperative, but really is already functional-compatible, this is what I would write: var getIncompleteTaskSummariesForMember_imperative = function(memberName) { return fetchData().then(function(data) { var tasks = data.tasks; var results = []; for (var i = 0; i < tasks.length; i++) { var task = tasks[i]; if (task.member == memberName && !task.complete) { results.push({ id: task.id, dueDate: task.dueDate, title: task.title, priority: task.priority }) } } results.sort(function(first, second) { return first.dueDate - second.dueDate; }); return results; }); } Still longer than the 10 lines of functional code, but a much more fair comparison. [0]: http://scott.sauyet.com/Javascript/Talk/2014/01/FuncProgTalk/#slide-123 http://scott.sauyet.com/Javascript/Talk/2014/01/FuncProgTalk...
- the_imp 12y agoI was bothered by the presented functional implementation; it seemed to use a whole pile of library-specific functions to do the same things base JavaScript already does, just expressed slightly differently. This would be the code I'd write for the same task, pure JS assuming that fetchData returns an ES6 Promise: var getIncompleteTaskSummariesForMember = function(memberName) { return fetchData().then(function(data) { return data.tasks .filter(function(task) { return (task.member == memberName && !task.complete); }) .map(function(task) { return { id: task.id, dueDate: task.dueDate, title: task.title, priority: task.priority }; }) .sort(function(first, second) { return first.dueDate - second.dueDate; }); }, function(reason) { console.log(reason); }); }; Now, does that count as functional? I'm not sure I really care, but it's certainly JavaScript-ish.