Re:Why learning Haskell/Python makes you a worse programmer ...
by munificent on Monday April 22, 2013 @06:07PM
(#5592040)
It's interesting to see how far C# progressed since this was written. Just a couple of years later, C# 3.0 came out and his example becomes:
Which is, I think, pretty nice.
[ Parent
| on HN ]
Re:Why learning Haskell/Python makes you a worse programmer ...
by benhoyt on Monday April 22, 2013 @07:17PM
(#5592322)
It gets better, though. C# 3.0 also has query expressions, which are even closer to Python's list comprehensions. Also, I think String.Join(sep, enumerable) would be more efficient than repeated string concatenation with + (given that C# strings are immutable, it'd have to reallocate and copy bytes each time, just like in Python).
So we'd have:
String.Join("\n", from x in mylist where x.Description() != ""
select x.Description());
Hardly looks like a statically typed language anymore, does it?
Re:Why learning Haskell/Python makes you a worse programmer ...
by danbruc on Monday April 22, 2013 @07:06PM
(#5592281)
There is String.Join also used in the article but now there is a overload accepting IEnumerable<String>.
String.Join("\n", mylist.Select(foo => foo.Description()).Where(x => x != ""));
Using a method group yields an even more readable code. Okay, it depends - if you are not used to method groups, it looks a bit strange but I like it. String.Join("\n", mylist.Select(x => x.Description()).Where(String.IsNullOrEmpty));
Note that I slightly changed and inverted the predicate because there is no build-in String.IsNotNullOrEmpty but you could of course write it yourself. I have a small extension method in my tool library that wraps String.Join and takes care of filtering out null references and empty and white space-only strings. mylist.Select(item => item.Description()).JoinExcludingNullOrWhitespace("\n");
Re:Why learning Haskell/Python makes you a worse programmer ...
by famousactress on Monday April 22, 2013 @06:34PM
(#5592146)
Wow, that's interesting. Is 'x' just a magic var for whatever Select() yields in each iteration?
Re:Why learning Haskell/Python makes you a worse programmer ...
by adambard on Monday April 22, 2013 @06:51PM
(#5592219)
x is the variable in the anonymous function 'x => x != ""'. I don't know C# but I presume that everything before '=>' is the argument list and the rest is the function body (with implicit return).
You can see it again in '(a, b) => a + "\n" + b'.
As a side note, I find it a bit funny how C# chooses method names in Linq for the benefit of programmers who know SQL, despite `Select`, `Where`, and `Aggregate` acting exactly like what other functional languages would call `map`, `filter`, and `reduce`. Not wrong, just sort of quirky.
Re:Why learning Haskell/Python makes you a worse programmer ...
by cpeterso on Monday April 22, 2013 @07:20PM
(#5592335)
JavaScript will use the same "fat arrow" syntax for anonymous functions in ES6:
Re:Why learning Haskell/Python makes you a worse programmer ...
by adambard on Monday April 22, 2013 @07:22PM
(#5592338)
Coffeescript uses it already.
Re:Why learning Haskell/Python makes you a worse programmer ...
by vorg on Monday April 22, 2013 @07:37PM
(#5592396)
> `Select`, `Where`, and `Aggregate` acting exactly like what other functional languages would call `map`, `filter`, and `reduce`. Not wrong, just sort of quirky.
Or `collect`, `findAll`, and `inject`.
Re:Why learning Haskell/Python makes you a worse programmer ...
by adambard on Monday April 22, 2013 @09:11PM
(#5592728)
Yeah, I don't know what the precedent is for Ruby's versions. I'm glad map, filter and reduce are also present.
Re:Why learning Haskell/Python makes you a worse programmer ...
by archivator on Monday April 22, 2013 @06:39PM
(#5592170)
The argument to Where() is a Func<TSource, Boolean>, and that "x => x .. " business is C#'s lambda syntax. x is just a variable name, no magic there.
Re:Why learning Haskell/Python makes you a worse programmer ...
by daeken on Monday April 22, 2013 @06:39PM
(#5592176)
It's a lambda. Saying x => foo(x) is the same as lambda x: foo(x) from Python.