Skip to content

Instantly share code, notes, and snippets.

@stickupkid
Created January 17, 2012 17:40
Show Gist options
  • Select an option

  • Save stickupkid/1627765 to your computer and use it in GitHub Desktop.

Select an option

Save stickupkid/1627765 to your computer and use it in GitHub Desktop.
Kotlin: High level functions example (LINQ esq)
import java.util.*
fun print(message : String) {
System.out?.println(message)
}
fun <T> List<T>.filter(
body : (T) -> Boolean
) : List<T> {
val l = ArrayList<T>()
for(val i in this) {
if(i != null && body(i))
l.add(i)
}
return l;
}
fun <T> List<T>.foreach(
body : (T) -> Unit
) : List<T> {
for(val i in this) {
if(i != null)
body(i)
}
return this;
}
fun <T> Array<T>.list(
body : (T) -> T) : List<T> {
val l = ArrayList<T>()
for(val i in this) {
l.add(body(i))
}
return l
}
fun main(args : Array<String>) {
args list {it} filter {it.length > 0} foreach {print("Hello, $it!")}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment