Skip to content

Instantly share code, notes, and snippets.

View BenjaminEarley's full-sized avatar
🏁

Benjamin Earley BenjaminEarley

🏁
View GitHub Profile
@BenjaminEarley
BenjaminEarley / TowerOfHanoi.kt
Created July 5, 2022 14:27
Recursive Tower Of Hanoi solution using DeepRecursiveFunction
data class Operation(val disk: Int, val source: Char, val destination: Char, val auxiliary: Char)
val calculate = DeepRecursiveFunction<Operation, Unit> { (disk, source, destination, auxiliary) ->
if (disk <= 0) Unit
else {
callRecursive(Operation(disk - 1, source, auxiliary, destination))
println("Move disk $disk from tower [$source] to [$destination]")
callRecursive(Operation(disk - 1, auxiliary, destination, source))
}
}
@BenjaminEarley
BenjaminEarley / withLatestFrom.kt
Last active May 11, 2022 17:51
withLatestFrom with Flow
// Combines the source Observable with other Observables to create an Observable whose values are calculated
// from the latest values of each, only when the source emits.
// Interactive Diagram: https://rxmarbles.com/#withLatestFrom
// Discussion Thread: https://github.com/Kotlin/kotlinx.coroutines/issues/1498
fun <A, B : Any, R> Flow<A>.withLatestFrom(other: Flow<B>, transform: suspend (A, B) -> R): Flow<R> = flow {
coroutineScope {
val latestB = AtomicReference<B?>()
val outerScope = this
launch {
try {
fun mergeSort(list: List<Int>): List<Int> {
fun merge(left: ConsList<Int>, right: ConsList<Int>): ConsList<Int> =
when {
left is Nil -> right
right is Nil -> left
else -> {
val leftHead = (left as Cons).head
val rightHead = (right as Cons).head
if (leftHead < rightHead) Cons(leftHead, merge(left.tail, right))
@BenjaminEarley
BenjaminEarley / fib.ml
Last active September 20, 2018 13:45
fibonacci
let fib x = fibHelper (x-1);;
let rec fibHelper x = if x <= 1 then x else fibHelper (x - 1) + fibHelper (x - 2);;
(* 11th Value *)
fib 11
@BenjaminEarley
BenjaminEarley / fib.scala
Last active September 10, 2018 14:42
Lazy fibonacci
val fib:Stream[BigInt] = {
def next(fib: Stream[BigInt]): Stream[BigInt] = (fib.head + fib.tail.head) #:: next(fib.tail)
0 #:: 1 #:: next(fib)
}
// Print 11th Value 55
fib.drop(10).head
@BenjaminEarley
BenjaminEarley / drawableToBitmap.kt
Created July 2, 2018 13:41
Android: Extension for getting a bitmap from a drawable resource (optional tinting)
fun Context.getBitmapFromVectorDrawable(@DrawableRes drawableRes: Int, @ColorRes colorRes: Int? = null): Bitmap? {
var drawable = ContextCompat.getDrawable(this, drawableRes) ?: return null
drawable = DrawableCompat.wrap(drawable).mutate()
colorRes?.let {
DrawableCompat.setTint(
drawable,
ContextCompat.getColor(
this,
@BenjaminEarley
BenjaminEarley / tintedDrawable.kt
Created July 2, 2018 13:39
Android: Extension for getting a tinted drawable
fun Context.getTintedDrawable(@DrawableRes drawable: Int, @ColorRes color: Int) =
ContextCompat.getDrawable(
this,
drawable
)
?.let { d ->
DrawableCompat.wrap(d)
}
?.apply {
DrawableCompat.setTint(
@BenjaminEarley
BenjaminEarley / dipsToPixels.kt
Last active September 8, 2018 20:18
Android: Extensions for converting dp values to px values
fun Float.dipsToPixels() =
TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this,
Resources.getSystem().displayMetrics
)
fun Int.dipsToPixels() = toFloat().dipsToPixels().toInt()
@BenjaminEarley
BenjaminEarley / pair.kt
Created April 3, 2018 15:00
Pair type without a data structure
@file:Suppress("UNCHECKED_CAST")
fun main(args: Array<String>) {
val pair = pair("HELLO", 1)
println(pair.first().toLowerCase())
println(pair.second() * 5)
}
typealias Pair<A, B> = ((A, B) -> Any?) -> Any?

Keybase proof

I hereby claim:

  • I am benjaminearley on github.
  • I am bin_earley (https://keybase.io/bin_earley) on keybase.
  • I have a public key ASCvE5Eheyzi-cejt9fDSxyQfOL7CKSWt8w1HDveMW6J3Ao

To claim this, I am signing this object: