Hacker News new | ask | show | jobs
by Zak 5885 days ago
The Linq version is clearly superior to the imperative version, but I find the following even easier to understand:

  (into [] (map full-name
                (sort-by :last-name (filter #(= (:role %) 'developer) employees))))
It's good to see mainstream languages adding declarative constructs though.
3 comments

but I find the following even easier to understand

Entirely subjective. To me, "#(= (:" is pure line noise.

It would look that way if you don't know Clojure's quick and dirty function literal syntax. Another way to write it is:

  (fn [x] (= (:role x) 'developer))
I don't know Clojure. "(= (:" is equally line noise.
This is an absurd discussion. If you don't know C#, "x =>" is line noise. (More fairly, since (= (x y) z) is trivial to understand if you've ever seen any kind of Lisp, I should say instead -- if you don't know any C-like languages, "List<int> { 1, 2, 3 }; is line noise.)

I suggest you learn the language you program in; problem solved.

The point is that "easier to understand" is, in these cases, subjective enough, and entirely dependant on prior experience enough, to be absolutely meaningless.
I was referring to the semantics being easier to understand. Whether Lisp syntax is easier or harder to understand than C syntax is just not a place we should go.

All programming languages look more or less like words interspersed with line noise if you're not familiar with the syntax.

I personally like in simple scenarios using List<T>.ConvertAll:

    var foo = new List<int>{ 1, 2, 3};
    var bar = foo.ConvertAll(x => x * 2);
over the the Linq equivalent:

    var foo = new List<int>{ 1, 2, 3};
    var bar = from x in foo select x * 2
that's the same as

var foo= new List<int> {1, 2, 3};

var bar = foo.Select(x => x*2);

Yep, one thing to realize is that LINQ does not simply refer to the query syntax - it refers to a set of technologies, including expression trees and the ability to compile them to alternate languages like TSQL, the query syntax, and the extension methods like Select.

One small difference with your example though is that Select requires "using System.Linq", and ConvertAll is defined on List<T>.

And of course, the LINQ version returns an IEnumerable<TResult>, while the List version return List<TResult>. You could chain a .ToList() there to make them equivalent (if needed).
Clojure is compact and expressive, but there is a learning curve. Having data-centered and function-oriented programming features available for the mainstream is a great step forward. (Think mainstream programmers, not just mainstream languages.)