7 ms·
The Linq version is clearly superior to the imperative version, but I find the following even easier to understand: (into [] (map full-name (
by Zak 17y 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.
- WorkerBee 17y agobut I find the following even easier to understand Entirely subjective. To me, "#(= (:" is pure line noise.
- Zak 17y agoIt 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))
- WorkerBee 17y agoI don't know Clojure. "(= (:" is equally line noise.
- mquander 17y agoThis 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.
- WorkerBee 17y agoThe point is that "easier to understand" is, in these cases, subjective enough, and entirely dependant on prior experience enough, to be absolutely meaningless.
- Zak 17y agoI 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.
- rayvega 17y agoI 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
- deleted 17y ago[deleted]
- WorkerBee 17y agothat's the same as var foo= new List<int> {1, 2, 3}; var bar = foo.Select(x => x*2);
- nlawalker 17y agoYep, 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>.
- bluetech 17y agoAnd 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).
- cema 17y agoClojure 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.)