O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A composed function with custom operator

Let's define a new custom operator to use instead of our composed function:

precedencegroup AssociativityLeft {     associativity: left } infix operator |> : AssociativityLeft func |> <T, V>(f: @escaping (T) -> V, g: @escaping (V) -> V ) -> (T) -> V {     return { x in g(f(x)) } } let composedWithCustomOperator = extractElements |> formatWithCurrency composedWithCustomOperator("10,20,40,30,80,60") // The result will be: ["10$", "20$", "40$", "30$", "80$", "60$"] 

In this example, we have defined a new operator, |>, that takes two generic functions and combines them, returning a function that has the first function's input as the parameter and the second function's return as the return type.

As this new ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required