I'm learning about currying and I decided to try to make a curried minimum function called getMin
.
To my understanding, this means that I should be able to call
getMinimum(5)(2)(6)
and have it return 2
.
I tried to implement this with a simple closure and I came up with something that returns numbers instead of functions. Here's my code:
function getMin(val){
var min = val
function calc(num){
if(num<min){
// set new minimum
min = num
return num
}
else {
return min
}
}
return calc
}
var x = getMin(5) // => 5
console.log(x(6))
console.log(x(4))
console.log(x(8))
console.log(x(2))
This logs:
5
4
4
2
This doesn't meet the requirements of currying.
So as I consider how I might change this function so that it returns a function, I run into a problem. Every time the curried function is called with a number argument, it should return the minimum (a number), but if I am understanding this correctly, it should also return a function (so that it can be called once again with another number). How does this work?