Skip to content

Instantly share code, notes, and snippets.

View martin-tarjanyi's full-sized avatar

Martin Tarjányi martin-tarjanyi

View GitHub Profile
@martin-tarjanyi
martin-tarjanyi / flow_concurrent_map.kt
Created June 15, 2024 20:58
Kotlin flow concurrent map
fun <T, R> Flow<T>.concurrentMap(
concurrency: Int,
dispatcher: CoroutineDispatcher = Dispatchers.Unconfined,
transform: suspend (value: T) -> R,
): Flow<R> =
map { flow { emit(transform(it)) }.flowOn(dispatcher) }
.flattenMerge(concurrency)
@martin-tarjanyi
martin-tarjanyi / apache_http_coroutine.kt
Created June 1, 2024 22:01
Non-blocking HTTP calls with Async HTTP 5 and Kotlin Coroutines
package org.example.apache
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ToString
@EqualsAndHashCode
public final class ExplicitComparator<T> implements Comparator<T>
{
private final Map<T, Integer> rankMap;