JVM technologies Spring certified trainer 7+ years with Scala 5+ years with Kotlin funKTionale Arrow (funTKtionale + Kategory) RxKotlin original developer and former team leader Spring JDBC Kotlin support
hello1: String = null //Null can not be a value of a non-null type String val hello2: String? = null val reversed0: String? = hello2?.reversed() val reversed1: String = reversed0.reversed()
Int): Int { return f(a, b) } fun main(args: Array<String>) { val x = doWithTwoNumbers(2, 3, { a, b a + b }) doWithTwoNumbers(x, 3) { a, b a * b } }
body: () Unit) { if (!conditional) { body() } } fun main(args: Array<String>) { val age: Int = //Magic here! unless(age > 18) { //Play video games all day long } }
listOf(1, 2, 3, 4) //call to method reference .map(InttoString) .map { i i + i } .map(StringtoInt) .filter { it > 20 } // `it` is an implicit parameter println(myList) }
val startTime = System.currentTimeMillis() val v = body() val endTime = System.currentTimeMillis() return v to endTime - startTime } inline fun <T> inTime(body: () T): Pair<T, Long> { val startTime = System.currentTimeMillis() val v = body() val endTime = System.currentTimeMillis() return v to endTime - startTime }
final Function0<? extends T> body) { Intrinsics.checkParameterIsNotNull((Object)body, "body"); final long startTime = System.currentTimeMillis(); final Object v = body.invoke(); final long endTime = System.currentTimeMillis(); return (Pair<T, Long>)TuplesKt.to(v, (Object)(endTime - startTime)); } @NotNull public static final <T> Pair<T, Long> inTime(@NotNull final Function0<? extends T> body) { Intrinsics.checkParameterIsNotNull((Object)body, "body"); final long startTime = System.currentTimeMillis(); final Object v = body.invoke(); final long endTime = System.currentTimeMillis(); return (Pair<T, Long>)TuplesKt.to(v, (Object)(endTime - startTime)); } Decompiled bytecode
{ Intrinsics.checkParameterIsNotNull((Object)args, "args"); final Pair b = time( (kotlin.jvm.functions.Function0<?>)_05_inlineKt$main$b._05_inlineKt$main$b$1.INSTANCE ); final long startTime$iv = System.currentTimeMillis(); Thread.sleep(1000L); final Object v$iv = "inline"; final long endTime$iv = System.currentTimeMillis(); final Pair i = TuplesKt.to(v$iv, (Object)(endTime$iv - startTime$iv)); System.out.println(b); System.out.println(i); }
{ public static final _05_inlineKt$main$b$1 INSTANCE; @NotNull public final String invoke() { Thread.sleep(1000L); return "boring"; } static { _05_inlineKt$main$b$1.INSTANCE = new _05_inlineKt$main$b$1(); } }
val log = LoggerFactory.getLogger("builder") return log.body() } fun main(args: Array<String>) { val i = logged { info("THIS is INFO") debug("THIS is DEBUG") error("THIS is ERROR") 1 + 2 } }
Scanner(System.`in`) fun awaitEnter(){ println("Press enter to continue") scanner.nextLine() } } fun monitored(body: MonitorUtil.() Unit) { val monitor = MonitorUtil() monitor.body() monitor.awaitEnter() }
a block of code and has a similar life cycle, but can complete with a return value or an exception. Technically, a coroutine is an instance of a suspendable computation, a computation that may suspend. Coroutines aren't bound to a particular thread and can suspend in one Thread and resume execution in a different one” From the book Functional Kotlin
function with a suspend modifier. It can suspend a coroutine without blocking the thread Suspending lambda A lambda function with a suspend modifier Coroutine builder A function that takes a suspending lambda Suspension point A point where a suspending function is invoked Continuation The state of a suspended coroutine at a suspension point
GetCounter(val response: CompletableDeferred<Int>) : CounterMsg() fun counterActor(start: Int) = actor<CounterMsg> { var counter = start for (msg in channel) { when (msg) { is IncCounter counter is GetCounter msg.response.complete(counter) } } }
import arrow.typeclasses.binding fun main(args: Array<String>) { val a = Some(1) val b = Some(2) val c = Option.monad().binding { val x = a.bind() val y = b.bind() x + y }.ev() println("c = $c") } continuation: flatMap continuation: map