This file contains hidden or 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
package com.example.bridge | |
import arrow.core.Either | |
import arrow.core.raise.Raise | |
import arrow.core.raise.ensure | |
import arrow.core.raise.ensureNotNull | |
// Functions useful for bridging from context receivers to the new context parameters | |
// This is temporary and will not be required (or provided by library authors) in the future |
This file contains hidden or 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
fun getLevels(report: String): List<Int> = report | |
.trim() | |
.split(" ".toRegex()) | |
.filter { it.length > 0 } | |
.map { it.trim().toInt() } | |
fun isSafe(levels: List<Int>): Boolean { | |
// if all diffs are in range, and go all up or all down, it's safe | |
// iterate through and taking once of the numbers out to | |
return (0 until levels.size).any { idx -> |
This file contains hidden or 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
// our data model | |
data class Person(val name: String, val age: Int, val email: String) | |
// vanilla Kotlin | |
fun createPerson(name: String?, age: Int?, email: String?): Person? = | |
name?.let { n -> | |
age?.let { a -> | |
email?.let{ e -> | |
Person(n, a, e); | |
} |
This file contains hidden or 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
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.newSingleThreadContext | |
import kotlinx.coroutines.runBlocking | |
// inspired by: https://dzone.com/articles/print-even-and-odd-numbers-using-two-threads-compl | |
// You can run this in a Kotlin REPL | |
fun main() = runBlocking { | |
(1..100).forEach { |
This file contains hidden or 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
import kotlinx.coroutines.async | |
import kotlinx.coroutines.coroutineScope | |
import kotlinx.coroutines.flow.Flow | |
import kotlinx.coroutines.reactive.asFlow | |
import kotlinx.coroutines.reactor.awaitSingle | |
import reactor.core.publisher.Flux | |
import reactor.core.publisher.Mono | |
typealias Workflow<R> = suspend () -> Result<R> | |
typealias MonoWorkflow<R> = suspend () -> Mono<R> |
This file contains hidden or 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
import io.vavr.control.Try; | |
import java.util.concurrent.Future; | |
import org.springframework.scheduling.annotation.Async; | |
import org.springframework.scheduling.annotation.AsyncResult; | |
class Foo {} | |
interface FooService { | |
/** Returns Future<Try<T>> for better exception handling */ | |
@Async |
This file contains hidden or 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 trait Quantity | |
object Quantity { | |
final case class UnitQuantity private (units: Int) extends Quantity | |
final case class WeightQuantity private (kgs: Double) extends Quantity | |
// factory with validation | |
object UnitQuantity { | |
def create(units: Int): Either<ValidationError, UnitQuantity> = | |
if (units <= 0) Either.left(ValidationError("units must be positive")) |
This file contains hidden or 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
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Objects; | |
import java.util.Optional; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
import java.util.function.Supplier; | |
/** |
This file contains hidden or 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
package techtalk; | |
import com.jnape.palatable.lambda.adt.hlist.Tuple2; | |
import com.jnape.palatable.lambda.functions.Fn1; | |
import com.jnape.palatable.lambda.functions.Fn2; | |
import io.vavr.collection.HashMap; | |
import io.vavr.collection.Map; | |
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple; | |
import static com.jnape.palatable.lambda.functor.builtin.State.state; |