forked from yole/idiomatic-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
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
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 | ||
} |
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
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 | ||
} |
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
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 } |
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
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>() |
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
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() | ||
|
||
|
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
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 } |
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
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 | ||
}) |
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
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)) |
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
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 | ||
} |
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
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 } |
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
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) | ||
} | ||
} |
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
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) | ||
} |
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
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]) | ||
} |
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
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('/')) | ||
} |