Created
September 22, 2021 19:42
-
-
Save yakushevichsv/a224b24378a1b4abcb72d015a609b9a7 to your computer and use it in GitHub Desktop.
Simple sample of using Map & FlatMap On Optionals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str: String? | |
let result0 = str.flatMap { Int($0) }.valueOrDefault(.min) | |
var result1: Int = .min | |
if let str = str { | |
result1 = Int(str) ?? .min | |
} | |
// result1 can be changed here, but result0 can't. | |
assert(result0 == result1) | |
//Similar can be done using "map" method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment