Skip to content

Commit 277d700

Browse files
committed
#17-Swift
1 parent 647dcdd commit 277d700

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import Foundation
2+
3+
// Array para iterar.
4+
var numbers = [1, 2, 3, 4, 5, 5, 7, 8, 9, 10]
5+
6+
7+
// Función recursiva con numeros.
8+
print("\nFunción recursiva con numeros.")
9+
func printNumbers(_ n: Int) {
10+
if n > 0 {
11+
printNumbers(n - 1)
12+
13+
print(n)
14+
}
15+
}
16+
printNumbers(10)
17+
18+
19+
// Función recursiva con array.
20+
print("\nFunción recursiva con array.")
21+
func printArray(_ a: inout [Int]) {
22+
var b: [Int] = a
23+
24+
if !b.isEmpty {
25+
print(b[0])
26+
27+
b.removeFirst()
28+
29+
printArray(&b)
30+
}
31+
}
32+
printArray(&numbers)
33+
34+
35+
// Metodo map, imprime los números del array.
36+
print("\nMetodo map.")
37+
let map: [()] = numbers.map { n in
38+
print(n)
39+
}
40+
41+
42+
// Metodo forEach, imprime los números del array.
43+
print("\nMetodo forEach.")
44+
numbers.forEach { n in
45+
print(n)
46+
}
47+
48+
49+
50+
51+
// DIFICULTAD EXTRA
52+
print("\nDIFICULTAD EXTRA.")
53+
54+
55+
// Clouser que imprime los caracteres de un string.
56+
print("\nClosure imprime caracteres.")
57+
var string = ""
58+
let closurePrint: (String) -> Void = { str in
59+
string = str
60+
if !str.isEmpty {
61+
print(str[string.startIndex])
62+
string.removeFirst()
63+
closurePrint(string)
64+
65+
}
66+
}
67+
closurePrint("Hello")
68+
69+
70+
// Metodo filter, imprime los números del array que sean multiplos de 2.
71+
print("\nMetodo filter")
72+
let filter = numbers.filter { n in
73+
if n % 2 == 0 {
74+
print(n)
75+
}
76+
return true
77+
}
78+
79+
80+
81+
// Metodo reduce, sume 10 a cada uno de los números del array y los imprime.
82+
print("\nMetodo reduce.")
83+
let reduce = numbers.reduce(into: 10) { n1, n2 in
84+
print(n1 + n2)
85+
}
86+
87+
88+
// Metodo sequencee, inicia una secuencia en 10 y le eleva al cubo tantas veces como sea indicado.
89+
print("\nMetodo sequence.")
90+
let seq = sequence(first: 10) { n in
91+
pow(n, 3)
92+
}
93+
// Se eleva 4 veces al cubo el numero 10 e imprime los números.
94+
let firstFour: [()] = Array(seq.prefix(4)).map { n in
95+
print(n)
96+
}
97+
98+
99+
// Metodo sort, ordena de mayor a menor e imprime los numeros.
100+
print("\nMetodo sort.")
101+
numbers.sort { n1, n2 in
102+
n1 > n2
103+
}
104+
numbers.forEach { n in
105+
print(n)
106+
}
107+

0 commit comments

Comments
 (0)