Last active
September 28, 2016 08:14
-
-
Save ken0nek/3b5bff7a057855a5f3ee to your computer and use it in GitHub Desktop.
【Swift】2つの配列を交互につなぎ合わせる ref: http://qiita.com/ken0nek/items/55e9ac0be32d35fbaa67
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
let num = 1234 | |
// Int -> [String] | |
let numList = String(num).characters.flatMap { String($0) } | |
print(numList) | |
// ["1", "2", "3", "4"] | |
let opeList = ["+", "-", "*"] | |
let zipped = zip(numList, opeList + [""]) // 要素数を合わせる | |
print(zipped) | |
// Zip2Sequence<Array<String>, Array<String>>(_sequence1: ["1", "2", "3", "4"], _sequence2: ["+", "-", "*", ""]) | |
let equation = zipped.reduce("") { $0 + $1.0 + $1.1 } | |
print(equation) | |
// "1+2-3*4" |
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
let num = 1234 | |
// Int -> [String] | |
let numList = String(num).characters.flatMap { String($0) } | |
print(numList) | |
// ["1", "2", "3", "4"] | |
let opeList = ["+", "-", "*"] | |
let zipped = zip(numList, opeList + [""]) | |
print(zipped) | |
// Zip2Sequence<Array<String>, Array<String>>(_sequence1: ["1", "2", "3", "4"], _sequence2: ["+", "-", "*", ""]) | |
let concatenated = zipped.map { $0.0 + $0.1 } | |
print(concatenated) | |
// ["1+", "2-", "3*", "4"] | |
let equation = concatenated.joined() | |
print(equation) | |
// "1+2-3*4" |
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
let num = 1234 | |
// Int -> [String] | |
let numList = String(num).characters.flatMap { String($0) } | |
print(numList) | |
// ["1", "2", "3", "4"] | |
let opeList = ["+", "-", "*"] | |
let zipped = zip(numList, opeList + [""]) // 要素数を合わせる | |
print(zipped) | |
// Zip2Sequence<Array<String>, Array<String>>(_sequences: (["1", "2", "3", "4"], ["+", "-", "*", ""])) | |
let equation = zipped.reduce("") { $0 + $1.0 + $1.1 } | |
print(equation) | |
// "1+2-3*4" |
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
let num = 1234 | |
// Int -> [String] | |
let numList = String(num).characters.flatMap { String($0) } | |
print(numList) | |
// ["1", "2", "3", "4"] | |
let opeList = ["+", "-", "*"] | |
let zipped = zip(numList, opeList + [""]) | |
print(zipped) | |
// Zip2Sequence<Array<String>, Array<String>>(_sequences: (["1", "2", "3", "4"], ["+", "-", "*", ""])) | |
let concatenated = zipped.map { $0.0 + $0.1 } | |
print(concatenated) | |
// ["1+", "2-", "3*", "4"] | |
let equation = "".join(concatenated) | |
print(equation) | |
// "1+2-3*4" |
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
let num = 1234 | |
// Int -> [String] | |
let numList = String(num).characters.flatMap { String($0) } | |
print(numList) | |
// ["1", "2", "3", "4"] | |
let opeList = ["+", "-", "*"] | |
let zipped = zip(numList, opeList + [""]) | |
print(zipped) | |
// Zip2Sequence<Array<String>, Array<String>>(_sequences: (["1", "2", "3", "4"], ["+", "-", "*", ""])) | |
let concatenated = zipped.map { $0.0 + $0.1 } | |
print(concatenated) | |
// ["1+", "2-", "3*", "4"] | |
let equation = "".join(concatenated) | |
print(equation) | |
// "1+2-3*4" |
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
let num = 1234 | |
// Int -> [String] | |
let numList = String(num).characters.flatMap { String($0) } | |
print(numList) | |
// ["1", "2", "3", "4"] | |
let opeList = ["+", "-", "*"] | |
let zipped = zip(numList, opeList + [""]) // 要素数を合わせる | |
print(zipped) | |
// Zip2Sequence<Array<String>, Array<String>>(_sequences: (["1", "2", "3", "4"], ["+", "-", "*", ""])) | |
let equation = zipped.reduce("") { $0 + $1.0 + $1.1 } | |
print(equation) | |
// "1+2-3*4" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment