Created
August 20, 2016 18:02
-
-
Save raindev/7c51d1927429c23f1562a4318724fd9d to your computer and use it in GitHub Desktop.
Mapping arrays with closures in Swift
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
converting number 13 to String | |
temporary number is currently 13 | |
current digit 3 | |
current digit as string Three | |
current string value of number Three | |
temporary number is currently 1 | |
current digit 1 | |
current digit as string One | |
current string value of number OneThree | |
converting number 52 to String | |
temporary number is currently 52 | |
current digit 2 | |
current digit as string Two | |
current string value of number Two | |
temporary number is currently 5 | |
current digit 5 | |
current digit as string Five | |
current string value of number FiveTwo | |
converting number 510 to String | |
temporary number is currently 510 | |
current digit 0 | |
current digit as string Zero | |
current string value of number Zero | |
temporary number is currently 51 | |
current digit 1 | |
current digit as string One | |
current string value of number OneZero | |
temporary number is currently 5 | |
current digit 5 | |
current digit as string Five | |
current string value of number FiveOneZero | |
["OneThree", "FiveTwo", "FiveOneZero"] |
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 digitNames = [ | |
0: "Zero", | |
1: "One", | |
2: "Two", | |
3: "Three", | |
4: "Four", | |
5: "Five", | |
] | |
let numbers = [13, 52, 510] | |
// 👆 гадаю, вище все ясно | |
// виконати для кожного числа з списку numbers наступну функцію (кложуру) | |
// типу Int -> String; записати результат до списку strings | |
// перетворюємо тип [Int] у тип [String] | |
let strings = numbers.map({number -> String in | |
print("converting number \(number) to String") | |
// number, параметр, кложури за замовчуванням константа (let) | |
// записуємо його в змінну з таким же ім'ям, щоби мати можливість модифікувати | |
var number = number | |
// результат - рядок, що міститиме число, як String (e.g. "42") | |
// починаємо з пустого рядка | |
var output = "" | |
// виконуємо, поки в числі number не залишиться цифр | |
while number > 0 { | |
print("temporary number is currently \(number)") | |
// остання цифра, остача ділення на 10 | |
let digit = number % 10 | |
print("current digit \(digit)") | |
// отримуємо зі словника ім'я цифри | |
// одразу розпаковуємо (примусово) Optional | |
// (в словнику маємо значення для кожної з цифр, тому це безпечно | |
let digitString = digitNames[digit]! | |
print("current digit as string \(digitString)") | |
// додаємо поточну цифру до початку результуючого рядка | |
output = digitString + output | |
print("current string value of number \(output)") | |
// відкидаємо останню цифру, яку вже зконвертували | |
// після останнього ділення (націло) в number буде 0 | |
number /= 10 | |
} | |
// повертаємо результат для поточного числа зі списку | |
return output | |
}) | |
print(strings) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment