Skip to content

Instantly share code, notes, and snippets.

@kas-kad
kas-kad / gradualStreamLoop.kt
Last active October 9, 2017 11:54
the synchronous function gradually iterates over a stream during a given timeInterval.
fun <T> gradualLoop(timeInterval: Long, stream: Stream<T>, streamElementsCount: Long, action: (T) -> Unit) {
println("total elements: ${streamElementsCount}")
if (streamElementsCount > 0) {
var accumulatingOverdue: Long = 0L
var iterationLenFactMillis: Long = 0L // used to compensate iteration duration, so that we don't sleep through the end of the timeInterval
val iterationMaxAvgLenMillis: Long = timeInterval / streamElementsCount // maximum sleep duration
println("max iteration duration must be: ${iterationMaxAvgLenMillis} ms")
stream.forEach {
@kas-kad
kas-kad / SourceKit panic
Last active April 27, 2016 17:28
This Swift dictionary hangs the compilation and generates a SourceKit process that'll eat your CPU. (XCode Version 7.3 (7D175)) Bugtracker link: https://bugs.swift.org/browse/SR-1337
let machines = [
"iPod5,1":"iPod Touch 5",
"iPod7,1":"iPod Touch 6",
"iPhone3,1":"iPhone 4",
"iPhone3,2":"iPhone 4",
"iPhone3,3":"iPhone 4",
"iPhone4,1":"iPhone 4s",
"iPhone5,1":"iPhone 5 (GSM)",
"iPhone5,2":"iPhone 5 (GSM+CDMA)",
"iPhone5,3":"iPhone 5c (GSM)",