Skip to content

Instantly share code, notes, and snippets.

View LouisCAD's full-sized avatar

Louis CAD LouisCAD

View GitHub Profile
@LouisCAD
LouisCAD / Flow.chunked.kt
Created December 7, 2022 18:25
Cut a Flow into chunks for batch processing.
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
fun <T> Flow<T>.chunked(size: Int): Flow<List<T>> = flow {
val elements = ArrayList<T>(size)
collect {
elements.add(it)
if (elements.size == size) {
emit(elements.toList())
@LouisCAD
LouisCAD / Flow.collectInParallel.kt
Last active December 13, 2022 16:05
Collect a Flow in parallel
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
suspend fun <T> Flow<T>.collectInParallel(
concurrency: Int,
collector: suspend (T) -> Unit
) = coroutineScope {
val channel: Channel<T> = Channel()
repeat(concurrency) { launch { for (e in channel) collector(e) } }
@LouisCAD
LouisCAD / ModalBottomSheet.kt
Last active November 20, 2023 09:16
Put this in its own Gradle module to have ModalBottomSheet in Material3 (or even without Material Design at all).
package com.louiscad.splitties.eap.bottomsheet
import android.app.Activity
import android.view.ViewGroup
import androidx.activity.compose.BackHandler
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
@LouisCAD
LouisCAD / SystèmeInternationalUnits_draft.kt
Created September 14, 2021 10:25
A partial double precision floating point based implementation of the Système International of Units.
@file:Suppress("experimental_is_not_enabled")
@file:OptIn(ExperimentalTime::class)
import kotlin.math.sqrt
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
//// Metrics
@LouisCAD
LouisCAD / Document.kt
Created May 24, 2021 21:28
Kotlin extensions for javax.xml (package org.w3c.dom). Feel free to copy paste.
import org.w3c.dom.Document
import org.w3c.dom.Text
import java.io.StringWriter
import javax.xml.transform.OutputKeys
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
fun Document.toText(): String {
val domSource = DOMSource(this)
@LouisCAD
LouisCAD / CharSequenceGetLineAndColumn.kt
Created March 24, 2021 22:25
Get the line and the column of an index in a given CharSequence.
/**
* 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 ->
import java.io.Serializable
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
LouisCAD / Companion object Powers example.kt
Last active January 21, 2024 07:45
Demonstration of the powers of companion objects when it comes to discoverability. Great alternative to java-style "factories".
package i_love_companion_objects
suspend fun main() {
// Discoverability is easy with the companion object
// (although autocomplete could use some performance improvements...)
doStuff(SomeInterface.createWithDefaults())
val someInstance: SomeInterface = SomeInterface.create(
parameterOne = 0,
parameterTwo = 1
)
@LouisCAD
LouisCAD / Flow <-> LiveData.kt
Created October 23, 2019 15:35
Convert a Flow to a LiveData and use LiveData as a Flow (from kotlinx.coroutines).
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
LouisCAD / FusedLocationFlow.kt
Last active May 23, 2024 12:11
Create a Flow of location updates on Android (using kotlinx.coroutines), backed by Fused Location Provider from Google Play Services.
/*
* Copyright 2019 Louis Cognault Ayeva Derman. Use of this source code is governed by the Apache 2.0 license.
*/
import android.location.Location
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import kotlinx.coroutines.CancellationException