def sum35multiple(a, b):

l = range(a, b)

c = lambda n: n%3 == 0 or n%5 == 0

s = [ x for x in l if c(x) ]

return reduce(lambda x,y:x+y, s)

note the trick we play in lines 3 and 5. line 3 creates a local
variable that holds a reference to an anonymous function and line 5 is
the place where it is invoked. combined with closures, this can be used
to implement some powerful idioms. we’ll see one example of that when
we get around to
currying
.

###
**
closures
**

a closure is a somewhat more complicated concept and it will
probably take more than one blog entry to get through. so, in this
post, we’re going to dive in and use the damn thing before trying to
understand it works.

again our target is a problem from project euler -
problem 6.

the sum of the squares of the first ten natural numbers is,

1² + 2² + … + 10² = 385

the square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)² = 55² = 3025

hence the difference between the sum of the squares of the first ten
natural numbers and the square of the sum is 3025 − 385 = 2640.

find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

this problem is also rather trivial to solve both imperatively as
well as mathematically. but for the purposes of this post, we’re going
to take a hybrid semi-computational solution. we need to get through a
little math before we get down to code.

our goal is to find:

i’ll skip the details here, but it is easy to show that:

here is straightforward translation of this equation into python code.

def d(n):

sn = n*(n+1)/2

i = range(1, n+1)

d = map(lambda x,y: (sn - x)*y, i, i)

return reduce(lambda x,y: x+y, d)

i’ve introduced a new built-in function in this code, the
```
map
```

function. the map function takes a variable number of arguments, the
first of which is always a function, and the remaining all sequences.
it iterates over each sequence “in-step” and returns a new sequence
that is generated by applying the function on each element of the input
sequences.

again, this is the kind of thing that is best explained with an example.

def print_add(x, y):

print "adding", x, y

return x+y

print map (print_add, [1, 2, 3], [4, 5, 6])

running this snippet produces the output:

```
```

adding 1 4

adding 2 5

adding 3 6

[5, 7, 9]

with that explanation, it should be clear that the function call to
map in the function d is simply to calculate each term of the first
summation in the equation for d(n).

i guess you’re wondering how is all this related to a closure. in fact, i’ve sneaked a closure into the function d(n).

look at line 4 again, i’m using the
*
local variable sn
*
inside of the anonymous function. notice how this is fundamentally
different from a function that you could create in a c-like language.
the function created by the lambda is not even completely defined until
line 2 in the function d(n) calculates sn. conceptually, we’re
*
creating a different function
*
to pass to map
*
during each invocation of
*
d(n).
one of the reasons why people claim that functional languages afford
large gains in productivity and elegance of expression is this ability
to “close state” and create functions at runtime.

there’s a bit more to this business of passing functions around that
i want to cover, but i hope i’ve whetted your appetite enough for now.
until the next post, tata, bye-bye and let me know in the comments how
you’re liking/disliking this.

Comments