Created
July 13, 2016 12:51
-
-
Save stripe-q/43bb92ab142454fd4728229bc0ffea4a to your computer and use it in GitHub Desktop.
Array의 원소들을 개별 변수로 분해하는 함수.
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
enum DesctructError: ErrorProtocol { | |
case outOfIndex(String) | |
} | |
func desctruct<A> (array: [A]) throws -> (A, A) { | |
guard array.count > 1 else { | |
throw DesctructError.outOfIndex("the array doesn't have enough elements.") | |
} | |
return (array[0], array[1]) | |
} | |
func desctruct<A> (array: [A]) throws -> (A, A, A) { | |
guard array.count > 2 else { | |
throw DesctructError.outOfIndex("the array doesn't have enough elements.") | |
} | |
return (array[0], array[1], array[2]) | |
} | |
func desctruct<A> (array: [A]) throws -> (A, A, A, A) { | |
guard array.count > 2 else { | |
throw DesctructError.outOfIndex("the array doesn't have enough elements.") | |
} | |
return (array[0], array[1], array[2], array[3]) | |
} | |
let str = "1 2 3 4" | |
let (x, y, z) = try! desctruct(array:str.characters.split(separator:" ").map{ Int(String($0))! }) | |
print(x, y, z) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment