Skip to content

Commit eb05a29

Browse files
committed
Primeiros Passos em Kotlin
Primeiros Passos em Kotlin
1 parent edfabf4 commit eb05a29

File tree

8 files changed

+128
-1
lines changed

8 files changed

+128
-1
lines changed
Binary file not shown.

.gradle/7.2/fileHashes/fileHashes.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.* // required import
2+
3+
fun randomDay() : String {
4+
val week = arrayOf ("Monday", "Tuesday", "Wednesday", "Thursday",
5+
"Friday", "Saturday", "Sunday")
6+
return week[Random().nextInt(week.size)]
7+
}
8+
9+
fun fishFood(day : String) : String {
10+
return when (day) {
11+
"Monday" -> "flakes"
12+
"Wednesday" -> "redworms"
13+
"Thursday" -> "granules"
14+
"Friday" -> "mosquitoes"
15+
"Sunday" -> "plankton"
16+
else -> "nothing"
17+
}
18+
}
19+
20+
fun feedTheFish() {
21+
val day = randomDay()
22+
val food = fishFood(day)
23+
println ("Today is $day and the fish eat $food")
24+
}
25+
26+
fun main() {
27+
feedTheFish()
28+
}
29+
30+
fun swin(speed: String = "fast"){
31+
println("swimning $speed")
32+
}
33+
//swim() // uses default speed
34+
//swim("slow") // positional argument
35+
//swim(speed="turtle-like") // named parameter
36+
37+
fun shouldChangeWater (day: String, temperature: Int = 22, dirty: Int = 20): Boolean {
38+
return when {
39+
temperature > 30 -> true
40+
dirty > 30 -> true
41+
day == "Sunday" -> true
42+
else -> false
43+
}
44+
}
45+
46+
fun feedTheFish2() {
47+
val day = randomDay()
48+
val food = fishFood(day)
49+
println ("Today is $day and the fish eat $food")
50+
println("Change water: ${shouldChangeWater(day)}")
51+
}
52+
53+
fun isTooHot(temperature: Int) = temperature > 30
54+
55+
fun isDirty(dirty: Int) = dirty > 30
56+
57+
fun isSunday(day: String) = day == "Sunday"
58+
59+
fun shouldChangeWater2 (day: String, temperature: Int = 22, dirty: Int = 20): Boolean {
60+
return when {
61+
isTooHot(temperature) -> true
62+
isDirty(dirty) -> true
63+
isSunday(day) -> true
64+
else -> false
65+
}
66+
println("shouldChangeWater2")
67+
}

bin/main/PrimeiroProjeto/Hello.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package PrimeiroProjeto
2+
3+
fun main() {
4+
val decorations = listOf("rock", "pagoda", "plastic plant", "alligator", "flowerpot")
5+
6+
// eager, cria uma nova lista
7+
val eager = decorations.filter { it[0] == 'p' }
8+
println("eager: $eager")
9+
10+
11+
// lazy, vai esperar até ser solicitado a avaliar
12+
val filtered = decorations.asSequence().filter { it[0] == 'p' }
13+
println("filtered: $filtered")
14+
15+
val newList = filtered.toList()
16+
println("new list: $newList")
17+
18+
val lazyMap = decorations.asSequence().map {
19+
println("aceess: $it")
20+
it
21+
}
22+
println("lazy: $lazyMap")
23+
println("-----")
24+
println("first: ${lazyMap.first()}")
25+
println("-----")
26+
println("all: ${lazyMap.toList()}")
27+
28+
29+
val lazyMap2 = decorations.asSequence().filter { it[0] == 'p' }.map {
30+
println("access: $it")
31+
it
32+
}
33+
println("_____")
34+
println("filtered: ${lazyMap2.toList()}")
35+
36+
val dirtyLevel = 20
37+
val waterFilter = {dirty: Int -> dirty /2}
38+
println(waterFilter(dirtyLevel))
39+
40+
41+
//O corpo do código chama a função que foi passada como o segundo argumento e passa o primeiro argumento para ela
42+
fun updateDirty(dirty: Int, operation: (Int) -> Int ): Int{
43+
return operation(dirty)
44+
}
45+
println(updateDirty(30, waterFilter))
46+
47+
fun increaseDirty( start: Int) = start + 1
48+
println(updateDirty(15, ::increaseDirty))
49+
50+
var dirtyLevel = 19;
51+
dirtyLevel = updateDirty(dirtyLevel) { dirtyLevel -> dirtyLevel + 23}
52+
println(dirtyLevel)
53+
}

src/main/kotlin/PrimeiroProjeto/Hello.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ fun main() {
2323
println("-----")
2424
println("first: ${lazyMap.first()}")
2525
println("-----")
26-
println("all: ${lazyMap.toList()}")
26+
println("all: ${lazyMap.toList()}"
27+
)
2728

2829

2930
val lazyMap2 = decorations.asSequence().filter { it[0] == 'p' }.map {

0 commit comments

Comments
 (0)