swift高阶函数全排列_Swift中的高阶函数

swift高阶函数全排列

As a swift developer, you must know about higher-order functions. It will speed up your software development skills.

作为一名Swift的开发人员,您必须了解高阶函数。 它将加快您的软件开发技能。

A higher-order function is a function that takes one or more functions as arguments or returns a function as its result. Here are some swift higher-order functions — forEach, map, CompactMap, flatMap, filter, reduce, sort, and sorted.

高阶函数是将一个或多个函数作为参数或返回一个函数作为其结果的函数。 这是一些快速的高阶函数-forEach,map,CompactMap,flatMap,filter,reduce,sort和sorted。

In this article, you will explore the implementation of mostly all types of higher-order functions of swift.

在本文中,您将探索swift几乎所有类型的高阶函数的实现。

Let’s start -

开始吧 -

每次 (ForEach)

forEach will iterate through all elements in an array and will not return anything.

forEach将遍历数组中的所有元素,并且将不返回任何内容。

forEach Example
每个例子

If you run the above code then the output will look like this —

如果运行上面的代码,则输出将如下所示:

Image for post
forEach forEach的输出

In the above code, in step 1 you are creating an array with name coins.

在上面的代码中,在步骤1中,您将创建一个带有名称硬币的数组。

In step 2, you are iterating through all coins and adding $ to every element and prints it.

在步骤2中,您要遍历所有硬币并将$添加到每个元素并进行打印。

In step 3, you are using forEach for iterating through all coins and adding $ to every element and prints it.

在第3步中,您将使用forEach遍历所有硬币并将$添加到每个元素并进行打印。

In step 4, you are using a short syntax of forEach.

在步骤4中,您使用的是forEach的简短语法

“forEach” works like “for in” but the basic difference is, you can’t use break and continue statement to exit from the closure for forEach.

“ forEach”的工作方式类似于“ for in”,但是基本的区别是,您不能使用break and continue语句从forEach的闭包中退出。

地图 (map)

map will iterate through all elements in an array and will return an updated array.

map将遍历数组中的所有元素,并返回更新后的数组。

map Example
地图示例

If you run the above code then the output will look like this —

如果运行上面的代码,则输出将如下所示:

Image for post
output of mapExample
mapExample的输出

In the above code, in step 1 you are creating an array with name coins.

在上面的代码中,在步骤1中,您将创建一个带有名称硬币的数组。

In step 2, you are iterating through all coins, adding $ to every coin, and appending that element into the new array.

在步骤2中,您要遍历所有硬币,将$添加到每个硬币,然后将该元素附加到新数组中。

In step 3, you are using map for iterating through all coins, adding $ to every coin, and returning new array.

在第3步中,您将使用map遍历所有硬币,将$添加到每个硬币,并返回新的数组。

In step 4, you are using a short syntax of map.

在第4步中,您将使用map的简短语法

compactMap (compactMap)

compactMap will iterate through all elements in an array and will return an updated array only with the elements which satisfied the condition written inside the body of compactMap. Any element which resulting in a nil value will be excluded from the updated array.

compactMap将遍历数组中的所有元素,并且仅使用满足在compactMap主体内编写的条件的元素,才会返回更新后的数组。 任何导致nil值的元素都将从更新后的数组中排除。

compactMap Example
compactMap示例

If you run the above code then the output will look like this —

如果运行上面的代码,则输出将如下所示:

Image for post
compactMapExample output
compactMapExample输出

In the above code, in step 1 you are creating an array with name coins.

在上面的代码中,在步骤1中,您将创建一个带有名称硬币的数组。

In step 2, you are iterating through all coins, filtering the coin which holds int value, and appending to the new array.

在第2步中,您将遍历所有硬币,过滤具有int值的硬币,然后追加到新数组。

In step 3, you are using compactMap for iterating through all coins, filtering the coin which holds int value, and returning the new array.

在第3步中,您将使用compactMap遍历所有硬币,过滤具有int值的硬币并返回新数组。

In step 4, you are using a short syntax of compactMap.

在第4步中,您将使用CompactMap的简短语法

flatMap (flatMap)

flatMap converts 2D array to one dimensional array.

flatMap将2D数组转换为一维数组。

flatMap Example
flatMap示例

In the above code, in step 1 you are creating an 2D array with name arrayOfCoins.

在上面的代码中,在步骤1中,您将创建一个名称为arrayOfCoins的2D数组。

In step 2, you are using flatMap for combining rows to covert it into a one-dimensional array.

在第2步中,您将使用flatMap合并行以将其隐式转换为一维数组。

In step 3, you are using a short syntax of flapMap.

在第3步中,您使用的是fliftMap的简短语法

过滤 (filter)

filter will iterate through all elements in an array and will return an updated array only with the elements which satisfied the condition written inside the body of filter.

filter将遍历数组中的所有元素,并且仅使用满足满足条件的元素返回更新后的数组。

filter Example
过滤器示例

If you run the above code then the output will look like this —

如果运行上面的代码,则输出将如下所示:

Image for post
filterExample output
filterExample输出

In the above code, in step 1 you are creating an array with name coins.

在上面的代码中,在步骤1中,您将创建一个带有名称硬币的数组。

In step 2, you are iterating through all coins, filtering the coin which has value less than six.

在步骤2中,您要遍历所有硬币,过滤价值小于六的硬币。

In step 3, you are using a short syntax of filter.

在步骤3中,您将使用filter的简短语法

减少 (reduce)

reduce will iterate through all elements in an array and returns an object with a combined value of all elements.

reduce将迭代数组中的所有元素,并返回一个包含所有元素组合值的对象。

syntax -

句法 -

reduce(initialResult, (currentResult, currentElement) -> Return finalResult)

reduce( initialResult ,( currentResultcurrentElement )->返回finalResult )

reduce Example
减少例子

If you run the above code then the output will look like this —

如果运行上面的代码,则输出将如下所示:

Image for post
reduceExample output
reduceExample输出

In the above code, in step 1 you are creating an array with name coins.

在上面的代码中,在步骤1中,您将创建一个带有名称硬币的数组。

In step 2, you are setting initial value as 0, iterating through all coins and adding currentResult(result) to currentElement(coin) in the body of closure, and iterating it.

在第2步中,您将初始值设置为0,遍历所有硬币,并将currentResult(result)添加到闭包主体中的currentElement(coin),然后对其进行迭代。

In step 3, you are using a short syntax of reduce.

在第3步中,您使用简短的reduce语法

sort(by :)和sorted(by :) (sort(by:) and sorted(by:))

sort(by:) will sort all elements according to the condition written inside the body of the closure.

sort(by :)将根据闭包内部编写的条件对所有元素进行排序。

sorted(by:) will sort all elements according to the condition written inside the body of the closure and returns a new array.

sorted(by :)将根据闭包内部编写的条件对所有元素进行排序,并返回一个新数组。

sort(by:) and sorted(by:) Example
sort(by :)和sorted(by :)示例

In the above code, in step 1 you are creating an array with name coins.

在上面的代码中,在步骤1中,您将创建一个带有名称硬币的数组。

In step 2, you are sorting an array in decreasing order and creating an array with name sortCoins.

在第2步中,您将以降序对数组进行排序,并创建一个名称为sortCoins的数组。

In step 3, you are sorting the same array in decreasing order.

在步骤3中,您将以降序对同一数组进行排序。

Here is the short syntax for sort(by:) and sorted(by:)

这是sort(by :)和sorted(by :)的简短语法

short syntax for sort(by:) and sorted(by:)
sort(by :)和sorted(by :)的简短语法

Thank you for reading this article. If you found this article helpful, please don’t forget to clap.

感谢您阅读本文。 如果您发现这篇文章对您有所帮助,请别忘了鼓掌。

If you have any questions or comments, feel free to reach out to me on Twitter.

如果您有任何疑问或意见,请随时在Twitter上与我联系。

翻译自: https://levelup.gitconnected.com/higher-order-functions-in-swift-35861620ad1

swift高阶函数全排列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值