Javascript currying function

javascript currying es6
curried function
javascript partial function
function currying scala
javascript curry implementation
advantages of currying in javascript
currying javascript mdn
function composition javascript

I am having a problem with one of the assignments in my javascript programming course.

Assignment:

Create a function called "calculate" which expects three functions as parameters and when called returns one of those functions as a result.

Everytime you call

let result = calculate(add, subtract, multiply)(1,3)

it should return a different result.

I have tried different approaches and got different errors like calcuate is not a function or multiply is not defined.

This is what I have tried so far:

function add(num1, num2){
  return num1 + num2;
}

function subtract(num1, num2){
  return num1 - num2;
}

function multiply(num1, num2){
  return num1 * num2;
}

function calculate(add, subtract, multiply){
  
  let randNum = Math.floor(Math.random() * 3) + 1

  switch(randNum){
    case 1:
      let add = add(num1, num2)
      break;

    case 2:
      let subtract = subtract(num1, num2);
      break;

    case 3:
      let multiply = multiply(num1, num2);
      break;

  }

}

let result = calculate(add(2,4), subtract(2,4), multiply(2,4))
console.log(result);

I did some research on currying, but I can't see what I am getting wrong. Any help would be gratefully appreciated.

You could take a random function from the arguments and return this function for another call with parameters.

const
    add = (a, b) => a + b,
    subtract = (a, b) => a - b,
    multiply = (a, b) => a * b,
    calculate = (...args) => args[Math.floor(Math.random() * args.length)];

let result = calculate(add, subtract, multiply)(1, 3);

console.log(result);

Currying, Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c) . Currying doesn't call a� Currying is an incredibly useful technique from functional JavaScript. It allows you to generate a library of small, easily configured functions that behave consistently, are quick to use, and that

This isn't a great example for teaching currying. I think what the instructor/course is trying to get across here is that when you do:

let result = calculate(add, subtract, multiply)(1,3)

you're calling a function (calculate) that returns a function (one of the three you pass it, I guess picked at random though the assignment is unclear), and then you're calling that function (by using (1,3) on the result of the calculate(...) call.

calculate can be a one-liner:

function calculate(...fns) {
    return fns[Math.floor(Math.random() * fns.length)];
}

or in ES5:

function calculate() {
    return arguments[Math.floor(Math.random() * arguments.length)];
}

Live Example:

function add(num1, num2){
  return num1 + num2;
}

function subtract(num1, num2){
  return num1 - num2;
}

function multiply(num1, num2){
  return num1 * num2;
}

function calculate(...fns) {
    return fns[Math.floor(Math.random() * fns.length)];
}

let result = calculate(add, subtract, multiply)(1,3);
console.log(result);

Understanding Currying in JavaScript | by Chidume Nnamdi, Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting� Currying is a topic that often sits in the more advance user circles of JavaScript. It extends beyond the usual usage of functions and methods and starts to dive into the structural side of code.

calculate should return one of the functions so the result of the calculate function should be called with the actual params:

function add(num1, num2){
  return num1 + num2;
}

function subtract(num1, num2){
  return num1 - num2;
}

function multiply(num1, num2){
  return num1 * num2;
}

function calculate(add, subtract, multiply){

  let randNum = Math.floor(Math.random() * 3) + 1

  switch(randNum){
    case 1:
      return add
      break;

    case 2:
      return subtract
      break;

    case 3:
      return multiply
      break;

  }

}

console.log(calculate(add, subtract, multiply)(2, 4));

A Beginner's Guide to Currying in Functional JavaScript, Briefly, currying is a way of constructing functions that allows partial application of a function's arguments. What this means is that you can pass all� Currying is a transformation of a function with multiple arguments into a sequence of nested functions with a single argument. The currying allows to perform function specialization and composition. We can transform the fn (a,b,c) callable into fn (a) (b) (c).

You have two problems in this part of the code, inside your calculate function:

  switch(randNum){
    case 1:
      let add = add(num1, num2)
      break;

    case 2:
      let subtract = subtract(num1, num2);
      break;

    case 3:
      let multiply = multiply(num1, num2);
      break;

  1. You're calling you math functions (ej. add(num1, num2)) inside calculate. This will return a number but the expected behaviour is that it will return a function, so that you can call it with further parameters:

    let result = calculate(add, subtract, multiply)(1,3)
    

  2. Your calculate function is not returning any value. Thus, when your doing let result = calculate(...), result will always be undefined.

Instead of this, your calculate function should return a function:

    function calculate(add, subtract, multiply){

      let randNum = Math.floor(Math.random() * 3) + 1;

      switch(randNum){
          case 1:
          return add;

          case 2:
          return subtract;

          case 3:
          return multiply;
      }
    }

Demo: https://jsbin.com/jiholifife/edit?js,console

Note the difference: your function calculate is now returning a function. This is possible due to the fact that functions in JavaScript are First-Class objects: a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.

Currying in JavaScript, Currying is the process of taking a function with multiple arguments and returning a series of functions that take one argument and eventually resolve to a value. The original function volume takes three arguments, but once curried we can instead pass in each argument to three nested functions. Currying node.js functions can allow for sequential and parallel I/O processing of multiple files, much like the async library for node.js. Currying is not a pattern that is native to javascript,

What is Currying In JavaScript and Why Does it Matter?, Currying is a topic that often sits in the more advance user circles of JavaScript. It extends beyond the usual usage of functions and methods� Currying is a process to reduce functions of more than one argument to functions of one argument with the help of lambda calculus. f (n, m) --> f' (n) (m)

JavaScript currying, Currying. Currying is a transformation of a function with multiple arguments into a sequence of nested functions with a single argument. Currying and ES6 Arrow Functions Arrow functions are a concise way to create functions in ES6 JavaScript. Currying is a technique often used in functional programming languages like Haskell. Put them together, and you get beautiful, concise function declarations that are both easy to read and easy to test.

Currying in JavaScript - DEV, Currying is a process in functional programming in which we can transform a function with multiple ar Tagged with javascript, functional,� Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument. Here's an example in JavaScript: function add (a, b) { return a + b; } add(3, 4); // returns 7. This is a function that takes two arguments, a and b, and returns their sum.

Comments
  • "Everytime you call...it should return a different result." That's not a very clear assignment. What should it return the fourth time, if I've already returned the addition, subtraction, and multiplication results?
  • This (or the answer to it) isn't really an example of currying, not unless your instructor has some truly unnecessarily complicated solution for it. The simple solution, if using randomness, can easily be a one-liner.
  • Maybe I was bad at translating the assignment. I think what is wanted, is that if you call the function calculate it should randomly choose one of the 3 functions it received as parameters.
  • "or more concisely, but less clearly" Until you get used to arrow functions. Even then, though, if the nesting goes on, clarity can suffer (at least for me).
  • Code dumps are not useful answers. Say what you did, and why. (Before posting, not as an edit.)