Skip to content

Instantly share code, notes, and snippets.

View LDuncAndroid's full-sized avatar

L Duncan LDuncAndroid

  • Highlands, Scotland
  • 09:12 (UTC +01:00)
View GitHub Profile
import kotlin.random.Random
import kotlin.random.nextInt
// https://stackoverflow.com/posts/69076855/revisions @aSemy
val randomInts = generateSequence {
// this lambda is the source of the sequence's values
Random.nextInt(1..69)
}
// make the values distinct, so there's no repeated ints
.distinct()
[
{
"title": "Amandine",
"image": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Amandine_cake.jpg/200px-Amandine_cake.jpg",
"desc": "Chocolate layered cake filled with chocolate, caramel and fondant cream"
},
{
"title": "Angel cake",
"image": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Homemade_Angel_Slice_Cake_Stock_Photo_%2848754448236%29.jpg/200px-Homemade_Angel_Slice_Cake_Stock_Photo_%2848754448236%29.jpg",
"desc": "Sponge cake, cream, food colouring"
@LDuncAndroid
LDuncAndroid / ResponseBodyLogger.kt
Last active August 1, 2022 00:44
An OKHttp Interceptor that prints the response of a request to the console.
import com.squareup.okhttp.Interceptor
import com.squareup.okhttp.MediaType
import com.squareup.okhttp.Response
import com.squareup.okhttp.ResponseBody
import okio.BufferedSource
import okio.GzipSource
import okio.Okio
class ResponseBodyLogger : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
@LDuncAndroid
LDuncAndroid / iterable_sequence_count_pairs.kts
Last active May 24, 2021 22:23
Counts Pairs in Iterable & Sequence
fun <T> Iterable<T>.countPairs() = groupingBy { it }
.eachCount()
.values
.sumOf { it.div(2) }
fun <T> Sequence<T>.countPairs() = groupBy { it }
.mapValues { it.value.size }
.values
.sumOf {it.div(2)}
class LRUCache(capacity: Int) {
private val internalCache: LinkedHashMap<Int, Int> =
object : LinkedHashMap<Int, Int>(initialCapacity = capacity, loadFactor = 0.75f, accessOrder = true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Int, Int>?): Boolean {
return size > capacity
}
}
fun get(key: Int) = internalCache.getOrDefault(key, -1)
@LDuncAndroid
LDuncAndroid / Caching.kt
Created November 13, 2020 01:06
Implementing caching using Flow from Kotlin
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
// Following the RxJava implementation here: https://blog.mindorks.com/implement-caching-in-android-using-rxjava-operators
data class Data(val source: String)
class MemoryDataSource {

Keybase proof

I hereby claim:

  • I am lduncandroid on github.
  • I am lduncandroid (https://keybase.io/lduncandroid) on keybase.
  • I have a public key ASAOQZsKTkEH14w2y882fSvi50PhMuHeELEbx_-t4tTQdwo

To claim this, I am signing this object:

Android Audio Flinger

Requirements

  1. Introduction 1.1 Purpose 1.2 Intended Audience 1.3 Intended Use 1.4 Scope
  2. Overall Description
@LDuncAndroid
LDuncAndroid / ShootproofDownloader.kt
Created March 6, 2020 23:11
Quick Selenium script in Kotlin to download photos from Shootproof albums
package com.beflukey.shootproofdownloader
import org.openqa.selenium.By
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.support.ui.ExpectedConditions.presenceOfAllElementsLocatedBy
import org.openqa.selenium.support.ui.WebDriverWait
import java.io.*
import java.net.URL
import java.time.Duration
@LDuncAndroid
LDuncAndroid / DataBoundAdapter.kt
Created January 21, 2020 14:00
Data Bound Adapter
abstract class DataBoundAdapter<T, V : ViewDataBinding>(
diffCallback: DiffUtil.ItemCallback<T>
) : ListAdapter<T, DataBoundViewHolder<V>>(diffCallback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBoundViewHolder<V> {
val binding = createBinding(parent)
return DataBoundViewHolder(binding)
}
protected abstract fun createBinding(parent: ViewGroup): V