Skip to content

Instantly share code, notes, and snippets.

@priort
Last active July 12, 2019 20:38
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 priort/4140d0a67dfd74824b327dfef827db7c to your computer and use it in GitHub Desktop.
Save priort/4140d0a67dfd74824b327dfef827db7c to your computer and use it in GitHub Desktop.
fun <E> dropWhile(list: List<E>, f: (E) -> Boolean): List<E> =
when(list) {
Empty -> Empty
is Cons -> if (f(list.head)) dropWhile(list.tail, f) else list
}
fun main() {
val myList = Cons(1, Cons(2, Cons(3, Cons(4, Empty))))
dropWhile(myList) { it < 3 }.let(::println)
dropWhile(myList) { it < 1 }.let(::println)
dropWhile(myList) { it < 10 }.let(::println)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment