Skip to content

Instantly share code, notes, and snippets.

View cbeyls's full-sized avatar

Christophe Beyls cbeyls

View GitHub Profile
@cbeyls
cbeyls / BinaryTrees.kt
Created August 18, 2019 21:15
Kotlin implementation of the binary-trees benchmark from The Computer Language Benchmarks Game
/**
* The Computer Language Benchmarks Game
* https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
*
* based on Jarkko Miettinen's Java program
* contributed by Christophe Beyls
*/
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
@cbeyls
cbeyls / Parcelers.kt
Last active October 8, 2021 23:16
Kotlin Parcelize extensions
package be.digitalia.sample
import android.os.Parcel
import kotlinx.parcelize.Parceler
import java.math.BigDecimal
import java.math.BigInteger
import java.util.Date
inline fun <T> Parcel.readNullable(reader: () -> T) =
if (readInt() != 0) reader() else null
sealed class Animal {
// Cats are simple. A cat is a cat.
object Cat : Animal()
sealed class Dog : Animal() {
abstract val breed: String?
class DogWithBreed(override val breed: String) : Dog()
@cbeyls
cbeyls / LiveDataFactory.java
Created April 27, 2019 12:50
A Factory providing utility LiveData instances
package be.digitalia.common.livedata;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import java.util.concurrent.TimeUnit;
@cbeyls
cbeyls / ComputableLiveData.java
Created March 11, 2018 14:01
A LiveData class that can be invalidated & computed on demand (based on an internal architecture components class)
package be.digitalia.arch.lifecycle;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.os.AsyncTask;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.WorkerThread;
@cbeyls
cbeyls / ViewLifecycleFragment.java
Last active June 29, 2020 14:06
Fragment providing separate lifecycle owners for each created view hierarchy.
package be.digitalia.archcomponentsfix.fragment;
import android.arch.lifecycle.Lifecycle.Event;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
@cbeyls
cbeyls / SuspendIntentService.kt
Created August 6, 2017 11:30
An Intent Service processing work in order from a Kotlin coroutine running on the main thread.
package be.digitalia.common.services
import android.app.Service
import android.content.Intent
import android.os.Message
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.LinkedListChannel
import kotlinx.coroutines.experimental.launch
@cbeyls
cbeyls / KotlinFunctions.md
Last active February 28, 2025 09:59
Comparison of Kotlin functions: also, apply, let, run, with
Function Function type Target passed as Returns
also Extension it Target
apply Extension this Target
let Extension it Block return value
run Extension this Block return value
with Regular this Block return value
@cbeyls
cbeyls / ParcelableUtils.kt
Last active October 28, 2019 21:30
Kotlin Parcelable utilities
package be.digitalia.sample
import android.os.Parcel
import android.os.Parcelable
import java.math.BigDecimal
import java.math.BigInteger
import java.util.*
interface KParcelable : Parcelable {
@cbeyls
cbeyls / FragmentArgumentDelegate.kt
Last active October 16, 2018 13:17 — forked from yanngx/FragmentArgumentDelegate.kt
Fragment arguments without hassle !
package be.brol
import android.os.Binder
import android.os.Bundle
import android.os.Parcelable
import android.support.v4.app.BundleCompat
import android.support.v4.app.Fragment
import kotlin.reflect.KProperty
/**