Advantages of using curried functions over a normal function in javascript

前端 未结 1 1945
庸人自扰
庸人自扰 2021-01-24 06:24

Below is a specific use case of using a normal and a curried function. Are there any advantages for using either if you only using two arguments?

//Normal Functi         


        
1条回答
  •  余生分开走
    2021-01-24 06:31

    Here's a small example:

    let add = (x, y) => x + y;
    let addc = x => y => x + y;
    
    // add 5 to every element
    
    result = [1,2,3,4,5].map(x => add(x, 5))  // dirty and tedious
    result = [1,2,3,4,5].map(addc(5))         // nice and tidy
    

    In general, curried functions allow to express the logic in a "point-free" style, that is, as a combination of functions, without using variables, arguments and similar constructs.

    0 讨论(0)
提交回复
热议问题