Functional programming- Java-script example 2
What is a Curried Function?
A curried function is a function that takes exactly one parameter while taking multiple parameters.
It is useful for re-using, and partially using.
Example of the Curried function(.js)
This code has been regenerated by referring to the next lecture(Ref)
This code is an example of a curry function in functional programming. In other words, it is the code that the function is used while creating the other stacked-formed function.
- ‘add’ function : stacked-formed function which is operated only when inputted two-variables.
Codes(.js)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>category_classification</title>
</head>
<body>
<script>
function _curried_func(f){
return function(i1_var) {
return function(i2_var){
return f(i1_var, i2_var)
}
}
}
var add = _curried_func(function(i1,i2){return i1+i2;});
var add1 = add(1);
// ↓ 'add10'fuction is started rightly after '2' is inputted.
console.log(add1(2));
// ↓ the function is wating until (2) is inputted to the 'add' function
console.log(add(1)(2));
// we can see the same results.
</script>
</body>
</html>