Skip to content

Instantly share code, notes, and snippets.

@nkhalasi
nkhalasi / CoroutinesUtils.kt
Created September 10, 2022 05:21 — forked from jivimberg/CoroutinesUtils.kt
SQS Consumer using Kotlin coroutines and pool of workers.
package com.jivimberg.sqs.published
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.isActive
import kotlinx.coroutines.yield
import java.lang.Thread.currentThread
suspend fun CoroutineScope.repeatUntilCancelled(block: suspend () -> Unit) {
while (isActive) {
@nkhalasi
nkhalasi / zip_extract.kt
Last active March 4, 2017 03:56
Shortest zip extraction code I have ever written
import java.io.FileOutputStream
import java.nio.file.Path
import java.util.zip.ZipInputStream
tailrec fun ZipInputStream.extract(outputDir: Path, extractedFiles: List<Path>) : List<Path> =
nextEntry?.let { ze ->
val zf = outputDir.resolve(ze.name).apply {
toFile().outputStream().use {
fileForZipEntry(it)
}
@nkhalasi
nkhalasi / currying_and_scope.groovy
Created November 15, 2012 17:48
Function currying and scope in groovy
def sum = {a, b -> return a + b }
def square = {a -> return a * a }
def composition = {f, g, a, b -> f(g(a), g(b))}
def sumOfSquares = composition.curry(sum, square)
def sumOfSquaresOfTwoLargestNumbers(a, b, c) {
if ((a < b) && (a < c)) return sumOfSquares(b, c)
else if ((b < a) && (b < c)) return sumOfSquares(a, c)