Skip to content

Instantly share code, notes, and snippets.

View KotlinFlowCheckBlanks.kt
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
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
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
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
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
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
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)
@ShelbyCohen
ShelbyCohen / GreetingController
Last active Jun 21, 2019
Kotlin Example of Class and Controller
View GreetingController
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")
@ShelbyCohen
ShelbyCohen / Gradle Build
Last active Jun 21, 2019
Changes to a Gradle project to add Kotlin
View Gradle Build
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
@ShelbyCohen
ShelbyCohen / Maven Pom
Last active Jun 21, 2019
Changes in a maven application’s pom.xml to add Kotlin
View Maven Pom
<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>