E18: Experience Functional Programming (Part 1)

Overview

In this experience, you will explore functional programming concepts through Underscore. Before starting, please go through the readings on Underscore so you have some basic familiarity with the library.

To make this experience more interesting and enjoyable, we’re going to use a Hawaii Open Dataset providing Five Years of UH Degree Data. Here’s a screen image of this dataset:

I suggest you go to Five Years of UH Degree Data and play around a little bit so you understand how the data is structured.

Set up JSFiddle with Underscore

Next, open up JSFiddle, click on the settings button associated with the Javascript window, and select “Underscore 1.8.3” to load Underscore into your JSFiddle. Here’s what it should look like:

To verify that Underscore is available in your JSFiddle, invoke an underscore function (such as identity()) and log the output to the console. For example:

Set up JSFiddle with UH Degree Data

To make things easier, I’ve set up a web page at https://ics-software-engineering.github.io/uhdata/uhdata.js that defines a variable called “uhdata” whose value is an array of objects, one for each row in the table. Here’s what the first couple of rows look like:

var uhdata = [
  {
    "FISCAL_YEAR":2010,
    "CAMPUS":"UH Manoa",
    "CIP":220101,
    "CIP_DESC":"Law",
    "GROUP1":"School of Law",
    "GROUP2":"School of Law",
    "GROUP3":"Law",
    "GROUP4":"",
    "GROUP5":"",
    "OUTCOME":"Other",
    "HAWAIIAN_LEGACY":"",
    "AWARDS":69
  },
  {
    "FISCAL_YEAR":2010,
    "CAMPUS":"Kapi`olani CC",
    "CIP":240101,
    "CIP_DESC":"Liberal Arts",
    "GROUP1":"General & Pre-Prof Ed",
    "GROUP2":"Liberal Arts",
    "GROUP3":"Liberal Arts",
    "GROUP4":"",
    "GROUP5":"",
    "OUTCOME":"Associate Degrees",
    "HAWAIIAN_LEGACY":"",
    "AWARDS":322
  }

To load this data into JSFiddle, you need to specify it as an “External Resource”. Click on that link on the left side of the window, enter https://ics-software-engineering.github.io/uhdata/uhdata.js where it says “JavaScript/CSS URI”, and click the + icon to save it. You should see “uhdata.js” listed as an external resource. For example:

To test that the uhdata variable is defined correctly, try printing out its length to the console. You should get a length of 3744:

The WOD

Now that you have Underscore and the UH degree data available in JSFiddle, the fun begins!

  1. Start your timer.

  2. Create a variable called testData and set it equal to the first few elements of the uhdata array. Use this variable to check that your functions work correctly before running them on the full data set.

  3. Use the functions provided by the underscore package to implement the following two functions. Note that your solutions cannot include a for loop or an if statement!

  4. totalDegrees(data). This function is passed a data structure like uhdata and returns the total number of degrees awarded in the data set. For the uhdata set, the correct answer is 48,186.

  5. percentageHawaiian(data). This function can be passed uhdata and returns the percentage of degrees that were awarded to students of Hawaiian Legacy in the dataset. Such students are indicated by the string “HAWAIIAN” in the “HAWAIIAN_LEGACY” field. For the uhdata set, correct answer is about 18.9%.

  6. Press the “save” button to create a URL to refer to your code.

  7. Stop your timer and record your time. Be sure to record it, because you will need your WOD time data when you write your technical essay.

Rx: < 17 min Av: 17 - 25 min Sd: 25 - 30 min DNF: 30+ min

Demonstration

Once you’ve finished trying the WOD for the first time, watch me do it.

Standard WOD Caveats

You’ll learn significantly less from watching me solve the WOD if you haven’t attempted the WOD yourself first.

While it’s an achievement to finish the WOD no matter how long it takes, you might experience “diminishing returns” if you work longer than the DNF time. Thus, it is usually strategic to stop working at the DNF time and watch my solution.

After watching my solution, I recommend that you repeat the WOD if you have not achieved at least Av performance. If so, be sure to:

Feel free to keep trying until you make Rx if that’s of interest to you.

Submission Instructions

By the time and date indicated on the Schedule page, submit this assignment via Laulima.

Your submission should contain:

You will receive full credit for this practice WOD as long as you have attempted it at least once and submitted the email with all required data before the due date. Your code does not have to run perfectly for you to receive full credit. However, if you do not repeat each practice WOD until you can finish it successfully in at least AV time, you are unlikely to do well on the in-class WOD. To reduce the stress associated with this course, I recommend that you repeat each practice WOD as many times as necessary to achieve at least AV before its due date.