Created
March 12, 2012 03:40
-
-
Save ae6rt/2019572 to your computer and use it in GitHub Desktop.
Kotlin .each extension method on Collection<T>
This file contains 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
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