4 ms·
The author's C# example predated the release of LINQ, which makes the C# syntax much more like the Python's. The author's code: string.Join("\n", mylist.Conve
by brentb 18y ago
The author's C# example predated the release of LINQ, which makes the C# syntax much more like the Python's. The author's code:
string.Join("\n", mylist.ConvertAll<string>(
delegate(Foo foo)
{
return foo.Description();
}).FindAll(
delegate(string x)
{
return x != "";
}).ToArray());
can be rewritten as:
string.Join("\n", (from f in myList
where f.Description() != ""
select f.Description()).ToArray());
which is super-easy to read and understand. Maybe not as concise or pretty as Python or Haskell, but definitely a step in the right direction.
Others' comments that the author should consider F# are well founded, as it integrates seamlessly with other .NET-based code (such as C#) and is a pretty great language. However, it's worth noting that the post was written in 2006, well before F# was widely known about or distributed.
- d0mine 18y agoPerl: join "\n", grep { $_ } map { $_->description } @myList; Or join "\n", grep /./, map { $_->description } @myList; Ruby: myList.collect { |f| f.description }.select { |d| d != "" }.join("\n") Python: descriptions = (f.description() for f in mylist) Or if we'd like to constrain ourselves to one method `FooClass.description()` descriptions = map(FooClass.description, mylist) # `map` returns an iterator in py3k "\n".join(filter(None, descriptions)) # `None` means `lambda x: x` here Or if you don't like `filter()` "\n".join(d for d in descriptions if d) Or if `description()` is a side-effect-free method then It should be a property: "\n".join(f.description for f in mylist if f.description) At the end of the day new features doesn't matter due to most .NET-shops use old C# versions and It will not change any time soon.
- bprater 18y agoWhich seems the most "readable"? I think the Ruby one might be.
- d0mine 18y agoI like the Ruby one most. But "readable" is matter of taste (or programmer's background: I consider while (*to++ = *from++); to be a readable C code (not that I'll recommend it)). It is wrong to judge a language by tiniest code examples.
- hassy 18y agoHow about this? myList collect(description) select(!="") join("\n") http://www.iolanguage.com/scm/git/checkout/Io/docs/IoGuide.html http://www.iolanguage.com/scm/git/checkout/Io/docs/IoGuide.h...
- DaniFong 18y agoI like the python version: it is the densest, and most similar to set notation. It is done similarly in Fortress.
- lsb 18y agoUsing Symbol#to_proc, the old Ruby myList.collect { |f| f.description }.select { |d| d != "" }.join("\n") becomes myList.map(&:description).reject(&:empty?).join("\n") which is even more direct: take my list, map each element to its description, reject anything empty, and join with newlines.
- d0mine 18y agoI thought `Symbol#to_proc` is a Rails' extension.
- lsb 18y agoI learned about it in 2005, when I read it on http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.... and it's such a good idea that they've folded it into 1.9 http://www.infoq.com/news/2008/02/to_proc-currying-ruby19 http://www.infoq.com/news/2008/02/to_proc-currying-ruby19
- euccastro 18y ago# `None` means `lambda x: x` here I find it more helpful to think of it as meaning `bool` in that context. I think the None special case is only there because filter() predates bool() in Python.
- d0mine 18y agoThanks. `bool` is more readable. "\n".join(filter(bool, descriptions)) Or `len` "\n".join(filter(len, descriptions))
- felixmar 18y agoPython-like F# version: String.Join("\n", Array.of_seq (seq {for foo in mylist do if foo.Description <> "" then yield foo.Description})) (String.Join should have an overload for string IEnumerables / seqs and not just accept string arrays.)