Skip to content

Instantly share code, notes, and snippets.

@meoyawn
Created July 21, 2016 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoyawn/8b3cf46ddf33aca97aa129b93e30e99a to your computer and use it in GitHub Desktop.
Save meoyawn/8b3cf46ddf33aca97aa129b93e30e99a to your computer and use it in GitHub Desktop.
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