Skip to content

Instantly share code, notes, and snippets.

View jsonmergepatch.kt
val s = """
{
    "title": "Goodbye!",
    "author": {
      "givenName": "John",
      "familyName": "Doe"
    },
    "tags": [
      "example",
      "sample"
View jsonpatchsample.kt
val s = """
{
    "title": "Goodbye!",
    "author": {
      "givenName": "John",
      "familyName": "Doe"
    },
    "tags": [
      "example",
      "sample"
@bijukunjummen
bijukunjummen / CachingUtils.java
Last active Apr 6, 2021
Demonstration of caching with Project Reactor and Caffeine
View CachingUtils.java
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.time.Duration;
import java.util.function.Function;
import java.util.function.Supplier;
View tryflatmap.kt
sealed class Try<out T> {
    ...
    abstract fun <R> flatMap(tryBlock: (T) -> Try<R>): Try<R>
 
    data class Success<T>(private val result: T) : Try<T>() {
        ...
        override fun <R> flatMap(tryBlock: (T) -> Try<R>): Try<R> {
            return try {
                tryBlock(result)
            } catch (e: Throwable) {
View trymap.kt
sealed class Try<out T> {
    ...
    abstract fun <R> map(block: (T) -> R): Try<R>
 
    abstract fun get(): T
 
    data class Success<T>(private val result: T) : Try<T>() {
        ...
        override fun <R> map(block: (T) -> R): Try<R> {
            return of {
View tryget.kt
sealed class Try<out T> {
    ...
    abstract fun get(): T
 
    class Success<T>(private val result: T) : Try<T>() {
        ...
        override fun get(): T = result
    }
 
    class Failure<T>(private val throwable: Throwable) : Try<T>() {
View trysuccess.kt
sealed class Try<out T> {
 
    abstract fun isSuccess(): Boolean
    fun isFailure(): Boolean = !isSuccess()
 
    class Success<T>(private val result: T) : Try<T>() {
        override fun isSuccess(): Boolean = true
    }
 
    class Failure<T>(private val throwable: Throwable) : Try<T>() {
View basictrystruct1.kt
sealed class Try<out T> {
class Success<T>(private val result: T) : Try<T>()
class Failure<T>(private val throwable: Throwable) : Try<T>()
companion object {
fun <T> of(block: () -> T) = try {
Success(block())
View basictryusage1.kt
fun parseUrl(url: String): Try<URL> {
    return Try.of {
        URL(url)
    }
}
View findNSmallestAndLargest.kt
import java.util.*
fun findNSmallestAndLargest(nums: List<Int>, n: Int): Pair<List<Int>, List<Int>> {
val minFirst: Comparator<Int> = Comparator.naturalOrder<Int>()
val maxFirst: Comparator<Int> = minFirst.reversed()
val minPq: PriorityQueue<Int> = PriorityQueue(minFirst)
val maxPq: PriorityQueue<Int> = PriorityQueue(maxFirst)
for (num in nums) {
checkAndAddIfSmallest(maxPq, n, num)
checkAndAddIfLargest(minPq, n, num)