Created
July 21, 2016 09:33
-
-
Save meoyawn/8b3cf46ddf33aca97aa129b93e30e99a to your computer and use it in GitHub Desktop.
folding cursors all day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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