Skip to content

Instantly share code, notes, and snippets.

View christianb's full-sized avatar

Christian Bunk christianb

View GitHub Profile
/* Write a function that takes in an integer n and returns the nth Fibonacci number.
*
* The Fibonacci sequence is defined as follows: the first number of the sequence is `0`,
* the second number is `1`, and the nth number is the sum of (n - 1)th and (n - 2)th numbers.
*
* Note: For the purpose of this question, n starts at 1, so `getNthFib(1) == 0` and `getNthFib(2) == 1`
*
* Sequence: 0, 1, 1, 2, 3, 5 , 8, 13, 21, ...
*
* Sample input: n = 2
@christianb
christianb / pipe.kt
Created January 23, 2021 12:07
Pipe function in Kotlin
// From https://discuss.kotlinlang.org/t/pipe-forward-operator/2098/22
// Showes how to implement a pipe function in Kotlin
infix fun <T, R> T.into(func: (T) -> R) = func(this)
// How to use
fun add(x: Int): Int = x + 1
fun mul(x: Int): Int = x * 12
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.random.Random
fun main() = runBlocking {
val providers: List<Provider> = listOf(Provider("p1"), Provider("p2"), Provider("p3"))
println(executeAllProviderAsync(providers))
}
suspend fun executeAllProviderAsync(providers: List<Provider>): List<Int> = withContext(Dispatchers.Default){
@christianb
christianb / updateForkedRepo.sh
Last active September 11, 2020 22:06
Update a forked Repository
# https://stackoverflow.com/questions/7244321/how-do-i-update-a-github-forked-repository
# Add the remote, call it "upstream":
git remote add upstream https://github.com/whoever/whatever.git
git fetch upstream
git fetch --tags upstream
git checkout master
@christianb
christianb / reset branch to origin
Created September 7, 2020 11:54
Resets a branch to its origin state
git fetch origin branch_name
git reset --hard origin/branch_name
@christianb
christianb / checkout-before-date.sh
Created August 27, 2020 10:32
Git checkout commit before date
git checkout `git rev-list -n 1 --first-parent --before="2019-04-01 01:00" master`
@christianb
christianb / grep-occurence.sh
Created August 27, 2020 10:27
Show number of occurence of a string within files in directory
# number of @Test in all files in this directory
grep -o -r "@Test" . | wc -l
@christianb
christianb / constraint_view_min_max.xml
Created July 24, 2020 14:57
Contraint a View Min Max within ConstraintLayout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintWidth_min="10dp"
@christianb
christianb / ConcurrencyHelpers.kt
Created June 13, 2020 21:15 — forked from objcode/ConcurrencyHelpers.kt
Helpers to control concurrency for one shot requests using Kotlin coroutines.
import kotlinx.coroutines.CoroutineStart.LAZY
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.yield
import java.util.concurrent.atomic.AtomicReference
import kotlin.DeprecationLevel.ERROR
@christianb
christianb / when_updating_targetSdkVersion.txt
Created November 22, 2019 16:26
When updating targetSdkVersion?
https://support.google.com/googleplay/android-developer/answer/113469#targetsdk
https://developer.android.com/distribute/best-practices/develop/target-sdk