You can almost use Python as a functional language, but ... you run into trouble. The following code, discovered by Nithin Reddy, will raise an exception:
funs = [lambda i: i + j for j in [1,2,3,4,5]]
del j
funs[0](1)
The issue is that lambda, which executes in the context of the lambda call rather than the lambda creation. Using a proper def won't work either -- the following code fails in precisely the same way:
funs = []
for j in [1,2,3,4,5]:
def f(a): return a + j
funs.append(f)
del j
funs[0](1)
The fix is to curry your function:
def partial(f, *args, **kwargs):
def bound_f(*args2, **kwargs2):
x = kwargs.copy()
x.update(kwargs2)
return f(*(args + args2), **x)
return bound_f
import operator
funs = [partial(operator.add,j) for j in [1,2,3,4,5]]
del j
funs[0](1)
I'm not quite sure what to make of this. Is function context bound on return?
Wednesday, March 17, 2010
Wednesday, March 10, 2010
Too many options
The autocomplete module that comes with jQuery UI is not the same as the jQuery Autocomplete plugin. Totally different interface. Using the documentation for one while running the other will not make for an enjoyable afternoon.
jQueryUI comes with all manner of functionality beyond autocomplete, so that'd be my recommendation.
jQueryUI comes with all manner of functionality beyond autocomplete, so that'd be my recommendation.
Friday, March 5, 2010
Write once, read many
Nice: the PEDANT database contains the output of many common genome annotation tools against 3,000 complete genomes, notably including the human genome. I've been looking for the results of running, say, TMHMM against every human gene.
(For more, see the paper: PEDANT covers all complete RefSeq genomes.)
(For more, see the paper: PEDANT covers all complete RefSeq genomes.)
Subscribe to:
Posts (Atom)