Skip to content

Instantly share code, notes, and snippets.

@orangy
Created July 22, 2014 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orangy/e3beb9646b996ce48dc4 to your computer and use it in GitHub Desktop.
Save orangy/e3beb9646b996ce48dc4 to your computer and use it in GitHub Desktop.
package x
fun fn(items: List<String?>) {
items.foreach(filter.notNull) { value ->
println("$value")
}
items.foreach(map.indexed) { (index,value) ->
println("$index $value")
}
}
object map {
object indexed
}
object filter {
object notNull
}
inline fun <T> Iterable<T>.foreach(_: map.indexed, body: (Int, T) -> Unit) {
var index = 0
for (element in this) {
body(index++, element)
}
}
inline fun <T : Any> Iterable<T?>.foreach(_: filter.notNull, body: (T) -> Unit) {
for (element in this) {
if (element != null)
body(element)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment