Skip to content

Commit

Permalink
Merge pull request #226 from asarora72/as_git
Browse files Browse the repository at this point in the history
A new not function added and few corrections in previous codes.
  • Loading branch information
l-white committed Aug 12, 2023
2 parents 7d5ff62 + 5e77350 commit c38ad10
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 15 deletions.
154 changes: 140 additions & 14 deletions Solutions/Amarjit_JS_fun.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,42 @@ const maxb = (a,b) => {
};

const add = (...nums) => {
let n=0;
for (let i=0;i<nums.length;i++) {
if (nums.length === 0) {
throw new Error("No numbers provided for addition.");
}
let n=nums[0];
for (let i=1;i<nums.length;i++) {
n+=nums[i];
}
return n;
}

const sub = (...nums) => {
let n=0;
for (let i=0;i<nums.length;i++) {
if(n===0)
n=nums[i];
else
n-=nums[i];
if (nums.length === 0) {
throw new Error("No numbers provided for subtraction.");
}
let n=nums[0];
for (let i=1;i<nums.length;i++) {
n-=nums[i];
}
return n;
}

const mul = (...nums) => {
let n=1;
for (let i=0;i<nums.length;i++) {
if (nums.length === 0) {
throw new Error("No numbers provided for multiplication.");
}
let n=nums[0];
for (let i=1;i<nums.length;i++) {
n*=nums[i];
}
return n;
}

const min = (...nums) => {
if (nums.length === 0) {
throw new Error("No numbers provided for minimum.");
}
let n=nums[0];
for (let i=1;i<nums.length;i++) {
if(nums[i]<n)
Expand All @@ -57,15 +66,23 @@ const min = (...nums) => {
}

const max = (...nums) => {
let n=0;
for (let i=0;i<nums.length;i++) {
if (nums.length === 0) {
throw new Error("No numbers provided for maximum.");
}

let n=nums[0];
for (let i=1;i<nums.length;i++) {
if(nums[i]>n)
n=nums[i];
}
return n;
}

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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -123,7 +150,94 @@ const addRecurse = (...nums) => {
return max;
}

module.exports = {
const not = (func)=>{
return !func();
}

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);
}
};
};

const fill = (num) => {
const arr = new Array(num);
for (let i=0; i<arr.length; i++) {
arr[i]=num;
}
return arr;
};

const fillRecurse = (num,count=num,arr=[]) => {
if(count===0) {
return arr;
}
arr.push(num);

return fillRecurse(num,count-1,arr);
};

const set=(...args)=>{
return [...new Set(args)];
}

const identityf = (arg)=> {
return()=>arg;
}

const addf = (a)=>{
return (b)=> (a+b);
}

const liftf = (binary)=> {
return (x) => {
return (y) => {
return binary(x,y);
};
};
}

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,
subb,
Expand All @@ -138,5 +252,17 @@ module.exports = {
addRecurse,
mulRecurse,
minRecurse,
maxRecurse
maxRecurse,
not,
acc,
accPartial,
accRecurse,
fill,
fillRecurse,
set,
identityf,
addf,
liftf,
pure,
curryb
};
74 changes: 73 additions & 1 deletion test/Amarjit_solutions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,76 @@ 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));
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)

// 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)

const res = mathModule.fill(5);
console.log(res);

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());

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);


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)

0 comments on commit c38ad10

Please sign in to comment.