Skip to content

Instantly share code, notes, and snippets.

@erica
Last active December 11, 2018 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erica/0342d7d1ee347774c990 to your computer and use it in GitHub Desktop.
Save erica/0342d7d1ee347774c990 to your computer and use it in GitHub Desktop.
// Thanks, Lily Ballard
extension Optional {
func and<U>(x:U?) -> (T, U)? {
switch (self, x) {
case let (.Some(first), .Some(second)):
return (first, second)
default:
return nil
}
}
}
func and<A, B>(a:A?, b:B?)->(A, B)? {
return a.and(b)
}
func and<A, B, C>(a:A?, b:B?, c:C?)->(A, B, C)? {
switch (a, b, c) {
case let (.Some(aa), .Some(bb), .Some(cc)):
return (aa, bb, cc)
default:
return nil
}
}
func and<A, B, C, D>(a:A?, b:B?, c:C?, d:D?)->(A, B, C, D)? {
switch (a, b, c, d) {
case let (.Some(aa), .Some(bb), .Some(cc), .Some(dd)):
return (aa, bb, cc, dd)
default:
return nil
}
}
func and<A, B, C, D, E>(a:A?, b:B?, c:C?, d:D?, e:E?)->(A, B, C, D, E)? {
switch (a, b, c, d, e) {
case let (.Some(aa), .Some(bb), .Some(cc), .Some(dd), .Some(ee)):
return (aa, bb, cc, dd, ee)
default:
return nil
}
}
func and<A, B, C, D, E, F>(a:A?, b:B?, c:C?, d:D?, e:E?, f:F?)->(A, B, C, D, E, F)? {
switch (a, b, c, d, e, f) {
case let (.Some(aa), .Some(bb), .Some(cc), .Some(dd), .Some(ee), .Some(ff)):
return (aa, bb, cc, dd, ee, ff)
default:
return nil
}
}
func and<A, B, C, D, E, F, G>(a:A?, b:B?, c:C?, d:D?, e:E?, f:F?, g:G?)->(A, B, C, D, E, F, G)? {
switch (a, b, c, d, e, f, g) {
case let (.Some(aa), .Some(bb), .Some(cc), .Some(dd), .Some(ee), .Some(ff), .Some(gg)):
return (aa, bb, cc, dd, ee, ff, gg)
default:
return nil
}
}
func and<A, B, C, D, E, F, G, H>(a:A?, b:B?, c:C?, d:D?, e:E?, f:F?, g:G?, h:H?)->(A, B, C, D, E, F, G, H)? {
switch (a, b, c, d, e, f, g, h) {
case let (.Some(aa), .Some(bb), .Some(cc), .Some(dd), .Some(ee), .Some(ff), .Some(gg), .Some(hh)):
return (aa, bb, cc, dd, ee, ff, gg, hh)
default:
return nil
}
}
var x : Int?
var y : Int?
x = 5
y = 60
if let (a, b) = x.and(y) {
println("\(a), \(b)")
} else {
println("nope")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment