<jupyter_start><jupyter_text>Imperitive vs Functional Lets compare how we could do things in an imperitive way and in a functional way. Lets look at adding two lists together.<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Imperitive<jupyter_code>x1 = list(range(0,10))
x2 = list(range(10,20))

def sumLists(x,y):
    for i in range(0,len(x1)):
        x[i] = x[i]+y[i]
    return x


print(x1)
result = sumLists(x1,x2)
print(result)
print(x1)<jupyter_output>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]<jupyter_text>Lets look at What happens here. First we make the two lists, then we pass them to a function to sum them. and we print the result whigh is a list that is the sum of the two lists we put in. However x1 changes from when we made it and when we get it back. The function used a for loop and updated the value in x. It then returned the updated x. This is equivalent to writting x +=y for each element of the list. It does the job, but if you were to go on to calculate using x1 you would be getting the wrong results and would be confused why the next time you run sumlist(x1,x2) you would get different results. See Bellow:<jupyter_code>print(x1)
result2 = sumLists(x1,x2)
print(result2)<jupyter_output>[30, 34, 38, 42, 46, 50, 54, 58, 62, 66]
[40, 45, 50, 55, 60, 65, 70, 75, 80, 85]<jupyter_text>Functional Style now lets try a functional style. The key here is we arent using loops and we dont update the value of variables.<jupyter_code>x1 = list(range(0,10))
x2 = list(range(10,20))

def sumPairs(pair):
    return pair[0]+pair[1]


pairs = zip(x1,x2)
result = map(sumPairs,pairs)

print(x1)
print(list(result))
print(x1)<jupyter_output>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<jupyter_text>Lets break it down line by line. First we make the li. Then we define a function **sumPairs** that takes in a list or tuple, and sums the first and second elements and returns the results. So we need to take two lists and turn them into a list of pairs of numbers. For that we use the **zip** function<jupyter_code>list(zip(x1,x2))<jupyter_output><empty_output><jupyter_text>Next we need to call the sumPairs function on every element of the list of pairs. for that we use the **map** function, which applies the function onto each element of the list.<jupyter_code>pairs = zip(x1,x2)
print(list(map(sumPairs,pairs)))<jupyter_output>[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
{'fraction_non_alphanumeric': 0.11054287962234462, 'fraction_numerical': 0.07592446892210858, 'mean_word_length': 4.68903803131991, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '517744', 'license': 'MIT', 'n_tokens_mistral': 1076, 'n_tokens_neox': 914, 'n_words': 392, 'path': 'JupyterNotebooks/2019/Example - Imperative vs Functional.ipynb', 'repo_name': 'JeffreyILipton/ME480', 'size': '2542'}