“Functions are a first-class type” in swift?

后端 未结 5 1288
北海茫月
北海茫月 2021-02-03 13:21

the little knowledge , I have about first class function is that it supports passing functions as arguments and we can also return them as the values in another function ... I a

相关标签:
5条回答
  • 2021-02-03 14:08

    A function that returns a function while capturing a value from the lexical environment:

    enter image description here

    A function of an array of Comparables that returns a function of a test predicate that returns a function of a value that returns a Bool if the value is the extreme of the array under test. (Currying)

    enter image description here

    0 讨论(0)
  • 2021-02-03 14:12

    Properties of First class function

    1. A function is an instance of the Object type.
    2. You can store the function in a variable.
    3. You can pass the function as a parameter to another function.
    4. You can return the function from a function.
    5. You can store them in data structures such as hash tables, lists, …

    refer https://www.geeksforgeeks.org/first-class-functions-python/

    0 讨论(0)
  • 2021-02-03 14:23

    First-Class functions are functions that can return another functions. For instance:

    func operate( operand: String) -> ((Double, Double) -> Double)?{
    
        func add(a: Double, b: Double) -> Double {
            return a + b
        }
    
        func min(a: Double, b: Double) -> Double{
            return a - b
        }
    
        func multi(a: Double, b: Double) -> Double {
            return a * b
        }
    
        func div (a: Double, b: Double) -> Double{
            return a / b
        }
    
        switch operand{
        case "+":
            return add
        case "-":
            return min
        case "*":
            return multi
        case "/":
            return div
        default:
            return nil
        }
    }
    

    The function operate returns a function that takes two double as its arguments and returns one double.

    The usage of this function is:

    var function = operate("+")
    print(" 3 + 4 = \(function!(3,4))")
    
    function = operate("-")
    print(" 3 - 4 = \(function!(3,4))")
    
    function = operate("*")
    print(" 3 * 4 = \(function!(3,4))")
    
    function = operate("/")
    print(" 3 / 4 = \(function!(3,4))")
    

    When you don't care about the implementation of a function, using First-Class functions to return these functions become beneficials. Plus, sometimes, you are not responsible to develop (or not authorised ) of the functions like add, min. So someone would develop a First-Class function to you that returns these functions and it is your responsibility to continue ....

    0 讨论(0)
  • 2021-02-03 14:26

    A very simple example to demonstrate this behaviour:

    func functionA() {
        println("Hello by functionA")
    }
    
    func executeFunction(function: () -> ()) {
        function()
    }
    
    executeFunction(functionA)
    
    0 讨论(0)
  • 2021-02-03 14:26

    Any programming language is said to have first-class-functions, when functions are treated like normal variables. That means a function can be passed as parameter to any other function, can be returned by any function and also can be assigned to any variable.

    i.e., (Referring apple's examples)

    Passing function as parameter

    func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {
        for item in list {
            if condition(item) {
                return true
            }
        }
        return false
    }
    

    Returning function

    func makeIncrementer() -> ((Int) -> Int) {
        func addOne(number: Int) -> Int {
            return 1 + number
        }
        return addOne
    }
    
    0 讨论(0)
提交回复
热议问题