Skip to content

Instantly share code, notes, and snippets.

@ae6rt
Created March 12, 2012 03:40
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 ae6rt/2019572 to your computer and use it in GitHub Desktop.
Save ae6rt/2019572 to your computer and use it in GitHub Desktop.
Kotlin .each extension method on Collection<T>
package litfunc
import java.util.ArrayList
import java.util.Collection
fun main(args : Array<String>) : Unit {
val l = ArrayList<String>()
l.add("one")
l.add("two")
l.add("buckle my shoe")
l.each { it ->
println(it)
}
/*
Output is:
one
two
buckle my shoe
*/
}
fun <T> Collection<T>.each(fn : (T) -> Unit) {
for( item in this) {
fn(item)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment