Thanks for the reply! On Apr 23, 10:55 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > > or define a > > function composition operator for functions that take a single > > argument? > > You could use this: > > def chain(*args): > """Compose functions (assoc right). > last argument (args[-1]): argument to last function > args[0] .. args[-2]: functions taking a single argument > Returns args[0](args[1](...(args[-2]))(args[-1]) > """ > args = list(args) > data = args.pop(-1) > while args: > fn = args.pop(-1) > data = fn(data) > return data > > import random > items = [random.randrange(100) for _ in range(20)] > print chain(list, reversed, sorted, items)
This is already better. Is it possible to define function composition as an operator and have something like ([EMAIL PROTECTED]@sorted)(items) or (list*reversed*sorted)(items) ? > I almost never use backslashes for line continuation. If you have an open > (,[,{ continuation is implicit. If not, usually adding a heading ( is > enough. > That is, instead of > > x = a + b + \ > c - d > > I'd use: > > x = (a + b + > c - d) Thanks! This is very useful. Szabolcs -- http://mail.python.org/mailman/listinfo/python-list