From ac36b2e099c22f851853cfe428f6d69f6a315db6 Mon Sep 17 00:00:00 2001 From: Amarjit Singh Arora Date: Thu, 3 Aug 2023 21:37:29 +0530 Subject: [PATCH 1/6] A new not function added and few corrections in previous codes. --- Solutions/Amarjit_JS_fun.js | 58 ++++++++++++++++++++++++++++--------- test/Amarjit_solutions.js | 11 ++++++- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/Solutions/Amarjit_JS_fun.js b/Solutions/Amarjit_JS_fun.js index 4b1e583..1b0e453 100644 --- a/Solutions/Amarjit_JS_fun.js +++ b/Solutions/Amarjit_JS_fun.js @@ -21,33 +21,42 @@ const maxb = (a,b) => { }; const add = (...nums) => { - let n=0; - for (let i=0;i { - let n=0; - for (let i=0;i { - let n=1; - for (let i=0;i { + if (nums.length === 0) { + throw new Error("No numbers provided for minimum."); + } let n=nums[0]; for (let i=1;i { } const max = (...nums) => { - let n=0; - for (let i=0;in) n=nums[i]; } @@ -66,6 +79,10 @@ const max = (...nums) => { } const addRecurse = (...nums) => { + if (nums.length === 0) { + throw new Error("No numbers provided for adding recursively."); + } + let sum = 0; for (const num of nums) { if(Array.isArray(num)) { @@ -79,6 +96,9 @@ const addRecurse = (...nums) => { } const mulRecurse = (...nums) => { + if (nums.length === 0) { + throw new Error("No numbers provided for multiply recursively."); + } let sum = 1; for (const num of nums) { if(Array.isArray(num)) { @@ -92,6 +112,9 @@ const addRecurse = (...nums) => { } const minRecurse = (...nums) => { + if (nums.length === 0) { + throw new Error("No numbers provided for minimum recursively."); + } let min = Number.POSITIVE_INFINITY; for (const num of nums) { if(Array.isArray(num)) { @@ -108,6 +131,10 @@ const addRecurse = (...nums) => { } const maxRecurse = (...nums) => { + if (nums.length === 0) { + throw new Error("No numbers provided for maximum recursively."); + } + let max = Number.NEGATIVE_INFINITY; for (const num of nums) { if(Array.isArray(num)) { @@ -123,6 +150,10 @@ const addRecurse = (...nums) => { return max; } + const not = (func)=>{ + return !func(); + } + module.exports = { identity, addb, @@ -138,5 +169,6 @@ module.exports = { addRecurse, mulRecurse, minRecurse, - maxRecurse + maxRecurse, + not }; \ No newline at end of file diff --git a/test/Amarjit_solutions.js b/test/Amarjit_solutions.js index 02eba52..cc1e1a8 100644 --- a/test/Amarjit_solutions.js +++ b/test/Amarjit_solutions.js @@ -15,4 +15,13 @@ console.log(mathModule.max(10,2,3,11)); console.log(mathModule.addRecurse(10,2,3,[2,3,[5]],11)); console.log(mathModule.mulRecurse(10,2,3,[2,3,[5]],11)); console.log(mathModule.minRecurse(10,2,3,[2,3,[5]],11)); -console.log(mathModule.maxRecurse(10,2,3,[2,3,[5]],11)); \ No newline at end of file +console.log(mathModule.maxRecurse(10,2,3,[2,3,[5]],11)); + +// Define a test function for not method +const isEven = num => num % 2 === 0; + +// Use the 'not' function from mathModule to get the negation of the result of 'isEven' +const isOdd = mathModule.not(() => isEven(6)); + +console.log(isEven(6)); // Output: true +console.log(isOdd); // Output: false (negation of true) From 9c93a79b5baacd0614fa8ccdac9fbc588e759aa3 Mon Sep 17 00:00:00 2001 From: Amarjit Singh Arora Date: Fri, 4 Aug 2023 21:05:31 +0530 Subject: [PATCH 2/6] 3 new functions tested and added viz. acc, accPartial, accRecurse. --- Solutions/Amarjit_JS_fun.js | 38 +++++++++++++++++++++++++++++++++++-- test/Amarjit_solutions.js | 24 +++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Solutions/Amarjit_JS_fun.js b/Solutions/Amarjit_JS_fun.js index 1b0e453..1f50070 100644 --- a/Solutions/Amarjit_JS_fun.js +++ b/Solutions/Amarjit_JS_fun.js @@ -154,7 +154,38 @@ const addRecurse = (...nums) => { return !func(); } -module.exports = { + const acc = (func, initial) => { + return (...args) => { + let result = initial; + for (const arg of args) { + result = func(result, arg); + } + return result; + }; + }; + + const accPartial = (func, start, end) => { + return (...args) => { + let result = args[start]; + for (let i=start+1;i<=end;i++) { + result = func(result, args[i]); + } + return result; + }; + }; + + const accRecurse = (func, initial) => { + return (...args) => { + if(args.length===0) + return initial; + else { + const [head, ...tail] = args; + return func(initial, head) + accRecurse(func, initial)(...tail); + } + }; + }; + + module.exports = { identity, addb, subb, @@ -170,5 +201,8 @@ module.exports = { mulRecurse, minRecurse, maxRecurse, - not + not, + acc, + accPartial, + accRecurse }; \ No newline at end of file diff --git a/test/Amarjit_solutions.js b/test/Amarjit_solutions.js index cc1e1a8..bb77255 100644 --- a/test/Amarjit_solutions.js +++ b/test/Amarjit_solutions.js @@ -25,3 +25,27 @@ const isOdd = mathModule.not(() => isEven(6)); console.log(isEven(6)); // Output: true console.log(isOdd); // Output: false (negation of true) + +// Define a function to add numbers +const add = (a, b) => a + b; + +// Use the acc function to create a new function that accumulates the sum +const accumulateSum = mathModule.acc(add, 0); + +console.log(accumulateSum(1, 2, 3, 4)); // Output: 10 (0 + 1 + 2 + 3 + 4) + +// Define a function to multiply numbers +const multiply = (a, b) => a * b; + +// Use the accPartial function to create a new function that accumulates multiplication +const accumulateProduct = mathModule.accPartial(multiply, 1, 3); + +console.log(accumulateProduct(2, 3, 4, 5)); // Output: 60 (3 * 4 * 5) + +// Define a function to add numbers +const add1 = (a, b) => a + b; + +// Use the accRecurse function to create a new function that accumulates addition + const accumulateSum1 = mathModule.accRecurse(add1, 0); + +console.log(accumulateSum1(1, 2, 3, 4)); // Output: 10 (1 + 2 + 3 + 4) From 546cb7c9505c40c82a6d41267d3e58d1c5693d99 Mon Sep 17 00:00:00 2001 From: Amarjit Singh Arora Date: Sat, 5 Aug 2023 19:45:18 +0530 Subject: [PATCH 3/6] 2 nww functions fill and fillRecurse added. --- Solutions/Amarjit_JS_fun.js | 21 ++++++++++++++++++++- test/Amarjit_solutions.js | 6 ++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Solutions/Amarjit_JS_fun.js b/Solutions/Amarjit_JS_fun.js index 1f50070..57f33b6 100644 --- a/Solutions/Amarjit_JS_fun.js +++ b/Solutions/Amarjit_JS_fun.js @@ -185,6 +185,23 @@ const addRecurse = (...nums) => { }; }; + const fill = (num) => { + const arr = new Array(num); + for (let i=0; i { + if(count===0) { + return arr; + } + arr.push(num); + + return fillRecurse(num,count-1,arr); + }; + module.exports = { identity, addb, @@ -204,5 +221,7 @@ const addRecurse = (...nums) => { not, acc, accPartial, - accRecurse + accRecurse, + fill, + fillRecurse }; \ No newline at end of file diff --git a/test/Amarjit_solutions.js b/test/Amarjit_solutions.js index bb77255..2bb29f1 100644 --- a/test/Amarjit_solutions.js +++ b/test/Amarjit_solutions.js @@ -49,3 +49,9 @@ const add1 = (a, b) => a + b; const accumulateSum1 = mathModule.accRecurse(add1, 0); console.log(accumulateSum1(1, 2, 3, 4)); // Output: 10 (1 + 2 + 3 + 4) + + const res = mathModule.fill(5); + console.log(res); + + const result2 = mathModule.fillRecurse(5); + console.log(result2); From 8714c8117f56077b005558f5ed7c02d661251024 Mon Sep 17 00:00:00 2001 From: Amarjit Singh Arora Date: Mon, 7 Aug 2023 21:00:17 +0530 Subject: [PATCH 4/6] 2 more functions set and identifyf added --- Solutions/Amarjit_JS_fun.js | 12 +++++++++++- test/Amarjit_solutions.js | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Solutions/Amarjit_JS_fun.js b/Solutions/Amarjit_JS_fun.js index 57f33b6..fce830b 100644 --- a/Solutions/Amarjit_JS_fun.js +++ b/Solutions/Amarjit_JS_fun.js @@ -202,6 +202,14 @@ const addRecurse = (...nums) => { return fillRecurse(num,count-1,arr); }; + const set=(...args)=>{ + return [...new Set(args)]; + } + + const identityf = (arg)=> { + return()=>arg; + } + module.exports = { identity, addb, @@ -223,5 +231,7 @@ const addRecurse = (...nums) => { accPartial, accRecurse, fill, - fillRecurse + fillRecurse, + set, + identityf }; \ No newline at end of file diff --git a/test/Amarjit_solutions.js b/test/Amarjit_solutions.js index 2bb29f1..2d138d9 100644 --- a/test/Amarjit_solutions.js +++ b/test/Amarjit_solutions.js @@ -55,3 +55,10 @@ console.log(accumulateSum1(1, 2, 3, 4)); // Output: 10 (1 + 2 + 3 + 4) const result2 = mathModule.fillRecurse(5); console.log(result2); + + const oargs=[1,2,2,3,4,4,5]; + const uargs=mathModule.set(...oargs); + console.log(uargs); + + const fn = mathModule.identityf(7); + console.log(fn()); From 557fdc7d4001aa378f60e07c6e622df5c4ce11bd Mon Sep 17 00:00:00 2001 From: Amarjit Singh Arora Date: Tue, 8 Aug 2023 22:11:29 +0530 Subject: [PATCH 5/6] 2 more functions added viz. addf and liftf --- Solutions/Amarjit_JS_fun.js | 16 +++++++++++++++- test/Amarjit_solutions.js | 10 ++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Solutions/Amarjit_JS_fun.js b/Solutions/Amarjit_JS_fun.js index fce830b..678121e 100644 --- a/Solutions/Amarjit_JS_fun.js +++ b/Solutions/Amarjit_JS_fun.js @@ -210,6 +210,18 @@ const addRecurse = (...nums) => { return()=>arg; } + const addf = (a)=>{ + return (b)=> (a+b); + } + + const liftf = (binary)=> { + return (x) => { + return (y) => { + return binary(x,y); + }; + }; + } + module.exports = { identity, addb, @@ -233,5 +245,7 @@ const addRecurse = (...nums) => { fill, fillRecurse, set, - identityf + identityf, + addf, + liftf }; \ No newline at end of file diff --git a/test/Amarjit_solutions.js b/test/Amarjit_solutions.js index 2d138d9..f3a6e38 100644 --- a/test/Amarjit_solutions.js +++ b/test/Amarjit_solutions.js @@ -62,3 +62,13 @@ console.log(accumulateSum1(1, 2, 3, 4)); // Output: 10 (1 + 2 + 3 + 4) const fn = mathModule.identityf(7); console.log(fn()); + + const add2 = mathModule.addf(2); + console.log(add2(3)); + console.log(add2(5)); + console.log(add2(9)); + + const addl = (a,b)=>a+b; + const liftedadd=mathModule.liftf(addl); + const returnl=liftedadd(3)(5); + console.log(returnl); From 5e77350f4da74b7383374a767d1b9e9c037c74d7 Mon Sep 17 00:00:00 2001 From: Amarjit Singh Arora Date: Wed, 9 Aug 2023 21:23:37 +0530 Subject: [PATCH 6/6] 2 more functions pure and curryb added. --- Solutions/Amarjit_JS_fun.js | 19 ++++++++++++++++++- test/Amarjit_solutions.js | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Solutions/Amarjit_JS_fun.js b/Solutions/Amarjit_JS_fun.js index 678121e..51dddc9 100644 --- a/Solutions/Amarjit_JS_fun.js +++ b/Solutions/Amarjit_JS_fun.js @@ -222,6 +222,21 @@ const addRecurse = (...nums) => { }; } + const pure = (x,y)=> { + let newY=y; + let newZ; + const impure = (x)=> { + newY++; + newZ=x*newY; + } + impure(x); + return [newY, newZ]; + } + + const curryb = (binary,a)=> { + return (b)=>binary(a,b); + } + module.exports = { identity, addb, @@ -247,5 +262,7 @@ const addRecurse = (...nums) => { set, identityf, addf, - liftf + liftf, + pure, + curryb }; \ No newline at end of file diff --git a/test/Amarjit_solutions.js b/test/Amarjit_solutions.js index f3a6e38..32fd782 100644 --- a/test/Amarjit_solutions.js +++ b/test/Amarjit_solutions.js @@ -72,3 +72,19 @@ console.log(accumulateSum1(1, 2, 3, 4)); // Output: 10 (1 + 2 + 3 + 4) const liftedadd=mathModule.liftf(addl); const returnl=liftedadd(3)(5); console.log(returnl); + + +let y = 5, z; + +const result3 = mathModule.pure(20, y); +console.log(result3[1]); // Output: 120 + +const result4 = mathModule.pure(25, result3[0]); +console.log(result4[1]); // Output: 175 + +// Example usage +const add3 = (x, y) => x + y; +const curriedAdd = mathModule.curryb(add3, 5); + +console.log(curriedAdd(3)); // Output: 8 (5 + 3) +console.log(curriedAdd(10)); // Output: 15 (5 + 10) \ No newline at end of file