This Is Not The Course Website You Are Looking For

This course website is from a previous semester. If you are currently in the class, please make sure you are viewing the latest course website instead of this old one.

Everyone:

Next week, we will explore a new programming paradigm called functional programming, which, if you look closely, is similar to the Unix Philosophy: By decomposing data processing problems into small functions that work together to process data without side-effects, we can expose opportunities for concurrency and parallelism to build powerful abstractions such as MapReduce.

TL;DR

The focus of this reading is to introduce you to functional programming in Python.

Readings

The readings for this week are:

  1. Introduction to Functional Programming in Python

  2. Iterators, generator expressions and generators

Optional Resources

Here are some additional resources:

  1. Functional Programming HOWTO

  2. A practical introduction to functional programming

  3. Real Python

Quiz

This week the reading is split into two sections: the first part is a short dredd quiz, while the second part involves three short Python scripts: evens_fp.py, evens_lc.py, and evens_gr.py.

To test these scripts, you will need to download the Makefile and test scripts:

$ git checkout master                 # Make sure we are in master branch
$ git pull --rebase                   # Make sure we are up-to-date with GitLab

$ git checkout -b reading07           # Create reading07 branch and check it out

$ cd reading07                        # Go into reading07 folder

# Download Reading 07 Makefile
$ curl -LO https://gitlab.com/nd-cse-20289-sp20/cse-20289-sp20-assignments/raw/master/reading07/Makefile

# Execute tests (and download them)
$ make

Questions

Record the answers to the following Reading 07 Quiz questions in your reading07 branch:

Scripts

Given the following Python script, evens.py:

#!/usr/bin/env python3

import sys

results = []
for number in sys.stdin:
    number = number.strip()
    if int(number) % 2 == 0:
        results.append(number)

print(' '.join(results))
  1. Create a second version of this script called evens_fp.py that re-implements the original evens.py script in one line using map, filter, and lambda:

    #!/usr/bin/env python3
    
    import sys
    
    print(' '.join(
        # TODO: One-line expression with map, filter, lambda
    ))
    
  2. Create a third version of this script called evens_lc.py that re-implements the original evens.py script in one line using list comprehensions:

    #!/usr/bin/env python3
    
    import sys
    
    print(' '.join(
        # TODO: One-line expression with list comprehension
    ))
    
  3. Create a fourth version of this script called evens_gr.py that re-implements the original evens.py script by creating a generator function using the yield keyword.

    #!/usr/bin/env python3
    
    import sys
    
    def evens(stream):
        # TODO: Implementation that uses yield statement
    
    print(' '.join(evens(sys.stdin)))
    

To test your scripts manually, you should be able to reproduce the following output:

$ seq 1 10 | ./evens_fp.py
2 4 6 8 10

$ seq 1 10 | ./evens_lc.py
2 4 6 8 10

$ seq 1 10 | ./evens_gr.py
2 4 6 8 10

Submission

To submit you work, follow the same process outlined in Reading 01:

#--------------------------------------------------
# BE SURE TO DO THE PREPARATION STEPS ABOVE
#--------------------------------------------------

$ cd reading07                        # Go into reading07 folder

$ $EDITOR answers.json                # Edit your answers.json file

$ ../.scripts/submit.py               # Check reading07 quiz
Submitting reading07 assignment ...
Submitting reading07 quiz ...
      Q1 1.00
   Score 1.00

$ git add answers.json                # Add answers.json to staging area
$ git commit -m "Reading 07: Quiz"    # Commit work

$ $EDITOR evens_fp.py                 # Edit your evens_fp.py file
$ $EDITOR evens_lc.py                 # Edit your evens_lc.py file
$ $EDITOR evens_gr.py                 # Edit your evens_gr.py file

$ make                                # Test all scripts
Testing evens_fp.py ...
 evens_fp.py on seq 1 10                  ... Success
 evens_fp.py on seq 10 20                 ... Success
 evens_fp.py on seq 1 1000000 (count)     ... Success
 evens_fp.py no list()                    ... Success
 evens_fp.py structure                    ... Success

   Score 1.00

Testing evens_lc.py ...
 evens_lc.py on seq 1 10                  ... Success
 evens_lc.py on seq 10 20                 ... Success
 evens_lc.py on seq 1 1000000 (count)     ... Success
 evens_lc.py no list()                    ... Success
 evens_lc.py structure                    ... Success
   Score 1.00

Testing evens_gr.py ...
 evens_gr.py on seq 1 10                  ... Success
 evens_gr.py on seq 10 20                 ... Success
 evens_gr.py on seq 1 1000000 (count)     ... Success
 evens_gr.py no list()                    ... Success
 evens_gr.py structure                    ... Success
   Score 1.00

$ git add Makefile                    # Add Makefile to staging area
$ git add evens_fp.py                 # Add evens_fp.py to staging area
$ git add evens_lc.py                 # Add evens_lc.py to staging area
$ git add evens_gr.py                 # Add evens_gr.py to staging area
$ git commit -m "Reading 07: Scripts" # Commit work

$ git push -u origin reading07        # Push branch to GitLab

Merge Request

Remember to create a merge request and assign the appropriate TA from the Reading 07 TA List.

DO NOT MERGE your own merge request. The TAs use open merge requests to keep track of which assignments to grade. Closing them yourself will cause a delay in grading and confuse the TAs.