Last active
May 5, 2017 20:54
-
-
Save JorgeCastilloPrz/4cbb52549b7260684db44a6fffc7e39b to your computer and use it in GitHub Desktop.
standard recursive filter function for blog post
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
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