Skip to content

HigherOrderFunction Lab 8 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"linebreak-style": [
"error",
"unix"
"windows"
],
"quotes": [
"error",
Expand Down
11 changes: 9 additions & 2 deletions Exercises/1-callback.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
'use strict';

const iterate = (obj, callback) => null;

const iterate = (obj, callback) => {
for (const key in obj) {
callback(key, obj[key], obj);
}
};
const obj = { a: 1, b: 2, c: 3 };
iterate(obj, (key, value) => {
console.log({ key, value });
});
module.exports = { iterate };
9 changes: 8 additions & 1 deletion Exercises/2-closure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
'use strict';

const store = x => null;
// const store = value => {
// const storage = [value];
// return () => storage.shift();
// };
const store = value => () => value;

const read = store(5);
const value = read();
console.log(value);
module.exports = { store };
19 changes: 18 additions & 1 deletion Exercises/3-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
'use strict';

const contract = (fn, ...types) => null;
const contract = (fn, ...types) => (...args) => {
for (let i = 0; i < args.length; i++) {
if (typeof(args[i]) !== types[i].name.toLowerCase()) throw new TypeError();
}
const res = fn(...args);
const resExpectedType = types.pop().name.toLowerCase();
if (typeof(res) !== resExpectedType) throw new TypeError();
return res;
};

const add = (a, b) => a + b;
const addNumbers = contract(add, Number, Number, Number);
const res1 = addNumbers(2, 1);
console.dir(res1);
const concat = (s1, s2) => s1 + s2;
const concatStrings = contract(concat, String, String, String);
const res2 = concatStrings('Hello ', 'world!');
console.dir(res2);

module.exports = { contract };
Loading