Instantly share code, notes, and snippets.

Embed
What would you like to do?
folding cursors all day
inline fun <T> Cursor.fold(zero: T, f: (Cursor, T) -> T): T =
if (moveToFirst()) {
moveToPrevious()
var accum = zero
while (moveToNext()) {
accum = f(this, accum)
}
accum
} else zero
inline fun <T> Cursor.forEachRow(f: (Cursor) -> T): Unit =
fold(Unit) { c, x -> f(c) }
inline fun <T> Cursor.map(f: (Cursor) -> T): List<T> =
fold(ArrayList<T>(count)) { c, list -> list.apply { add(f(c)) } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment