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

Multiple optional value mapping

Our previous example demonstrated single optional value mapping using FP techniques. What if we need to map multiple optional values together? In the optional binding section, we covered a non-functional way to handle multiple optional value binding, and in this section, we will look at multiple optional value mapping. As Optionals are high-kinded types, we will develop an apply function to use over Optionals:

func apply<T, V>(transform: ((T) -> V)?, input: T?) -> V? {     switch transform {     case .some(let fx): return fx <^> input     case .none: return .none     } } 

The apply function is very similar to the fmap function. The transform function is optional, and we use pattern matching to return none or some over ...

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