Functional programming of Swift (IV)

Source: Internet
Author: User

The article is from "functional programing in Swift", please refer to the book for details.

Map, Filter, Reduce

Functions that take Functions as arguments is sometimes called Higher-order Functions.

Higher-order function (higher-order function) means that functions can be used as arguments to another function.

In this chapter, we will introduce some of the high-level functions related to arrays in the SWIFT Standard library, first introduce some of the more common ones and then describe how to organize these generic operations into a more complex operation.

Introducing generics (generic type)

Function: Returns the array after adding 1 to each element of the original array
Func Incrementarray (xs: [int]), [int] { var result: [int] = [] for x in xs { result.append (x + 1) } return result}

Function: Returns an array that is multiplied by 2 for each element of the original array
Func doubleArray1 (xs: [int]), [int] { var result: [int] = [] for x in xs { result.append (x * 2) } return result}

Do you find that there are many similar codes in these two methods that can abstract these different places and write them as a single, more general function? As shown below:

Func Computeintarray (xs: [int]), [int] {     var result: [int] = [] for    x in xs {        result.append (/* Something Using X */)}    return result}    

To complete the above definition, we will need a new parameter to abstract out these different places:

Func Computeintarray (xs: [int], f:int-i, int), [int] {     var result: [int] = [] for    x in xs {        result . append (f (x))     }    return result}    

Then the previous code will become the following:

Func doubleArray2 (xs: [int]), [int] {     return Computeintarray (XS) {x in X * 2}}

Note that we used the trailing closure (trailing closures) syntax when calling Computeintarray

But this computeintarray is not very flexible, if we support the calculation of an array of elements is not even:

Func Isevenarray (xs: [Int]), [Bool] {     Computeintarray (xs) {x in x% 2 = = 0}}

Unfortunately, this code will report a type error. So how do we fix it? Maybe we'll do this:

Func Computeboolarray (xs: [Int], F:int, BOOL), [bool] {Let     result: [bool] = [] for    x in xs {        Resul T.append (f (x))     }    return result}

But this is still not good, if we need to calculate a string? Are we going to define another high-order function with a parameter type of int->string?

We'll say "NO"

Fortunately, there is a workaround: We can use generics (generics):

Func genericcomputearray<u> (xs: [Int], F:int-u), [u] {     var result: [u] = [] for    x in xs {        re Sult.append (f (x))     }    return result}

But you'll find our side still has a [INT],SO:

Func map<t, U> (xs: [T], F:t-u), [u] {     var result: [u] = [] for    x in xs {        result.append (f (x))     }    return result}

Let's transform one of the above functions:

Func computeintarray<t> (xs: [Int], F:int-T), [t] {     return map (XS, f)}

Functional programming of Swift (IV)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.