Checking Signature of Function Parameter

Ian Kelly ian.g.kelly at gmail.com
Mon Aug 29 19:42:51 CEST 2011


On Mon, Aug 29, 2011 at 10:45 AM, Travis Parks <jehugaleahsa at gmail.com> wrote:
> I wanted to allow for calls like this:
>
> extend(range(0, 1000)).map(lambda x: x * x).where(lambda x: x % 2 ==
> 0).first(lambda x: x % 7 == 0)
>
> It allows me to compose method calls similarly to LINQ in C#. I think
> this looks better than:
>
> first(where(map(range(0, 1000), lambda x: x * x, lambda x: x % 2 == 0,
> lambda x : x % 7 == 0)))

FWIW, I would be inclined to write that in Python like this:

def first(iterable):
    try:
        return next(iter(iterable))
    except StopIteration:
        raise ValueError("iterable was empty")

squares = (x * x for x in range(0, 1000))
first(x for x in squares if x % 14 == 0)

It does a bit too much to comfortably be a one-liner no matter which
way you write it, so I split it into two.

Cheers,
Ian



More information about the Python-list mailing list