Skip to content
Snippets Groups Projects
Commit 8e235082 authored by Etienne Kneuss's avatar Etienne Kneuss
Browse files

Add List.take and List.drop, unproven, for convenience in benchmarks

parent 25fabf5c
No related branches found
No related tags found
No related merge requests found
......@@ -64,6 +64,26 @@ sealed abstract class List[T] {
case Cons(x,xs) => xs.reverse :+ x
}
} ensuring (res => (res.size == size) && (res.content == content))
def take(i: Int): List[T] = (this, i) match {
case (Nil(), _) => Nil()
case (Cons(h, t), i) =>
if (i == 0) {
Nil()
} else {
Cons(h, t.take(i-1))
}
}
def drop(i: Int): List[T] = (this, i) match {
case (Nil(), _) => Nil()
case (Cons(h, t), i) =>
if (i == 0) {
Cons(h, t)
} else {
t.drop(i-1)
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment