Skip to content

Instantly share code, notes, and snippets.


Louis CAD LouisCAD

View GitHub Profile
LouisCAD / SystèmeInternationalUnits_draft.kt
Created Sep 14, 2021
A partial double precision floating point based implementation of the Système International of Units.
View SystèmeInternationalUnits_draft.kt
import kotlin.math.sqrt
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
//// Metrics
LouisCAD / Document.kt
Created May 24, 2021
Kotlin extensions for javax.xml (package org.w3c.dom). Feel free to copy paste.
View Document.kt
import org.w3c.dom.Document
import org.w3c.dom.Text
import javax.xml.transform.OutputKeys
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
fun Document.toText(): String {
val domSource = DOMSource(this)
LouisCAD / CharSequenceGetLineAndColumn.kt
Created Mar 24, 2021
Get the line and the column of an index in a given CharSequence.
View CharSequenceGetLineAndColumn.kt
* Returns the line and the column of [index] in the given CharSequence.
fun CharSequence.getLineAndColumn(index: Int): LocationInText {
if (index !in 0..lastIndex) {
throw IndexOutOfBoundsException(index)
var lineNumber = 1
var columnNumber = 1
forEachIndexed { currentIndex, c ->
View MutableLazy.kt
import kotlin.reflect.KProperty
interface MutableLazy<T> : Lazy<T> {
override var value: T
fun <T> mutableLazyOf(value: T): MutableLazy<T> = InitializedLazyImpl(value)
fun <T> mutableLazy(initializer: () -> T): MutableLazy<T> = SynchronizedLazyImpl(initializer)
LouisCAD / Companion object Powers example.kt
Last active Sep 26, 2021
Demonstration of the powers of companion objects when it comes to discoverability. Great alternative to java-style "factories".
View Companion object Powers example.kt
package i_love_companion_objects
suspend fun main() {
// Discoverability is easy with the companion object
// (although autocomplete could use some performance improvements...)
val someInstance: SomeInterface = SomeInterface.create(
parameterOne = 0,
parameterTwo = 1
LouisCAD / Flow <-> LiveData.kt
Created Oct 23, 2019
Convert a Flow to a LiveData and use LiveData as a Flow (from kotlinx.coroutines).
View Flow <-> LiveData.kt
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.liveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.*
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.seconds
LouisCAD / FusedLocationFlow.kt
Last active May 21, 2022
Create a Flow of location updates on Android (using kotlinx.coroutines), backed by Fused Location Provider from Google Play Services.
View FusedLocationFlow.kt
* Copyright 2019 Louis Cognault Ayeva Derman. Use of this source code is governed by the Apache 2.0 license.
import android.location.Location
import kotlinx.coroutines.CancellationException
LouisCAD / AndroidLocationFlow.kt
Last active Sep 1, 2021
Exposes Location updates from Android's LocationManager as a Flow. Also see
View AndroidLocationFlow.kt
import android.location.Criteria
import android.location.Location
import android.location.LocationListener
import android.Manifest
import android.os.Bundle
import androidx.annotation.RequiresApi
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import splitties.systemservices.locationManager // See
LouisCAD / !
Created Feb 28, 2019 — forked from Ribesg/!
How to use an iOS Framework in a Kotlin MPP library published to Maven
View !

This gist demonstrates how to build a Kotlin MPP library so that the iOS sourceSet depends on and uses an iOS Framework as a dependency.

Key ideas:

  • We use [Carthage] to retrieve/build the iOS Framework.
  • We use [cinterop] to create bindings allowing us to use the iOS Framework from Kotlin
  • We build and publish the library using ./gradlew publishToMavenLocal
  • We build and publish [klib] artifacts for both the arm64 and x86_64 architectures, you can easily add arm32 if you need.
  • Note that the publish process also publishes a cinterop klib artifact, allowing dependents to also know about the iOS Framework headers.

You can find a gist explaining how to use such library in an iOS app [here][ios-app].

View ConflatedBroadcastChannelDelegate.kt
inline operator fun <E> ConflatedBroadcastChannel<E>.getValue(
thisRef: Any?,
prop: KProperty<*>
): E = value
inline operator fun <E> ConflatedBroadcastChannel<E>.setValue(
thisRef: Any?,
prop: KProperty<*>,