Sugar Syntax means to write code that is compact and elegant.  It makes code more like a mathematical expression that can be written in one line than a bunch of lines of messy for-loop and function declarations.  This is also called functional programming.

Here we illustrate all of that as simply and gently as possible by writing Python code to calculate the mathematical constant e.  Some programmers might find this difficult to understand, so we go slowly.  That’s the whole point of sugar syntax: compact and terse, yet sometimes complicated.

In case you forgot, e is the sum of the infinite series:

```e = 1 + (1 / (1 * 2)) + (1 / (1 * 2 * 3*) + (1 / (1 * 2* 3* 4)) + …

Which you can also write:

e = 1 + Σ n=0 to ∞ (1/n!)```

Where n! is n factorial.  5 factorial, for example, is 5*4*3*2*1.

This number e is also important for lots of reasons.  For example, it is related to compound interest.  Some banker figured out they could earn more money by calculating interest every day rather than one time per year.  That is called compounding. The smaller they make the period for calculating interest, like every minute, the more the banker makes.  At the end of the year the loan would have grown to p(1 + r/n)**(nt) where p is the loan amount, r is the interest rate, and t is the time period.  If interest is calculated continuously that is pe**rt.

You can see that you could write a lot of messy for loops and functions to calculate this.  Or you could use the Sugar Syntax features of Python to write that in small elegant statements.

We do that using the Python functions map, reduce, and lambda.

Map is a function that runs over every item in an iterable object, like a list, and returns another list. Reduce is a function that takes all the numbers in that list as a pair and does some calculation over each of these pairs, yielding one single result.  To put that in terms of mathematics, it is like summing an infinite series, which is what we do when we calculate e.

We start with a list.  Here we have 8 integers. We could have 9, 10, or ∞, each of which gets us closer to calculating the true value of e, which is an infinite series.

list = [1,2,3,4,5,6,7,8]

Now we run the calculation 1 / n! over every item in the list using the map function.

Map is a function that runs over an iterable object like a list like this:

map(function, list)

We could define a function using def.   Instead we define one inline using the lambda function, which is another Sugar Syntax. This notation takes one parameter, x, and returns the calculation 1 / x!.

m=map(lambda x : (1 / float(math.factorial(x))), list )

```Which returns:
[1.0, 0.5, 0.16666666666666666, 0.041666666666666664, 0.008333333333333333, 0.001388888888888889, 0.0001984126984126984, 2.48015873015873e-05]```

We had to use float above so that Python would not round each of these decimal numbers to the integer 0.

Then we use the reduce function to iteratively sum each of the numbers in this list as a series, meaning pairwise:

`e=reduce(lambda x,y: x+y,m)`

That takes each adjacent pair in the series x, y and then sums them x + y and returns a result.

`2.7178571428571425`

Now we can chain it altogether and put it on one line.

`e= 1 + reduce(lambda x, y: x + y, map(lambda x : float(1 / float(math.factorial(x))), list ))`

which gives:

`2.71827876984127`

To see that this is e, here is how to use Python’s e constant:

import math

math.e

`2.718281828459045`

Now, we can get a much more accurate calculation by using a list comprehension to make our list very large.  This is an example of functional programming and sugar syntax.  We use a formula to make the list.  Here we make a list of 100 integers and then run the e calculation.

list=range(1,100)

`e= 1 + reduce(lambda x, y: x + y, map(lambda x : float(1 / float(math.factorial(x))), list ))`

print (e)

`2.7182818284590455`

Everything you need to know about outsourcing technology development
Access a special Introduction Package with everything you want to know about outsourcing your technology development. How should you evaluate a partner? What components of your solution that are suitable to be handed off to a partner? These answers and more below.

Zymr blogger