This link has been bookmarked by 53 people . It was first bookmarked on 18 Feb 2008, by reckoner reckoner.
-
02 Feb 14
-
29 Jun 12
-
list comprehensions aren’t useful if you’re working with iterators that return an infinite stream or a very large amount of data. Generator expressions are preferable in these situations.
-
Generator expressions are surrounded by parentheses (“()”) and list comprehensions are surrounded by square brackets (“[]”). Generator expressions have the form:
-
Again, for a list comprehension only the outside brackets are different (square brackets instead of parentheses).
-
-
21 Apr 12
-
21 Oct 11
-
27 Jun 11
-
29 Nov 10
-
18 Mar 10
-
Two common operations on an iterator’s output are 1) performing some operation for every element, 2) selecting a subset of elements that meet some condition. For example, given a list of strings, you might want to strip off trailing whitespace from each line or extract all the strings containing a given substring.
List comprehensions and generator expressions (short form: “listcomps” and “genexps”) are a concise notation for such operations, borrowed from the functional programming language Haskell (http://www.haskell.org).
-
but the parentheses signalling a function call also count
-
Any function containing a yield keyword is a generator function
-
Inside a generator function, the return statement can only be used without a value, and signals the end of the procession of values
-
I recommend that you always put parentheses around a yield expression when you’re doing something with the returned value, as in the above example. The parentheses aren’t always necessary, but it’s easier to always add them instead of having to remember when they’re needed.
-
-
Which alternative is preferable? That’s a style question; my usual course is to avoid using lambda.
-
-
18 Sep 09
-
06 Jun 09
-
23 Dec 08
-
26 Nov 08
-
24 Nov 08
-
17 Nov 08
-
05 Aug 08
-
30 Jul 08
-
26 Mar 08
-
23 Feb 08
-
18 Feb 08
-
13 Feb 08
-
10 Feb 08
-
09 Feb 08
-
07 Feb 08
-
05 Feb 08
Sheryl A. McCoybasic information and education about functional programming
Mathematics article code DIY education reference computer professional_development programming
-
03 Feb 08
-
02 Feb 08
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.