Last active
August 5, 2024 07:09
-
-
Save natecook1000/a69bc9c4c64569f86328 to your computer and use it in GitHub Desktop.
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
protocol StringType { | |
var isEmpty: Bool { get } | |
} | |
extension String : StringType { } | |
extension Optional where Wrapped: StringType { | |
var isNullOrEmpty: Bool { | |
return self?.isEmpty ?? true | |
} | |
} | |
extension Optional where Wrapped: CollectionType { | |
var isNullOrEmpty: Bool { | |
return self?.isEmpty ?? true | |
} | |
} | |
// Strings: | |
let strs: [String?] = [nil, "", "Hello!"] | |
for s in strs { | |
if s.isNullOrEmpty { | |
print("null or empty string: \(s)") | |
} | |
} | |
// Collections: | |
let colls: [[Int]?] = [nil, [], [1, 2, 3]] | |
for c in colls { | |
if c.isNullOrEmpty { | |
print("null or empty collection: \(c)") | |
} | |
} | |
let filtered = colls.map({ $0?.lazy.filter({ $0 > 3 }) }) | |
for c in filtered { | |
if c.isNullOrEmpty { | |
print("null or empty filtered collection: \(c)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment