[OT] C# -- sharp or carp? was Re: Learning Python (or Haskell) makes you a worse programmer

Peter Otten __peter__ at web.de
Tue Mar 29 12:05:47 EDT 2016


Sven R. Kunze wrote:

> On 29.03.2016 12:18, Sven R. Kunze wrote:
>> On 29.03.2016 11:39, Peter Otten wrote:
>>> My question to those who know a bit of C#: what is the state-of-the-art
>>> equivalent to
>>>
>>> "\n".join(foo.description() for foo in mylist
>>>                           if foo.description() != "")
>>>
>>
>> Using LINQ, I suppose:
>> https://en.wikipedia.org/wiki/Language_Integrated_Query
> 
> Friend of mine told me something like this:
> 
> String.Join("\n", mylist.Where(foo =>
> !String.IsNullOrEmpty(foo.description)).Select(foo => foo.description))
> 
> [untested, but from what I know of quite correct]

Reformatting it a bit

String.Join(
    "\n", 
    mylist.Where(
        foo => !String.IsNullOrEmpty(foo.description)
    ).Select(
        foo => foo.description))

this looks like a variant of Python's

str.join(
   "\n",
   map(lambda foo: foo.description,
   filter(lambda foo: foo.description, mylist)))

Assuming it's type-safe and can perhaps reshuffle the where and select part 
into something optimised there is definitely progress.

But still, Python's generator expressions are cool..




More information about the Python-list mailing list