Closed microamp closed 10 years ago
Will update unit tests in order to make it compatible with Python 2.6.
Unit tests updated and build now passed with Python 2.6 as well.
Actually, compact
is not very useful. There is special filter
form in Python:
In [3]: filter(None, [1,0,2,"",3,False])
Out[3]: [1, 2, 3]
which means that compact = partial(filter, None)
.
reject
is ok, implementation is not obvious. In any case, Haskell
variant is good example of functional programming:
reject f = filter (not . f)
It will be good point to implement something like this in fn.py
to give example of functional approach.
from operator import not_
from functools import partial
compact = partial(filter, None)
def reject(f, it):
return filter(compose(not_, id if f is None else f), it)
I didn't check this code, just few ideas.
P.S. id
should be defined instead of lambda x: x
because it is really widespread case in functional programming.
I didn't catch idea in groupby
.
For countby
you can use composition of collections.Counter
with map
.
Thanks for the replies and suggestions, much appreciated.
How does this look for reject
?
from operator import not_
from fn.func import F
from fn.underscore import shortcut as _
def reject(func, iterable):
return filter(F(not_) << (_ if func is None else func), iterable)
even = _ % 2 == 0
assert [2, 4, 6, 8, 10] == list(filter(even, range(1, 11)))
assert [1, 3, 5, 7, 9] == list(reject(even, range(1, 11)))
Or, I can do
return filter(F(_ if func is None else func) >> not_, iterable)
Slightly more concise as it doesn't require extra parens.
P.S. I will open an issue prior to a commit request next time. Thanks, learning a lot here.
I like first variant, you can even try to do
def reject(func, iterable):
return filter(F(not_) << (func or _), iterable)
It doesn't check for concrete None
value, but useful enough.
Nice. I will open a new pull request for reject
tomorrow. Please close this one.
Will you add compact = partial(filter, None)
?
And update please README file with new functions (iters
block).
Okay, I will add compact
as well (unless you find it trivial). And I will update README accordingly. Thanks.
Additional functions to be added to
fn.iters
(influenced by Underscore.js):compact
: filter out falsey items from iterablereject
: the opposite of filtergroupby
: saner approach to itertools.groupbycountby
: similar to groupby, but instead of sub-iterator, it yields a count for the number of items in each groupUnit tests added. Tested with Python 2 (2.7) and Python 3 (3.3).
Thanks for reviewing in advance. Any feedback, positive or negative, would be highly appreciated.
Cheers, James