From 8e235082b4e3649e06e768ec50a2b6b6382fa73d Mon Sep 17 00:00:00 2001 From: Etienne Kneuss <colder@php.net> Date: Fri, 30 May 2014 15:23:50 +0200 Subject: [PATCH] Add List.take and List.drop, unproven, for convenience in benchmarks --- library/collection/List.scala | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/collection/List.scala b/library/collection/List.scala index f2f629243..1f8253f13 100644 --- a/library/collection/List.scala +++ b/library/collection/List.scala @@ -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) + } + } } -- GitLab