News for hackers. Stuff that matters.
Re:Zed finishes Learn Python The Hard Way by shin_lao on Wednesday September 29, 2010 @12:11PM (#1739944)
I thought the book was to be an introduction to programming through Python, that's a good opportunity to show "by the way, you can also see things the functional way...".

I'm no Python expert, I couldn't say what's idiomatic or not. That being said, I never had the feeling of struggling with the language when using map and filter, quite the contrary.

What's the idiomatic way of doing this?

   suffix_str_lst = map(lambda x: x + "_suffix", str_lst)
Re:Zed finishes Learn Python The Hard Way by koenigdavidmj on Wednesday September 29, 2010 @12:20PM (#1739969)
Probably to use a list comprehension:

    suffix_str_lst = [x + "_suffix" for x in str_lst]
If you are okay with a generator instead of a list, then you can flip the square brackets to parentheses and then you do not need to hold the whole thing in memory at once.

EDIT: You can add a filter in a similar way:

    suffix_str_list = [x for x in str_list if x.startswith("meep")]
Re:Zed finishes Learn Python The Hard Way by RyanMcGreal on Wednesday September 29, 2010 @12:52PM (#1740081)
You can also combine maps and filters in a list comprehension:

    suffix_str_lst = ['{0}_suffix'.format(x) for x in str_list if x[:4] == 'meep']
Re:Zed finishes Learn Python The Hard Way by zedshaw on Wednesday September 29, 2010 @01:54PM (#1740348)
Read that out loud and tell me that doesn't grate on your ears? That's my basic test if code for the book would be understood by the reader. What you have here, this "symbol soup" is what makes programming hard for people in the beginning.
Re:Zed finishes Learn Python The Hard Way by angusgr on Wednesday September 29, 2010 @07:48PM (#1741639)
Some of the simpler list comprehensions (like the student one two parents up) are fairly "symbol soup" free, though - and a very clean way of expressing what you want.

OTOH, I can see how they could still be a hard concept for a beginner who hasn't done any functional programming before, particularly because they don't read front-to-back like most imperative statements.

EDIT: You can always split complex ones up as well, for most common usages:

  meeps = [ s for s in str_list if s[:4] == 'meep' ]
  suffix_str_lst = ['{0}_suffix'.format(m) for m in meeps ]
This one was probably a bit simple to really need that, but it's a bit easier to read. :).
Re:Zed finishes Learn Python The Hard Way by RyanMcGreal on Wednesday September 29, 2010 @02:19PM (#1740471)
I actually find it really elegant. I fell in love with comprehensions as soon as I discovered them. They were the terse, expressive syntax I'd long been searching for.
Re:Zed finishes Learn Python The Hard Way by bryanh on Wednesday September 29, 2010 @02:35PM (#1740550)
Is there a tutorial on learning simple one liners like this? I find myself frustrated after seeing simple and obvious solutions like the above, but can never bring myself to code them as such.
Re:Zed finishes Learn Python The Hard Way by steveklabnik on Wednesday September 29, 2010 @03:18PM (#1740717)
http://learnyouahaskell.com

I'm only half kidding. The best way to get used to using functional concepts is to actually use them in their native setting, and then apply (giggle) them elsewhere. When you _have_ to solve problems this way, at some point, it clicks.

Re:Zed finishes Learn Python The Hard Way by mcbain on Wednesday September 29, 2010 @09:52PM (#1741956)
I did something similar to learn a bit of Haskell (to knock the rust off my C geared brain) by picking through a bunch of the problems in http://www.haskell.org/haskellwiki/H-99:_Ninety-Nine_Haskell...

I can say that it has indeed improved my Python scripting as well.