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

China☆狼群 提交于 2019-12-03 13:05:42

问题


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 am very new in Swift programming language can anyone please elaborate it with an example.


回答1:


A very simple example to demonstrate this behaviour:

func functionA() {
    println("Hello by functionA")
}

func executeFunction(function: () -> ()) {
    function()
}

executeFunction(functionA)



回答2:


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 ....




回答3:


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

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)




回答4:


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
}


来源:https://stackoverflow.com/questions/26236247/functions-are-a-first-class-type-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!