E20: Experience Functional Programming (Part 3)

Let’s finish our exploration of functional programming concepts.

As before, we’ll use a Hawaii Open Dataset providing University of Hawaii Student Degrees Awarded. Here’s a screen image of this dataset:

picture

As a reminder, here’s what the uhdata variable’s value looks like:

type UhData = {
fiscalYear: number;
campus: string;
cip: number;
cipDesc: string;
group1: string;
group2: string;
group3: string;
group4: string;
group5: string;
outcome: string;
hawaiianLegacy: string;
awards: number;
};

const uhdata: UhData[] = [
  {fiscalYear: 2023, campus: "UH Hilo", cip: 90101, cipDesc: "Speech Communicatn & Rhetoric", group1: "College of Arts & Sciences", group2: "Arts & Humanities", group3: "Communication", group4: "Communication", group5: "", outcome: "Bachelor's Degrees", hawaiianLegacy: "HAWAIIAN", awards: 16},
  {fiscalYear: 2023, campus: "UH Manoa", cip: 130301, cipDesc: "Curriculum & Instruction", group1: "College of Education", group2: "Education", group3: "Curriculum Studies", group4: "Curriculum Studies", group5: "", outcome: "Master's Degrees",
    hawaiianLegacy: "HAWAIIAN", awards: 20},
  {fiscalYear: 2023, campus: "UH Manoa", cip: 140801, cipDesc: "Civil Engineering", group1: "College of Engineering", group2: "Engineering", group3: "Civil Engineering", group4: "", group5: "", outcome: "Master's Degrees", hawaiianLegacy: "", awards: 16},
];

The WOD

Ready to have some more fun?

  1. Start your timer.

  2. Open your solution from the previous experience.

  3. Implement the following functions using the Array functional methods. Note that your solutions cannot include a for or while loop!

  4. listCampusDegrees(data). This function can be passed uhdata and returns an array of objects of type {campus: string; degrees: number}. Hint: one approach is to use the groupBy, mapGroupBy and a for (const [key, value] of Object.entries(obj)) loop. The results for uhdata should look like: [{ "campus": "UH Hilo", "degrees": 486 }, { "campus": "UH Manoa", "degrees": 3042 }, { "campus": "Kaua`i CC", "degrees": 286 }, { "campus": "Windward CC", "degrees": 338 }, { "campus": "Kapi`olani CC", "degrees": 679 }, { "campus": "Maui College", "degrees": 363 }, { "campus": "Hawai`i CC", "degrees": 405 }, { "campus": "Honolulu CC", "degrees": 539 }, { "campus": "Leeward CC", "degrees": 922 }, { "campus": "UH West O`ahu", "degrees": 320 }]

  5. maxDegrees(data). This function can be passed uhdata, computes the number of degrees earned for each campus in the data set, and then returns an integer which is the number of degrees earned for the campus where the most degrees were earned. Hint: one approach is to use the values, sort functions methods and the groupBy, mapGroupObject functions given below. The correct number of degrees is 3042.

  6. Press the “Share” button to save the Playground URL.

  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: < 12 min Av: 12 - 16 min Sd: 16 - 20 min DNF: 20+ min             

function groupBy(data: any[], key: string) {
  return data.reduce((result:any, currentValue:any) => {
    (result[currentValue[key]] = result[currentValue[key]] || []).push(currentValue);
    return result;
  }, {});
}

function mapGroupBy(data: Map<string, any[]>): Map<string, number> {
  return new Map(Object.entries(data).map(([key, value]) => [key, value.length]));
}

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 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 your work on Laulima with all required data before the due date. Your code does not have to run perfectly for you to receive full credit.