Skip to content

Instantly share code, notes, and snippets.

Avatar

Zach Klippenstein zach-klippenstein

View GitHub Profile
@zach-klippenstein
zach-klippenstein / ComposableDumper.kt
Last active Aug 17, 2020
Simple utility to dump the current Composition tree to logcat.
View ComposableDumper.kt
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.currentComposer
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.IntBounds
import androidx.ui.tooling.CallGroup
import androidx.ui.tooling.Group
import androidx.ui.tooling.NodeGroup
import androidx.ui.tooling.asTree
@zach-klippenstein
zach-klippenstein / DrawLayerDemo.kt
Last active Oct 21, 2020
Interactive demo of the drawLayer composable function. (screencap in comments)
View DrawLayerDemo.kt
import androidx.animation.PhysicsBuilder
import androidx.animation.Spring.DampingRatioHighBouncy
import androidx.animation.Spring.StiffnessLow
import androidx.compose.Composable
import androidx.compose.Model
import androidx.compose.remember
import androidx.ui.animation.animate
import androidx.ui.core.DrawClipToBounds
import androidx.ui.core.Text
import androidx.ui.core.drawLayer
View Host.kt
interface Host {
suspend fun getMovies(): List<Movies>
suspend fun setWatched(movie: Movie): Boolean
close()
}
class HostImpl(
// You could have a different WorkerPool for each system you need to talk to
// that has its own throttling requirements.
private val workerPool: WorkerPool
@zach-klippenstein
zach-klippenstein / DatabaseWorkerPool.kt
Created Oct 24, 2018
Sketch of what a worker pool for limiting concurrent database operations might look like.
View DatabaseWorkerPool.kt
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.coroutineContext
class DatabaseExecutor(
nWorkers: Int,
context: CoroutineContext
) {
View CoroutineNamesTest.kt
class CoroutineNamesTest {
@Test fun coroutineNames() {
runBlocking(CoroutineName("parent")) {
println("parent: $coroutineContext")
launch {
println("unnamed child: $coroutineContext")
}
launch(CoroutineName("named child")) {
@zach-klippenstein
zach-klippenstein / SelectClauses.kt
Last active Jun 1, 2019
Helper methods to build select clauses for Kotlin coroutines
View SelectClauses.kt
package com.zachklipp.coroutines
import kotlinx.coroutines.experimental.DisposableHandle
import kotlinx.coroutines.experimental.intrinsics.startCoroutineCancellable
import kotlinx.coroutines.experimental.selects.SelectClause0
import kotlinx.coroutines.experimental.selects.SelectClause1
import kotlinx.coroutines.experimental.selects.SelectClause2
import kotlinx.coroutines.experimental.selects.SelectInstance
/**
@zach-klippenstein
zach-klippenstein / git-edit.sh
Last active Aug 29, 2015
Shell script for amending commits on a feature branch.
View git-edit.sh
#!/bin/zsh
if [ "$1" = '-h' ]; then
cat <<-EOF
Usage: git-edit <branch> [args-to-commit]
e.g. If your tree looks like this:
* cc3cd23 (HEAD -> feat1) Feature 1.2
* 53f56c1 Feature 1
* 08355e5 (master) Initial commit.
@zach-klippenstein
zach-klippenstein / audiowave.go
Last active Dec 26, 2018
Terminal program that plays a sine wave.
View audiowave.go
//usr/local/bin/go run $0 "$@"; exit
/*
Terminal program that plays a sine wave.
The pitch and volume can be controlled (help is shown in the UI).
Installation:
go get github.com/gizak/termui
@zach-klippenstein
zach-klippenstein / FunctionalStack.kt
Created Jan 31, 2015
Example of a simple functional data structure in Kotlin.
View FunctionalStack.kt
package com.example.funcstack
import java.util.NoSuchElementException
public trait FunctionalStack<out T> {
public val size: Int
public fun pop(): Pair<T, FunctionalStack<T>>
}
/**
You can’t perform that action at this time.