HigherOrderFunction icon indicating copy to clipboard operation
HigherOrderFunction copied to clipboard

My work

Open Destaby opened this issue 6 years ago • 3 comments

Destaby avatar Dec 16 '19 02:12 Destaby

Please rebase on master to activate CI

tshemsedinov avatar Dec 16 '19 09:12 tshemsedinov

'use strict';

const contract = (fn, ...types) => (...arr) => { for (let i = 0; i < types.length - 1; i++) { const argType = typeof arr[i]; const neededArgType = types[i].name.toLowerCase(); if (argType !== neededArgType) { throw new TypeError('Types are different'); } } const neededReType = types[types.length - 1].name.toLowerCase(); const result = fn(...arr); if (typeof result !== neededReType) { throw new TypeError('Types are different'); } return result; };

module.exports = { contract };

Is it correct now?

On Mon, 16 Dec 2019 at 21:28, Timur Shemsedinov [email protected] wrote:

@tshemsedinov requested changes on this pull request.

In Exercises/3-wrapper.js https://github.com/HowProgrammingWorks/HigherOrderFunction/pull/4#discussion_r358410781 :

@@ -1,5 +1,13 @@ 'use strict';

-const contract = (fn, ...types) => null; +const contract = (fn, ...types) => (...arr) => {

  • for (let i = 1; i < types.length - 1; i++) {
  • if (typeof fn(...arr) !== types[i].name.toLowerCase()) {
  •  throw new TypeError('Types are different');
    
  • }
  • }
  • return fn(...arr);

Check result type after calling fn

In Exercises/3-wrapper.js https://github.com/HowProgrammingWorks/HigherOrderFunction/pull/4#discussion_r358421536 :

@@ -1,5 +1,21 @@ 'use strict';

-const contract = (fn, ...types) => null; +const contract = (fn, ...types) => (...arr) => {

  • const resultType = typeof fn(...arr);
  • const neededReType = types[types.length - 1].name.toLowerCase();
  • for (let i = 0; i < types.length - 1; i++) {
  • const argType = typeof arr[i];
  • const neededArgType = types[i].name.toLowerCase();
  • if (argType !== neededArgType) {
  •  throw new TypeError('Types are different');
    
  • }
  • if (resultType !== neededReType) {

Why do you check result types in loop?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/HowProgrammingWorks/HigherOrderFunction/pull/4?email_source=notifications&email_token=AMXNJKMGX2D4ZWJAGXUE6R3QY7JFFA5CNFSM4J3DL4S2YY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCPLBF3Y#pullrequestreview-332796655, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMXNJKKZDZXCRQH47WYLZRDQY7JFFANCNFSM4J3DL4SQ .

Destaby avatar Dec 17 '19 22:12 Destaby

Please don't send changes in email, commit it directly to git because I need to see diff

tshemsedinov avatar Dec 17 '19 23:12 tshemsedinov