Skip to content

Instantly share code, notes, and snippets.

@ken0nek
Last active September 28, 2016 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ken0nek/3b5bff7a057855a5f3ee to your computer and use it in GitHub Desktop.
Save ken0nek/3b5bff7a057855a5f3ee to your computer and use it in GitHub Desktop.
【Swift】2つの配列を交互につなぎ合わせる ref: http://qiita.com/ken0nek/items/55e9ac0be32d35fbaa67
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"
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"
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"
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"
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"
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