Skip to content

lab HigherOrderFunction #16

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
7 changes: 6 additions & 1 deletion Exercises/1-callback.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict';

const iterate = (obj, callback) => null;
const iterate = (object, callback) => {
for (const key in object) {
const value = object[key];
callback(key, value, object);
}
};

module.exports = { iterate };
2 changes: 1 addition & 1 deletion Exercises/2-closure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

const store = x => null;
const store = x => () => x;

module.exports = { store };
21 changes: 20 additions & 1 deletion Exercises/3-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
'use strict';

const contract = (fn, ...types) => null;
const contract = (fn, ...types) => (...args) => {
args.forEach((arg, index) => {
const typeName = types[index].name.toLowerCase();
const argType = typeof arg;
if (typeName !== argType) {
throw new TypeError(`Argument type expected:
${typeName}, got: ${argType}`);
}
});
const result = fn(...args);
const expectedResultType = types[types.length - 1].name.toLowerCase();
const resultType = typeof result;

if (expectedResultType !== resultType) {
throw new TypeError(`Result type expected:
${expectedResultType}, got: ${resultType}`);
}

return result;
};

module.exports = { contract };