5 ms·
Is the final code equivalent to: var getIncompleteTaskSummaries = function(membername) { return fetchData().then(function(data) { var incompleteTas
by twic 12y ago
Is the final code equivalent to:
var getIncompleteTaskSummaries = function(membername) {
return fetchData().then(function(data) {
var incompleteTasks = [];
for (i = 0; i < data.tasks.length; ++i) {
var task = data.tasks[i];
if (task.username == membername && !task.complete) {
incompleteTasks.push({id: task.id, dueDate: task.dueDate, title: task.title, priority: task.priority});
}
}
incompleteTasks.sort(function(a, b) {
return a.getTime() - b.getTime();
});
return incompleteTasks;
});
}
?
Apologies if that's wrong, i barely know JavaScript.
That code is not so great, and a bit '90s, but it's not appalling. The horrifically verbose non-currying code in the article isn't something i see any need to write. It looks like an outrageous strawman to me.
My code here could definitely improved with some functional idioms for the looping, picking, and sorting, but i wouldn't reach for currying here.
- CrossEye 12y agoYes, at first glance, that's pretty close. (You'd have to do something about `.getTime()`, as the original just did lexicographic sorting on date strings.) The article really didn't get into the fact that these blocks of code that were listed inline would ideally represent reusable functions. That these functions would best be defined separately and only included by reference would have cluttered the article, but leaving them out does make it feel a bit like a strawman, I agree. The breakdown of a task into these smaller functions is something I discussed more fully in the previous article: http://fr.umio.us/why-ramda/ http://fr.umio.us/why-ramda/
- skybrian 12y agoThis is perfectly good style, and better than the original article. Don't listen to the functional language fans. Replacing the for loop with its functional equivalents would make it less readable.
- jamii 12y agoCurrying with map/reduce/filter can be hard to read, but list comprehensions are pretty nice: (defn get-imcomplete-task-summaries [membername] (.then (fetch-data) (fn [data] (sort-by :time (for [task (:tasks data) :when (= membername (:username task)) :when (not (:complete task))] task ;; immutable, no need to copy ))))) It makes me a little sad that functional programming has come to mean map/reduce/filter for lots of people, rather than avoiding mutation which is far more interesting.
- skybrian 12y agoWell, avoiding mutation can be helpful but it's not a rule to be followed blindly either. A mutable local variable never hurt anyone, so long as it doesn't leak out of the function.
- daemonize 12y agoEven in a multiple-thread scenario?
- skybrian 12y agoIf it doesn't leak out of the function (though a closure or a pointer), how can another thread see it? It's only on one stack.
- twic 12y agoAs an aside, one of the really interesting ideas that's bubbling up from Rust is that it's not mutable state that's a problem, it's shared mutable state, and that you can make it safe by removing the sharing, rather than removing the mutability: http://smallcultfollowing.com/babysteps/blog/2014/05/13/focusing-on-ownership/ http://smallcultfollowing.com/babysteps/blog/2014/05/13/focu... I have no idea if this is true, but it's going to be really interesting to see that idea tried.
- CrossEye 12y agoJust to note, the copy wasn't meant to deal with mutability but to note a changed interface. The items that are emitted have only a subset of the properties of those ingested. We could as easily have added some new properties to them as well. Mutability wasn't the primary concern here (although in Javascript it's always a genuine concern.)
- Chris_Newton 12y agoReplacing the for loop with its functional equivalents would make it less readable. Readability is in the eye of the beholder. A tidy functional equivalent might look something like this (in a made-up syntax with typical functional idioms): getIncompleteTaskSummaries membername = fetchData >>> tasks >>> filter (task => username task == membername and not complete task) >>> map (subsetkeys ["id", "dueDate", "title", "priority"]) >>> sortwithkeyfn getTime Compare that with the explicit loops and tests above: var getIncompleteTaskSummaries = function(membername) { return fetchData().then(function(data) { var incompleteTasks = []; for (i = 0; i < data.tasks.length; ++i) { var task = data.tasks[i]; if (task.username == membername && !task.complete) { incompleteTasks.push({id: task.id, dueDate: task.dueDate, title: task.title, priority: task.priority}); } } incompleteTasks.sort(function(a, b) { return a.getTime() - b.getTime(); }); return incompleteTasks; }); } In practice, functions like filter, map, subsetkeys and sortwithkeyfn would probably be in your standard library and immediately familiar to anyone used to programming in such a language, so the only interesting details are the interesting details: which tasks you want to pick out, how you want to summarise them, and how you want the results ordered. Depending on what else is going on, you might even extract some or all of those three details into named one-liner functions to make the main algorithm more self-documenting and/or to allow those concepts to be reused elsewhere. The functional version as shown reduces 10 substantial lines with a lot of temporary variables and deep nesting to 6 total lines that each have a clear, self-contained purpose and that collectively systematically transform your input to your output. To my eyes, that style is far more readable and maintainable than manually reinventing fundamental algorithms like filter and map and camouflaging the interesting details within three or four levels of context. Edited to add: Often the thing that makes functional style awkward is when a language that wasn’t designed with that style in mind adopts some of the useful concepts but without the elegant syntax and idioms to match. For example, we have somewhat unwieldy notation for defining quick local functions in JavaScript, lambdas that work as long as you can do everything on one line in Python, and the horrific lambda expression notation in modern C++. But I don’t think it’s helpful to conflate this with functional programming style itself, any more than it’s helpful to conflate Java’s verbosity with static type systems.