swift排列组合
Programmers come across functions every day. A function represents a special type of relationship: every input value that the function takes is associated with some output value. So in a more generic way, a function is a rule which maps some input values to one output value.
程序员每天都会遇到各种功能。 函数代表一种特殊的关系:函数采用的每个输入值都与某个输出值相关联。 因此,以一种更通用的方式,函数是将某些输入值映射到一个输出值的规则。
The basic idea behind function composition is applying one function to the result of another function. So it is a mathematical concept of combining functions into one function.
函数组合背后的基本思想是将一个函数应用于另一个函数的结果。 因此,这是将函数组合为一个函数的数学概念。
入门 (Getting started)
Let’s discuss it along with the mathematical concept. In the above diagram, “f” and “g” are two functions. We can represent the functions as below:
让我们与数学概念一起讨论它。 在上图中,“ f”和“ g”是两个功能。 我们可以将函数表示如下:
f: A -> Bg: B -> C
If we do composition of these two functions, then we can represent it as “g o f” (you can say g of f).
如果我们将这两个函数进行组合,则可以将其表示为“ gof”(可以说f中的g)。
(g o f): A -> C such that (g o f)(a) = g(f(a)) for all a in A
Let’s try to explore it more with a simple example:
让我们尝试通过一个简单的示例进一步探索它:
Let f(a) = 2a + 3 & g(a) = 3a + 5, then function composition
(g o f)(a) = g(f(a)) = 3(f(a)) + 5 = 3(2a + 3) + 5 = 6a + 14
This concept is not only applicable in mathematics — we can also apply it in programming languages. Those languages are called functional programming languages. Understanding this concept improves your code readability and makes it easier to understand for other programmers.
这个概念不仅适用于数学-我们还可以将其应用在编程语言中。 这些语言称为功能编程语言。 理解此概念可以提高代码的可读性,并使其他程序员更容易理解。
Swift作为一种功能编程语言的介绍 (An introduction to Swift as a functional programming language)
Now, the good news is that swift is also a functional programming language. In Swift programming, a function has the most important role, so you’ll interact with them daily. A Swift function can return a value, and then we can use the returned value as an input into another function. This is a common programming practice.
现在,好消息是Swift也是一种功能编程语言。 在Swift编程中,函数扮演着最重要的角色,因此您将每天与它们进行交互。 Swift函数可以返回一个值,然后我们可以将返回的值用作另一个函数的输入。 这是一种常见的编程习惯。
快速实现功能组合 (Implementing function composition in swift)
Suppose we have an array of integers, and we want the output to be a squared array of unique even integers. So for that normally, we would implement functions like below:
假设我们有一个整数数组,并且我们希望输出是唯一的偶数整数的平方数组。 因此,通常,我们将实现以下功能:
This code gives us the correct output, but as you can see, the code’s readability is not great. Also, the function calling order looks like the opposite from what we’d want, and it might create confusion for some new programmers. This block of code is hard to analyze.
该代码为我们提供了正确的输出,但是如您所见,该代码的可读性不是很好。 同样,函数调用顺序看起来与我们想要的相反,并且可能会使一些新程序员感到困惑。 此代码块很难分析。
So here comes function composition to rescue us from all of the above problems. We can achieve function composition by taking advantage of generics, closure, and the infix operator.
因此,这里出现了功能组合,可以使我们摆脱以上所有问题。 我们可以利用泛型,闭包和infix运算符来实现功能组合。
So let’s look what is happening in the above block of code:
因此,让我们看看上面的代码块中正在发生什么:
- We have declared a custom infix operator “>>>”. It has left associativity and precedence order just like the + operator. 我们已经声明了一个自定义的中缀运算符“ >>>”。 就像+运算符一样,它保留了关联性和优先级顺序。
- We have declared a function whose name is the same as the infix operator’s name. The function uses three generics T, U, V and it takes two closures as input parameters. 我们已经声明了一个函数,其名称与中缀运算符的名称相同。 该函数使用三个泛型T,U,V,并且使用两个闭包作为输入参数。
- The left parameter is a closure, and it takes an input of type T and returns an output of type U. 左参数是一个闭包,它接受类型T的输入并返回类型U的输出。
- The right parameter is also a closure, and it takes an input of type U and returns the output of type V. 正确的参数也是一个闭包,它接受类型U的输入并返回类型V的输出。
- Now, the >>> function returns a function or closure, which has the type of (T) → V. The output closure takes an input of type T and returns the output of type V. Here the output of the left parameter is the input of the right parameter. 现在,>>>函数返回类型为(T)→V的函数或闭包。输出闭包采用类型T的输入并返回类型V的输出。这里,左参数的输出为输入正确的参数。
left : (T) -> U right: (U) -> V
Output Type: (T) -> V
If you understand the mathematical representation of function composition, then you can see that it is exactly the same with Swift’s implementation.
如果您了解函数组合的数学表示形式,那么您会发现它与Swift的实现完全相同。
6. In the function body, it returns the result of the right parameter on the left parameter.
6.在函数主体中,它在left参数上返回right参数的结果。
Now if we want the same result (a squared array of unique even integers), we can do this with function composition.
现在,如果我们想要相同的结果(唯一的偶数整数的平方数组),则可以使用函数组合来实现。
It is a chain of functions which returns the same result. The function order now looks similar to what a human being might think. It has better readability and is easier to understand for everyone.
它是返回相同结果的函数链。 现在,功能顺序看起来类似于人的想法。 它具有更好的可读性,并且每个人都更容易理解。
Thank you for reading!
感谢您的阅读!
翻译自: https://www.freecodecamp.org/news/a-quick-intro-to-function-composition-in-swift-17f5c9999cee/
swift排列组合