Skip to content

Instantly share code, notes, and snippets.

@JorgeCastilloPrz
Last active May 5, 2017 20:54
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 JorgeCastilloPrz/4cbb52549b7260684db44a6fffc7e39b to your computer and use it in GitHub Desktop.
Save JorgeCastilloPrz/4cbb52549b7260684db44a6fffc7e39b to your computer and use it in GitHub Desktop.
standard recursive filter function for blog post
fun <T> List<T>.tail() = drop(1)
fun <T> List<T>.head() = first()
fun <T> filter(l: List<T>, f: (T) -> Boolean): List<T> {
if (l.isEmpty()) {
return listOf()
} else {
return if (f(l.head())) {
listOf(l.head()) + filter(l.tail(), f)
} else {
filter(l.tail(), f)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment