Does JavaScript support partial function application?

后端 未结 4 727
盖世英雄少女心
盖世英雄少女心 2021-01-05 01:18

Reading through the Wikipedia article on First-Class functions, there is a nice table of language support for various aspects of functional programming: http://en.wikipedia.

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-05 01:41

    What you show is an example of higher order functions, functions that take functions as arguments and/or return functions.

    Partial application is something different. Here a Haskell example:

    add     :: Int -> Int -> Int
    add x y = x + y
    
    addOne = add 1
    

    add is a function that takes two Int and returns an Int, denoted as Int -> Int -> Int. If you're unfamiliar with the syntax, in Javascript this would roughly look like this:

    /**
     * @param int x
     * @param int y
     * @return int
     */
    function add(x, y) {
        return x + y;
    }
    

    add 1 calls this function with only one parameter, which returns a new function that takes one Int and returns an Int (Int -> Int). The add function was not explicitly designed to be a higher order function, it was simply partially applied.

提交回复
热议问题