Skip to content

Instantly share code, notes, and snippets.

@chrisbrandow
Last active March 29, 2017 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbrandow/7eb91366075f33fb8ec01bacb11454bb to your computer and use it in GitHub Desktop.
Save chrisbrandow/7eb91366075f33fb8ec01bacb11454bb to your computer and use it in GitHub Desktop.
infix operator ??? : TernaryPrecedence
infix operator ||| : AdditionPrecedence
func ???(input: String?, valuesBlock: ((String?) -> String)) -> String {
return valuesBlock(input)
}
func |||(ifNotNil: String, ifNil: String) -> ((String?) -> String) {
return { (input: String?) -> String in
if let _ = input {
return ifNotNil
} else {
return ifNil
}
}
}
var anotherOptionalValue: String? = nil
let notNil = anotherOptionalValue ??? valueIfNotNil ||| valueIfNil
print("notNil: \(notNil)")
anotherOptionalValue = "string"
let yeahItsNil = anotherOptionalValue ??? valueIfNotNil ||| valueIfNil
print("yeahItsNil: \(yeahItsNil)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment