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

6

9

The little knowledge I have about first-class functions 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?

V answered 7/10, 2014 at 12:40 Comment(1)
You'll want to read the Function Types section in Apple's Swift book.Basically
G
13

A very simple example to demonstrate this behaviour:

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

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

executeFunction(functionA)
Gorilla answered 7/10, 2014 at 12:48 Comment(1)
This example illustrated one of the properties of "First class function" .. Property is "Pass the function as a parameter to another function"Usual
L
5

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

Lvov answered 11/9, 2015 at 16:25 Comment(1)
Thanks for Detailed ExplanationSignalize
U
1

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

Une answered 8/10, 2014 at 1:34 Comment(0)
I
1

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
}
Ipsambul answered 26/7, 2018 at 18:48 Comment(0)
Z
1

Concept of first-class is generic one which applies to many languages. What it means is that something can be classified as first class if it :

  • Can be passed into functions
  • Can be returned from functions as return type
  • Can be assigned to variables
  • Can be tested for equality

Functions for example are first class in languages like Python, JavaScript and Swift. Along with functions, Swift also has other first class types which include

  • Actors
  • Classes
  • Structures
  • Functions
  • Closures
  • Enumerations
  • Tuples
  • Protocols
Zygophyte answered 18/5 at 3:26 Comment(0)
U
0

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/

Usual answered 25/7, 2020 at 1:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.