Skip to content

Commit

Permalink
Enough examples for now
Browse files Browse the repository at this point in the history
  • Loading branch information
yole committed Jul 4, 2017
1 parent 0d59d2b commit c2cec22
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/kotlin/t20_Apply/bad.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package t20_Apply.bad

import java.awt.Color
import javax.swing.JLabel

fun createLabel(): JLabel {
val label = JLabel("Foo")
label.foreground = Color.RED
label.background = Color.BLUE
return label
}
9 changes: 9 additions & 0 deletions src/main/kotlin/t20_Apply/good.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package t20_Apply.good

import java.awt.Color
import javax.swing.JLabel

fun createLabel() = JLabel("Foo").apply {
foreground = Color.RED
background = Color.BLUE
}
4 changes: 4 additions & 0 deletions src/main/kotlin/t21_FilterIsInstance/bad.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package t21_FilterIsInstance.bad

fun findAllStrings(objects: List<Any>) =
objects.filter { it is String }
4 changes: 4 additions & 0 deletions src/main/kotlin/t21_FilterIsInstance/good.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package t21_FilterIsInstance.good

fun findAllStrings(objects: List<Any>) =
objects.filterIsInstance<String>()
9 changes: 9 additions & 0 deletions src/main/kotlin/t22_MapNotNull/bad.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package t22_MapNotNull.bad

data class Result(val data: Any?,
val errorMessage: String?)

fun listAllErrorMessages(errors: List<Result>): List<String> =
errors.map { it.errorMessage }.filterNotNull()


7 changes: 7 additions & 0 deletions src/main/kotlin/t22_MapNotNull/good.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package t22_MapNotNull.good

data class Result(val data: Any?,
val errorMessage: String?)

fun listAllErrorMessages(errors: List<Result>): List<String> =
errors.mapNotNull { it.errorMessage }
12 changes: 12 additions & 0 deletions src/main/kotlin/t23_CompareBy/bad.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package t23_CompareBy.bad

class Person(val name: String, val age: Int)

fun sortPersons(persons: List<Person>) =
persons.sortedWith(Comparator<Person> { person1, person2 ->
val rc = person1.name.compareTo(person2.name)
if (rc != 0)
rc
else
person1.age - person2.age
})
6 changes: 6 additions & 0 deletions src/main/kotlin/t23_CompareBy/good.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package t23_CompareBy.good

class Person(val name: String, val age: Int)

fun sortPersons(persons: List<Person>) =
persons.sortedWith(compareBy(Person::name, Person::age))
7 changes: 7 additions & 0 deletions src/main/kotlin/t24_Zip/bad.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package t24_Zip.bad

fun calculateRequestDurations(requestTimestamps: LongArray,
responseTimestamps: LongArray) =
requestTimestamps.mapIndexed { index, req ->
responseTimestamps[index] - req
}
5 changes: 5 additions & 0 deletions src/main/kotlin/t24_Zip/good.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package t24_Zip.good

fun calculateRequestDurations(requestTimestamps: LongArray,
responseTimestamps: LongArray) =
(requestTimestamps zip responseTimestamps).map { (req, res) -> res - req }
11 changes: 11 additions & 0 deletions src/main/kotlin/t25_GroupBy/bad.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package t25_GroupBy.bad

class Request(val url: String, val remoteIP: String, val timestamp: Long)

fun analyzeRequests(log: List<Request>) {
val map = mutableMapOf<String, MutableList<Request>>()
for (request in log) {
map.getOrPut(request.url) { mutableListOf() }
.add(request)
}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/t25_GroupBy/good.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package t25_GroupBy.good

class Request(val url: String, val remoteIP: String, val timestamp: Long)

fun analyzeRequests(log: List<Request>) {
val map = log.groupBy(Request::url)
}
14 changes: 14 additions & 0 deletions src/main/kotlin/t26_SubstringBeforeLast/bad.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package t26_SubstringBeforeLast.bad

data class PathComponents(val directory: String,
val pathName: String)

val pattern = Regex("(.+)/([^/]*)")

fun splitPath(path: String): PathComponents {
val match = pattern.matchEntire(path)
?: return PathComponents("", path)

return PathComponents(match.groupValues[1],
match.groupValues[2])
}
9 changes: 9 additions & 0 deletions src/main/kotlin/t26_SubstringBeforeLast/good.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package t26_SubstringBeforeLast.good

data class PathComponents(val directory: String,
val pathName: String)

fun splitPath(path: String): PathComponents {
return PathComponents(path.substringBeforeLast('/', ""),
path.substringAfterLast('/'))
}

0 comments on commit c2cec22

Please sign in to comment.