Calling function through Apply vs direct calling from Currying functions - javascript

I'm trying to implement currying function:
function sum(a, b, c) {
console.log('SUM'+JSON.stringify(arguments))
return a + b + c;
}
var curry= function (func,...n) {
return function(...args2) {
//console.log('arg2s'+args2)
return func.apply(this,n.concat(args2));
};
}
curry(sum,1,2)(3) //SUM{"0":1,"1":2,"2":3}, O/P:6
Above gives correct output, but i am not getting the necessity to use apply.
So, if i change my function to below:
var curry= function (func,...n) {
return function(...args2) {
//console.log('arg2s'+args2)
return func(n.concat(args2));
};
}
curry(sum,1,2)(3) //SUM{"0":[1,2,3]} O/P:"1,2,3undefinedundefined"
I basically have two questions here:
Why is the implementation of curry not working when calling func directly?Why is the output such weird ?
2.How do i change my function in a way that i can call it as below and should return sum:
curry(1)(2)(3)/curry(1,2)(3)/curry(1,2,3) etc instead of the way i'm calling currently.
I could find some solutions on the web, but couldn't understand .
ex:
function curry(func) {
return function curriedFunc(...args) {
if (args.length >= func.length) {
return func.apply(this, args);
} else {
return function(...args1) {
return curriedFunc.apply(this, args.concat(args1));
}
}
};
}
Any help would be highly appreciated !!

Your func accepts three arguments: a, b, and c. When you do:
return func(n.concat(args2));
you're passing it one argument, an array. It's equivalent to:
const arr = n.concat(args2);
return func(arr);
Sounds like you'd want to spread the arguments into the call of func instead:
return func(...n, ...args2)
With spread syntax, each item from the iterable is put into a parameter. Eg, if the n array has one item, it gets set as the first argument passed to func, and if args2 has 2 items, the first gets set as the second argument passed, and the second gets set as the third argument passed.
function sum(a, b, c) {
console.log('SUM' + JSON.stringify(arguments))
return a + b + c;
}
var curry = function(func, ...n) {
return function(...args2) {
return func(...n, ...args2);
};
}
curry(sum, 1, 2)(3);
How do i change my function in a way that i can call it as below and should return sum: curry(1)(2)(3)/curry(1,2)(3)/curry(1,2,3) etc
Keep track of the total number of arguments passed in a closure created the first time the function is called. In the returned function, push all arguments to the array, and if the array's length is 3, return the sum, otherwise return the function again:
function sum(a, b, c) {
return a + b + c;
}
var curry = function(func, ...argsSoFar) {
const neededArgs = func.length;
const returnedFn = (...args) => {
argsSoFar.push(...args);
if (argsSoFar.length === neededArgs) {
return func(...argsSoFar);
} else {
return returnedFn;
}
};
return returnedFn;
}
console.log(
curry(sum, 1, 2)(3),
curry(sum)(1, 2)(3),
curry(sum)(1)(2, 3),
curry(sum)(1)(2)(3),
);

Related

tried to recreate the _.once function but only works upon at least 2 iterations not one

onceCopy function (testFunc) {
const copyFunc = (a) => {
const copyFunc2 = (b) => {
return testFunc(a);
};
return copyFunc2;
};
return copyFunc;
};
So the function returns the inner function upon first invocation.
Then returns the inner function of the inner function of the second invocation.
Then the second inner function (third invocation) actually returns the passed argument in the parent function and only invokes it with the character we gave it on the second invocation.
Ideally I want to achieve what I'm achieving over many invocations after only the first one if that makes sense.
Edit: Yes sorry, _.once.
Edit: so first invocation onceCopy SHOULD hold a copy of the Func passed
Second invocation SHOULD trigger the copy and gives an ouput
Third invocation SHOULD give the result of the second invocation so should the fourth, fifth, sixth and so on...
My function does do this, but on the second invocation it stores a function (copyFunc2) again, but I just made that because I need somewhere to store "a".
so like we have
function multiplyBy3 (a) {return a*3}
and then once copy stores a copy of multiplyBy3
const actualFunction = onceCopy(multiplyBy3)
then upon second and third invocation what I want
actualFunction(1) = 3
actualFunction(66) = 3
so the passed function ONLY RUNS ONCE
Cant explain more than this, its in the lodash docs.
I'm not familiar with the function you're trying to reimplement, so feel free to correct me if I misunderstood. To wrap a function and ensure it's only called once you don't need multiple nested wrappings, only one with some state.
You need to keep track of whether you already have a result to return (hasResult) and if so, what that result is (result). Keeping these two variables separate allows you to cover the case when result is undefined while keeping the code easy to read and understand.
function once(wrappedFunction) {
let hasResult = false;
let result;
return (...args) => {
if (hasResult) {
return result;
}
result = wrappedFunction.apply(this, args);
hasResult = true;
return result;
}
}
// An example function to wrap
function multiply(a, b) {
return a * b;
}
const demoFunction = once(multiply)
console.log(demoFunction(1, 2)); // 2
console.log(demoFunction(3, 4)); // 2
console.log(demoFunction(5, 6)); // 2
Is this what you were looking for?
The initial function return is kept on subsequent calls. I used a second variable called in case the first call returns undefined, which should also be returned on subsequent calls.
const once = (onceFn) => {
let called;
let value;
return (...args) => {
if (called) return value;
called = true;
return (value = onceFn(...args));
};
};
function multiplyBy3(a) {
return a * 3;
}
const fn = once(multiplyBy3);
console.log(fn(3)); // 9
console.log(fn(66)); // 9
After calling the function for the 1st time, and getting the result, create a new function that returns the result, and use it whenever the wrapped function is called:
const once = fn => {
let func
return (...args) => {
if(func) return func()
const result = fn(...args)
func = () => result
return result
}
}
const multiply = (a, b) => a * b
const demoFunction = once(multiply)
console.log(demoFunction(1, 2)); // 2
console.log(demoFunction(3, 4)); // 2
console.log(demoFunction(5, 6)); // 2

Passing a function and its parameters as a parameter to another

I've seen a few answers but its still a little unclear to me how to go about this.
I have a retry function, whose parameter of fn I'd like to call with the original parameters supplied to it:
function retry(fn, retries=3, err=null) {
if (!retries) {
return Promise.reject(err);
}
return fn().catch(err => {
return retry(fn, (retries - 1), err);
});
}
Is there a clean way to do this in JS?
In short, I'd like to do something like:
function a(b,c,d) { return Promise.resolve(b + c + d) }
retry(a, ...originalParams)
Besides what you've covered yourself, another way would be to wrap your function and it's arguments into a thunk. However, it's not exactly what you would like it to be.
function retry(fn, retries=3, err=null) {
if (!retries) {
return Promise.reject(err);
}
return fn().catch(err => {
return retry(fn, (retries - 1), err);
});
}
function toThunk(fn, ...args) {
// note that we return a new function which closes over
// the function and arguments here (e. g. creating closures)
return () => fn(...args);
}
function sumP(a, b, c) {
return Promise.resolve(a + b + c);
}
// ---
retry(toThunk(sumP, 1, 2, 3)).then(result => {
console.log(result);
});

Passing arguments differently, function should still output the same value

Is there any way that I can write the function so that when I call it even by passing arguments differently, it still outputs the same value
I am new to JavaScript and was recently asked this question in an interview -
Write a function sum which when called like sum(2)(3) or sum(2, 3) should return 5.
This could be done individually as follows
function sum(a){
return function(b){
return a+b
}
}
or
function sum(a, b){
return a+b
}
If I follow the first code, it won't execute sum(a, b) and of course the second code will not support sum(a)(b). Is there any way that I can merge the two code snippets so that it executes doesn't matter how I call it ?
You'll need to check how many arguments were passed. If two are passed, return the added values; otherwise, return a function that, when called, returns its argument plus the closure's argument:
const sum = (...args) => {
if (args.length === 2) {
return args[0] + args[1];
}
return arg => arg + args[0];
};
console.log(sum(2)(3));
console.log(sum(2, 3));
More generally, you can create a makeSum function to handle when the total number of arguments to accept is an arbitrary number:
const makeSum = totalArgCount => {
const fn = (...args) => {
if (args.length === totalArgCount) {
return args.reduce((a, b) => a + b, 0);
}
return fn.bind(undefined, ...args);
};
return fn;
};
const sum2 = makeSum(2);
console.log(sum2(2)(3));
console.log(sum2(2, 3));
const sum4 = makeSum(4);
console.log(sum4(2)(3)(4)(5));
console.log(sum4(2, 3, 4)(5));
function sum(a, b) {
return b !== undefined ? (a + b) : function(b) { return a + b; }
}
console.log(sum(1,2));
console.log(sum(1)(3));
You can do it like this, by checking the b argument. If it is undefined, then you return a function. If it is not, you return the sum of the numbers.
Of course you could also check the both arguments are numbers, but this is simple version to illustrate what you want.

lodash function to return the result of .tap

I'm looking for a lodash (or underscore) function that will return the result of .tap.
I want something like this:
var run = function(collection, fn) {
return fn(collection);
};
_.mixin({run: run});
I'm hoping I've overlooked something in the docs so I don't have to create my own mixin.
You can create a generic mixin like this:
function add(a, b, c) {
return a + b + c
}
function sub(a, b, c) {
return a - b - c
}
_.mixin({
run: function (v, f) {
var args = Array.prototype.slice.call(arguments, 2)
args.unshift(v)
return f.apply(this, args)
}
})
var r = _.chain(1).run(add, 1, 1).run(sub, 2, 2).value()
console.log(r) -> -1
1 + 1 + 1 - 2 - 2 = -1
You can pass function and additional parameter. The function will be called with the result from the last chain operation as first parameter and the additional parameter from the run call.
In lodash, you can use _.thru since v3:
_.thru(value, interceptor, [thisArg])
This method is like _.tap except that it returns the result of interceptor.

How to write a function that can be called with two pairs of parenthesis?

Recently, I was asked to write add(a)(b) function and this will return a+b. I had no clue how to write this sort function in javascript. How to write this function so that it returns a + b ?
Write a function that returns a function:
function add(a) {
return function(b) {
return a + b
}
}
In JavaScript, functions can be used just like normal objects. In fact, they are objects!
add would need to return a function:
function add( a ) {
return function( b ) {
return a + b;
}
}
add(1)(2) --> 3

Categories

Resources