Last active
August 29, 2015 14:14
-
-
Save boolkybear/cb54587d4784caa0744d to your computer and use it in GitHub Desktop.
Avoid if let ... pyramid of doom
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
func ifNotNil<T>(params: [T?], process: ([T])->Void) | |
{ | |
let nilParams = params.filter { $0 == nil } | |
if countElements(nilParams) == 0 | |
{ | |
process(params.map { $0! }) | |
} | |
} | |
func ifNotNil<T>(process: ([T])->Void, params:T?...) | |
{ | |
ifNotNil(params.map { $0 }, process) | |
} | |
func ifNotNil<T, U>(a: T?, b: U?, process: (T, U)->Void) | |
{ | |
switch (a, b) | |
{ | |
case (.Some(let a), .Some(let b)): | |
process(a, b) | |
default: | |
break | |
} | |
} | |
func ifNotNil<T, U, V>(a: T?, b: U?, c: V?, process: (T, U, V)->Void) | |
{ | |
ifNotNil(a, b) { | |
a, b in | |
if let c = c | |
{ | |
process(a, b, c) | |
} | |
} | |
} | |
func ifNotNil<T, U, V, W>(a: T?, b: U?, c: V?, d: W?, process: (T, U, V, W)->Void) | |
{ | |
ifNotNil(a, b, c) { | |
a, b, c in | |
if let d = d | |
{ | |
process(a, b, c, d) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment