On Aug 29, 1:42 pm, Ian Kelly <ian.g.ke...@gmail.com> wrote: > On Mon, Aug 29, 2011 at 10:45 AM, Travis Parks <jehugalea...@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)
Python's comprehensions make the need for many of the methods I am writing unnecessary. Which probably explains why no ones really bothered to write one before. The only problem I have above is either the composition causes complex method calls first(where(map(range(..., it requires complex comprehensions or it requires breaking the code into steps. Even my approach has problems, such as the overhead of carrying an invisible wrapper around. > > 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 > > Yeah. I have already seen a lot of better ways of writing my code based solely on your example. I didn't know about iter as a built-in function. I have been calling __iter__ directly. I also need to think more about whether methods like "where" and "map" are going to be beneficial. The good thing is that someone will be able to use my wrapper in any context where an Iterable can be used. It will allow someone to switch between styles on the fly. I'm still not convinced that this library is going to be very "pythony". I wrote a post a few days ago about how I know the syntax and libraries fairly well, but I don't have the "philosophy". I haven't seen a lot of tricks and I am never sure what is the "norm" in Python. I am sure if an experienced Python programmer looked at my code, they'd immediately know I was missing a few things. -- http://mail.python.org/mailman/listinfo/python-list