View KotlinFlowCheckBlanks.kt
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
val blank = { entry: Map.Entry<Int, LongWrapper> -> | |
flowOf(max(0L, entry.value.get() - | |
scrabbleAvailableLetters[entry.key - 'a'.toInt()])) | |
} | |
val nBlanks = { word: String -> | |
flow { | |
emit(histoOfLetters(word) | |
.flatMapConcat { map -> map.entries.iterator().asFlow() } | |
.flatMapConcat({ blank(it) }) |
View ReactorCheckBlanks.kt
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
val blank = Function<Map.Entry<Int, LongWrapper>, Flux<Long>> { entry -> | |
Flux.just(max(0L, entry.value.get() - scrabbleAvailableLetters[entry.key - 'a'.toInt()])) | |
} | |
val nBlanks = Function<String, Flux<Long>> { word -> | |
Flux.from(histoOfLetters.apply(word) | |
.flatMap<Map.Entry<Int, LongWrapper>> { map -> | |
Flux.fromIterable<Map.Entry<Int, LongWrapper>>( | |
Iterable { map.entries.iterator() }) } | |
.flatMap(blank) |
View KotlinFlowIntegerStream.kt
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
val toIntegerStream = { string: String -> | |
IterableSpliterator.of(string.chars().boxed().spliterator()).asFlow() | |
} | |
val histoOfLetters = { word: String -> | |
flow { | |
emit(toIntegerStream(word).fold(HashMap<Int, LongWrapper>()) { accumulator, value -> | |
var newValue: LongWrapper? = accumulator[value] | |
if (newValue == null) { | |
newValue = LongWrapper.zero() |
View ReactorIntegerStream.kt
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
val toIntegerStream = Function<String, Flux<Int>> { string -> | |
Flux.fromIterable(IterableSpliterator.of(string.chars().boxed().spliterator())) | |
} | |
val histoOfLetters = Function<String, Flux<HashMap<Int, LongWrapper>>> { word -> | |
Flux.from(toIntegerStream.apply(word) | |
.collect( | |
{ HashMap() }, | |
{ map: HashMap<Int, LongWrapper>, value: Int -> | |
var newValue: LongWrapper? = map[value] |
View KotlinFlowPlay.kt
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
public override fun play(): List<Map.Entry<Int, List<String>>> { | |
val scoreOfALetter = { letter: Int -> flowOf(letterScores[letter - 'a'.toInt()]) } | |
val letterScore = { entry: Map.Entry<Int, LongWrapper> -> | |
flowOf( | |
letterScores[entry.key - 'a'.toInt()] * Integer.min( | |
entry.value.get().toInt(), | |
scrabbleAvailableLetters[entry.key - 'a'.toInt()] | |
) | |
) |
View ReactorPlay.kt
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
public override fun play(): List<Map.Entry<Int, List<String>>> { | |
val scoreOfALetter = Function<Int, Flux<Int>> { letter -> Flux.just(letterScores[letter - 'a'.toInt()]) } | |
val letterScore = Function<Map.Entry<Int, LongWrapper>, Flux<Int>> { entry -> | |
Flux.just( | |
letterScores[entry.key - 'a'.toInt()] * Integer.min( | |
entry.value.get().toInt(), | |
scrabbleAvailableLetters[entry.key - 'a'.toInt()] | |
) | |
) |
View sealedClassExample
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
sealed class UserResult { | |
data class Failure(val message : String) : UserResult() | |
data class Success(val users : List<String>) : UserResult() | |
data class Loading(val animation : String) : UserResult() | |
} | |
fun process(result : UserResult) = when(result) { | |
is UserResult.Failure -> println(result.message) | |
is UserResult.Success -> result.users.forEach { println(it)} | |
is UserResult.Loading -> println(result.animation) |
View GreetingController
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
data class Greeting(val id: Long, val content: String) | |
Next, a GreetingController will be used for a get request: | |
@RestController | |
class GreetingController { | |
val counter = AtomicLong() | |
@GetMapping("/greeting") |
View Gradle Build
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
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | |
} | |
} |
View Maven Pom
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
<dependency> | |
<groupId>org.jetbrains.kotlin</groupId> | |
<artifactId>kotlin-stdlib-jdk8</artifactId> | |
<version>${kotlin.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jetbrains.kotlin</groupId> | |
<artifactId>kotlin-test</artifactId> | |
<version>${kotlin.version}</version> |