Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tuplized if let
//
// TupleAssignment.swift
// Hello Swift
//
// Created by Erica Sadun on 12/19/14.
// Copyright (c) 2014 Erica Sadun. All rights reserved.
//
import Foundation
// Thanks, Lily Ballard
extension Optional {
func tupled<A>(a:A?) -> (T, A)? {
switch (self, a) {
case let (.Some(tt), .Some(aa)):
return (tt, aa)
default:
return nil
}
}
}
func tupled<A, B>(a:A?, b:B?)->(A, B)? {
return a.tupled(b)
}
func tupled<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 tupled<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 tupled<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 tupled<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 tupled<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 tupled<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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.