Don't like this style? Click here to change it! blue.css

Daily Challenge

Math Task 1: Change this scatter plot to give me random points near the line y=3x + 1

Class 13: Generators and Intro to Functional Coding

We need to head back to the last set of notes to finish off "comprehensions".

Generators: Build your own "iterable"

So last time we explored many built-in iterable types in Python. Today let's build one from scratch. The yield keyword, acts like a partial return. A function which "yields" results will execute to the first yield, then wait for someone to call next on it:

Explore Task 2: Use the terminal to decide what the type of int_iter is.

Fib Yielder Task 3: Write a generator that yields the Fibonacci sequence. Print the first 10 terms.

So generators are functions which can be iterated. Just like we had "list comprehensions" we can do "generator comprehensions":

Comprehension Task 4: Write a generator, using the () notation, which yields the first 10 perfect squares. Now yield the first 10 triangular numbers.

Consumption Task 5: Now use the sum operator to add the first 10 perfect squares. Add the first 10 triangular numbers.

Filters

Any of our comprehensions can also be used as a natural "filter" on an iterable:

Capital Task 6: Build a generator which yields only the capital letters in the string "I Am Good Enough, Smart Enough, and Doggone It People Like Me!".

Map, Filter, Reduce

So far we've been making sets of commands for our programs to run. Sometimes putting those commands into "functions". This is called imperative programming. There are several other programming paradigms including declarative programming (tell the computer what it should do but not how) and Object-Oriented programming (make many objects which contain both data and actions).

Today we're going to explore a style of coding called functional programming. In functional programming we try to have a series of functions that define your program and functions don't have some lasting side-effect.

Python is not naturally a functional language, but it supports some features of functional programming. Because I want you to be exposed to this type of thinking let's explore some basic building blocks:

map: consumes a function and an iterable creates an iterable of the function called on each thing in the iterable.

Map Task 7: Map the function ord onto every character in "Andy is the greatest". Make a list out of the output. (These are the ascii values of those characters.)

filter: consumes a Boolean function and an iterable, returns an iterable containing only the elements for which the function returns True.

Prime Task 8: Write a function called is_prime which consumes an integer and returns True if the integer is prime (False otherwise).

Filter Task 9: Now use your function to filter the numbers 2 to 50 and display a tuple of the primes.

reduce: consumes a function (which consumes two elements and returns one), an iterable, and a starting value. It returns the result of having the function aggregate through the iterator.

This one is a little complicated to think out so pay close attention:

Reduction

Reduce Task 10: Write a "reducing function" to help you count the number of multiples of 3 in an iterable. Call it on range(2,70,5).

Functions which shall not be named

So in Python you can make un-named, one-off, simple functions called lambdas.

The format is: lambda x, y, ... : expression_of_x_y

These are really nice for using map, reduce, and filter.

Or:

Lambda Task 11: Use a lambda and map to double the numbers from 1 to 12.

Lambda Task 12: Use a lambda and filter to make a list of the multiples of 5 less than 131.

Daily Challenge

Use join, sorted, and split to write a function which consumes a sentence and returns the sentence where each word has been alphabetized. For instance: "Andy is the greatest" should return "Adny is eht aeegrstt".