News for hackers. Stuff that matters.
Re:How currying leads to elegant, readable code by skybrian on Thursday June 19, 2014 @03:30PM (#7917863)
This 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.
Re:How currying leads to elegant, readable code by jamii on Thursday June 19, 2014 @05:05PM (#7918306)
Currying 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.
Re:How currying leads to elegant, readable code by skybrian on Thursday June 19, 2014 @05:31PM (#7918420)
Well, 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.
Re:How currying leads to elegant, readable code by daemonize on Friday June 20, 2014 @12:34AM (#7919535)
Even in a multiple-thread scenario?
Re:How currying leads to elegant, readable code by twic on Friday June 20, 2014 @07:06AM (#7920232)
As 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/focu...

I have no idea if this is true, but it's going to be really interesting to see that idea tried.

Re:How currying leads to elegant, readable code by skybrian on Friday June 20, 2014 @01:54AM (#7919660)
If 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.
Re:How currying leads to elegant, readable code by CrossEye on Friday June 20, 2014 @08:43AM (#7920423)
Just 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.)
Re:How currying leads to elegant, readable code by Chris_Newton on Thursday June 19, 2014 @06:35PM (#7918652)
Replacing 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.

Re:How currying leads to elegant, readable code by skybrian on Thursday June 19, 2014 @07:05PM (#7918765)
Well, I could convert it to a SQL statement too, but then we're no longer using a general-purpose language. Filter, map, and so on are additions to the basic for loop that everyone already needs to know how to use.

"you might even extract some or all of those three details into named one-liner functions"

No. Replacing familiar operations with unfamiliar ones (adding a level of indirection) doesn't add readability or make it "more self-documenting" than pure boilerplate code that everyone's seen many times before and will see again.

But these sort of arguments never reach a conclusion. The way to test it would be to teach beginners and see which one is easier to teach. That's a bit beyond a casual internet discussion, though. :-)

Re:How currying leads to elegant, readable code by qu4z-2 on Thursday June 19, 2014 @08:02PM (#7918943)
I see this assumption quite frequently -- That what is easy for a beginner is easy for an expert. I'm not sure that's the case.

FWIW, I'm in the anti-boilerplate camp.

Re:How currying leads to elegant, readable code by skybrian on Thursday June 19, 2014 @11:09PM (#7919377)
Well, most things are easier for an expert, or they wouldn't be an expert, right? I think it's good practice to try to write code that's readable by people with a wide variety of skill levels.
Re:How currying leads to elegant, readable code by rnbwd on Saturday June 28, 2014 @03:39AM (#7957708)
I began programming in September of last year. For loops were trivially easy to understand (took me about a day), 1-2 weeks to become proficient. I started with php, moved to 'jquery', and then rails. Rails pisses me off. I said fuck this class I'm learning node/javascriot. After a week of js I switched to clojure (my instructor hadn't even heard of it, this is a full day bootcamp coding course). Rich Hickey is my single greatest influence in programming habits styles. I spent a few months, off and on, learning clojure. I still can't program competently, but I learned how to replace for loops with map / filter / reduce. Once I realized what was going on there, I had my greatest 'ah ha' moment in programming thus far. Clojure blogs led me to Om, om to react, react to frp and gulp, gulp lead to streams / piping, and I haven't used a for loop in 6 months. It's boilerplate that's so trivial, you only need to code for about 3 months for the functional abstractions to become more useful to you. It took me a few weeks to learn how map, filter, reduce work. It took me a few hours to learn a 4 loop. I still consider myself am amateur programmer, but this trend of code abstraction will continue rapidly, and our CPUs and more that capable of handling the potential performance hits.