Skip to content

Instantly share code, notes, and snippets.

@rkrzewski
Last active August 29, 2015 13:58
Show Gist options
  • Save rkrzewski/9937461 to your computer and use it in GitHub Desktop.
Save rkrzewski/9937461 to your computer and use it in GitHub Desktop.
def filterNot[T](ts: Seq[T])(p: T => Boolean): Seq[T] =
(ts :\ Seq[T]())((t, a) => if (!p(t)) t +: a else a)
@tailrec
def drop[T](ts: Seq[T])(n : Int): Seq[T] = (n, ts) match {
case (0, _) => ts
case (n, _ +: tts) if n > 0 => drop(tts)(n - 1)
case _ => Seq()
}
def duplicate[T](ts: Seq[T]): Seq[T] =
ts.flatMap(x => Seq(x, x))
def duplicate[T](ts: Seq[T]): Seq[T] = {
@tailrec
def go(ts: Seq[T], acc: Seq[T]): Seq[T] = ts match {
case Seq() => acc
case head +: tail => go(tail, head +: head +: acc)
}
go(ts.reverse, Seq())
}
def duplicate[T](ts: Seq[T]): Seq[T] =
ts.flatMap(x => (1 to 2).flatMap(n => List(x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment