[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Nick Coghlan
Nick Coghlan added the comment: Regarding the key parameter name, I believe this is closer to itertools.groupby (which uses "key=" as an optional argument, akin to min, max and sorted) than it is to filterfalse, dropwhile or takewhile (which use "pred" as the first positional argument) The on

[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Martin v . Löwis
Martin v. Löwis added the comment: Nick: that the code is difficult to decipher is really the fault of functional programming, which is inherently difficult to decipher (since last function applied is written first). Explicit iteration is easier to read. I would write Hynek's example as for r

[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Hynek Schlawack
Hynek Schlawack added the comment: Ah ok sorry. Anyhow, it’s just a very common idiom that should be easy and readable. As said, I’m not married to any names at all and would happily add a compatibility package to PyPI with the new names/parameters. -- ___

[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Martin v . Löwis
Martin v. Löwis added the comment: I also think it should go to itertools. I also found the name "first" confusing, in particular since it means what Serhiy assumes in LISP, which might be familiar to people interested in functional list processing. Also, Ruby and Smalltalk use "first" in that

[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Nick Coghlan
Nick Coghlan added the comment: Serhiy, Hynek covered the issue with the status quo in the original proposal.The existing alternative are painful to try and decipher by comparison with the named function: filterednext([0, None, False, [], (), 42]) vs next(filter(None, [0, None, False, []

[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Hynek Schlawack
Hynek Schlawack added the comment: `filter()` exhausts the full iterator which is potentially very expensive – like in conduction with regular expressions. -- ___ Python tracker ___

[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: In this case it would be pointless too. It is just next(filter(key, iterable), default) Are you need a special function for combination of two builtins? -- ___ Python tracker

[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Nick Coghlan
Nick Coghlan added the comment: It's a direct counterpart to any() and all() - first([0, [], ()]) is None for the same reason that any([0, [], ()]) and all([0, [], ()]) are both False. If first returned the actual first item in the iterable (regardless of truth value), then it would just be "n

[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: The function name looks a little misleading. I expected first([0, None, False, [], (), 42]) returns 0. -- nosy: +serhiy.storchaka ___ Python tracker

[issue18652] Add a “first” function to the stdlib

2013-08-04 Thread Hynek Schlawack
New submission from Hynek Schlawack: Let met try to get you sold on adding the “first” function I released on PyPI roughly a year ago: https://github.com/hynek/first It’s a very nice complement to functions like `any()` or itertools. I consists effectively of 9 lines of code but it proved ext