Skip to content

Instantly share code, notes, and snippets.

View smallufo's full-sized avatar
🏠
Working from home

smallufo

🏠
Working from home
  • Taiwan
View GitHub Profile
/**
* Created by smallufo on 2019-11-06.
*/
package destiny
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
import mu.KotlinLogging
import org.apache.http.client.fluent.Request
class FamilyReaderTest {
enum class Gender { M, F }
data class Family(
val father: String?,
val mother: String?,
val children: Set<Pair<Gender, String>>)
private fun chunk(lines: Sequence<String>): Sequence<Family> = sequence {
var father: String? = null
var mother: String? = null
val children = mutableSetOf<Pair<Gender, String>>()
lines.forEach { line ->
val tokens = line.split("[ \t]".toRegex())
val token0 = tokens[0]
if ((token0 == "F" || token0 == "M") && (children.isNotEmpty() || (father != null && mother != null))) {
yield(Family(father, mother, children.toSet()))
enum class Gender { M, F }
data class Family(
val father: String?,
val mother: String?,
val children: Set<Pair<Gender, String>>)
fun read() {
javaClass.getResourceAsStream("families.txt").bufferedReader(Charsets.UTF_8).useLines { lines ->
lines.filterNot { it.startsWith("#") }
# notes started with `#`
F Andy
M Babara
S Christ
D Dayna
F Andrew
M Barbie
S Charles
# notes started with `#`
F Andy
M Babara
S Christ
D Dayna
F Andrew
M Barbie
S Charles
S Daniel
@smallufo
smallufo / MyContext.kt
Created November 2, 2018 11:39
Kotlin DSL with Dependency Injection
import org.springframework.stereotype.Component
import javax.inject.Inject
class MyService(val mailImpl : Mailer) {
fun doSomething() {
// do something
}
}
@smallufo
smallufo / CarBikeTest2.kt
Created November 2, 2018 10:04
Cars Separated from Bikes
import org.junit.Test
class CarBikeTest2 {
enum class CarVendor { Audi, BMW, Benz }
enum class CarType { Sedan, Coupe, HatchBack }
data class Car(val vendor: CarVendor,
val model: String,
@smallufo
smallufo / CarBikeTest.kt
Last active November 2, 2018 09:46
Cars with Bikes
import org.junit.Test
class CarBikeTest {
enum class CarVendor { Audi, BMW, Benz }
enum class CarType { Sedan, Coupe, HatchBack }
data class Car(val vendor: CarVendor,
val model: String,
@smallufo
smallufo / CarTest.kt
Last active November 2, 2018 09:16
DSL style 1
import org.junit.Test
class CarTest {
enum class Vendor { Audi, BMW, Benz }
enum class Type { Sedan, Coupe, HatchBack }
data class Car(val vendor: Vendor,
val model: String,