HW2

.pdf

School

New York University *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

14

Uploaded by dj1380

8/25/23, 5:54 PM CSCI-UA.0480 - Homework https://cs.nyu.edu/courses/fall22/CSCI-UA.0467-001/_site/homework/02.html 1/14 Homework #2 Higher Order Functions: Exercises and Processing Data Overview Repository Creation Click on this link: https://classroom.github.com/a/8G_m2IZh (https://classroom.github.com/a/8G_m2IZh) to accept this assignment in GitHub classroom. This will create your homework repository Clone it once you've created it. Description hoffy.mjs - Write a series of functions that demonstrate the use of the rest operator (or call/apply), and higher order functions sfmovie.mjs and report.mjs - Print out a report analyzing movies filmed at San Francisco, which you will import as a csv file. See the sample output at the end of these instructions. Submission Process You will be given access to a private repository on GitHub. It will contain unit tests, stub files for your code, a package.json and a .eslintrc . The final version of your assignment should be in GitHub. Push your changes to the homework repository on GitHub. (4 points) Make at Least 4 Commits Commit multiple times throughout your development process. Make at least 4 separate commits - (for example, one option may be to make one commit per part in the homework). Part 1 - Setup and Exercises For this homework, you'll have files available in your repository, so you'll be cloning first. The solutions to the following problems can go in the same file - src/hoffy.mjs : Setup 1. go to your github account… and under your profile and organizations 2. find your repository: homework02-yourgithubusername 3. use the appropriate URL to run git clone git clone [YOUR REPO URL] Background Info Implement functions that use JavaScript features such as: the rest operator the spread operator
8/25/23, 5:54 PM CSCI-UA.0480 - Homework https://cs.nyu.edu/courses/fall22/CSCI-UA.0467-001/_site/homework/02.html 2/14 functions as arguments functions as return values decorators optionally call/apply/bind optionally arrow functions Array methods: 'filter', 'map', 'reduce' Go through the functions in order; the concepts explored build off one-another, and the functions become more and more challenging to implement as you go on. Do not use : while loops for loops for ... in loops for ... of loops forEach method . There will a small (-2) penalty every time one is used . (Homework is 100 points total) Steps 1. prep… NOTE that if the .eslint supplied is not working (it likely isn't), please copy over the .eslint.cjs file from you previous assignment(s) (it's a hidden file and starts with . ) … or see the edstem thread on how to update the config create a .gitignore to ignore node_modules create a package.json by using npm init make sure that mocha , chai , and eslint are still installed (similar to previous assignment) npm install --save-dev mocha npm install --save-dev eslint npm install --save-dev chai npm install --save-dev eslint-plugin-mocha you'll also need a few additional modules installed locally for the unit tests to run: finally, install sinon and mocha-sinon locally for mocking console.log (these are for unit tests) npm install --save-dev sinon npm install --save-dev mocha-sinon 2. implement functions below in hoffy.mjs 3. make sure you export your functions as you implement them so that… 4. you can run tests as you develop these functions (again, the tests are included in the repository): npx mocha tests/hoffy-test.mjs 5. also remember to run eslint (there's a .eslintrc file included in the repository): npx eslint src/* (40 points) Functions to Implement (-2 per while, for, forEach, for of, or for in loop used) getEvenParam(s1, s2, s3 to sN) Parameters: s1, s2, s3 to sN - any number of string arguments Returns: an array of parameters that have even index (starting from 0)
8/25/23, 5:54 PM CSCI-UA.0480 - Homework https://cs.nyu.edu/courses/fall22/CSCI-UA.0467-001/_site/homework/02.html 3/14 if no arguments are passed in, give back an empty array Description: Goes through every string argument passed in and picks up the even index parameters (suppose the first parameter has index 0, second has index 1, and so on). No error checking is required; you can assume that every argument is a string or no arguments are passed in at all. If there are no arguments passed in, return an empty array HINTS: use rest parameters! Example: getEvenParam('foo', 'bar', 'bazzy', 'qux', 'quxx') // --> ['foo', 'bazzy', 'quxx'] getEvenParam('foo', 'bar', 'baz', 'qux') // --> ['foo', 'baz'] getEvenParam() // --> [] maybe(fn) Parameters: fn - the function to be called Returns: a new function or undefined - the function calls the original function Description: maybe will take a function, fn and return an entirely new function that behaves mostly like the original function, fn passed in, but will return undefined if any null or undefined arguments are passed in to fn . The new function will take the same arguments as the original function ( fn ). Consequently when the new function is called, it will use the arguments passed to it and call the old function and return the value that's returned from the old function. However, if any of the arguments are undefined or null , the old function is not called, and undefined is returned instead. You can think of it as a way of calling the old function only if all of the arguments are not null or not undefined . Example: function createFullName(firstName, lastName) { return `${firstName} ${lastName}`; } maybe(createFullName)('Frederick', 'Functionstein'); // Frederick Functionstein maybe(createFullName)(null, 'Functionstein'); // undefined maybe(createFullName)('Freddy', undefined); // undefined filterWith(fn) Parameters: fn - a callback function that takes in a single argument and returns a value (it will eventually operate on every element in an array) Returns: function - a function that… has 1 parameter, an Array returns a new Array where only elements that cause fn to return true are present (all other elements from the old Array are not included) Description:
8/25/23, 5:54 PM CSCI-UA.0480 - Homework https://cs.nyu.edu/courses/fall22/CSCI-UA.0467-001/_site/homework/02.html 4/14 This is different from regular filter. The regular version of filter immediately calls the callback function on every element in an Array to return a new Array of filtered elements. filterWith , on the other hand, gives back a function rather than executing the callback immediately (think of the difference between bind and call/apply). filterWith is basically a function that turns another function into a filtering function (a function that works on Arrays). Example: // original even function that works on Numbers function even(n) {return n % 2 === 0;} // create a 'filter' version of the square function filterWithEven = filterWith(even); // now square can work on Arrays of Numbers! console.log(filterWithEven([1, 2, 3, 4])); // [2, 4] const nums = [1, NaN, 3, NaN, NaN, 6, 7]; const filterNaN = filterWith(n => !isNaN(n)); console.log(filterNaN(nums)); // [1, 3, 6, 7] repeatCall(fn, n, arg) Parameters: fn - the function to be called repeatedly n - the number of times to call function, fn arg - the argument to be passed to fn , when it is called Returns: undefined (no return value) Description: This function demonstrates using functions as an argument or arguments to another function. It calls function, fn , n times, passing in the argument, arg to each invocation / call. It will ignore the return value of function calls. Note that it passes in only one arg . Hint: Write a recursive function within your function defintion to implement repetition. Examples: repeatCall(console.log, 2, "Hello!"); // prints out: // Hello! // Hello! // calls console.log twice, each time passing in only the first argument repeatCall(console.log, 2, "foo", "bar", "baz", "qux", "quxx", "corge"); // prints out (again, only 1st arg is passed in): // foo // foo largerFn(fn, gn) Parameters: fn - candidate function to be returned, takes one integer parameter gn - candidate function to be returned, takes one integer parameter Returns:
8/25/23, 5:54 PM CSCI-UA.0480 - Homework https://cs.nyu.edu/courses/fall22/CSCI-UA.0467-001/_site/homework/02.html 5/14 function - a function that… takes two arguments… an argument for fn , and gn … and itself returns yet another function, a "chooser" function this "chooser" function compares the result of calling fn and gn ( fn and gn each takes one parameter) the "chooser" function returns either fn or gn depending on whichever function generates larger output (you can assume the value is not going to overflow and if the outputs are equal, return fn) Description: This function is a decorator. See the slides on the decorator pattern (../slides/js/higher-order-functions- continued.html) for background information. It builds on top of the example in the slides by actually modifying the return value of the original function. This function wraps the function fn and gn in another function so that operations can be performed before and after the original function fn and gn are called. This can be used to modify incoming arguments, modify the return value, or do any other task before or after the function call. Again, we'll be modifying the return value in this case. Example: // creates two functions, foo and bar function foo(x) { return x * x; } function bar(y) { return y * y * y; } // call largerFn const decorator = largerFn(foo, bar); const newFn = decorator(5,3); // 5*5 < 3*3*3 so newFn is bar console.log(newFn(5)) // -> prints 125 limitCallsDecorator(fn, n) Parameters: fn - the function to modify ( decorate ) n - the number of times that fn is allowed to be called Returns: function - a function that… does the same thing as the original function, fn (that is, it calls the original function) but can only be called n times after the n th call, the original function, fn will not be called, and the return value will always be undefined Description: You'll read from a variable that's available through the closure and use it to keep track of the number of times that a function is called… and prevent the function from being called again if it goes over the max number of allowed function calls. Here are the steps you'll go through to implement this: 1. create your decorator (function) 2. create a local variable to keep track of the number of calls 3. create an inner function to return the inner function will check if the number of calls is less than the max number of allowed calls if it's still under max, call the function, fn (allow all arguments to be passed), return the return value of calling fn , and increment the number of calls if it's over max, just return undefined immediately without calling the original function
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help