Finding the first stream of non-repeating elements in Scala (without recursion or side-effects) Here are some examples:

[1, 2, 3, 4, 5] =&gt; [1, 2, 3, 4, 5]
[10, 15, 10, 15, 30] =&gt; [10, 15]
[1, 2, 3, 4, 1, 5, 6, 7] =&gt; [1, 2, 3, 4]


Here's my best (and deeply ugly) non-recursive, side-effect-free solution so far:

x.scanLeft(List[Int]())((B, Term) =&gt; Term :: B).drop(1).takeWhile(i =&gt; !(i.tail contains i.head)).last.reverse


Minor optimization:

x.tail.scanLeft(List(x.head))((B, Term) =&gt; Term :: B).takeWhile(i =&gt; !(i.tail contains i.head)).last.reverse


This is different from distinct:

[1, 2, 3, 4, 1, 5, 6, 7] =&gt; [1, 2, 3, 4] and not [1, 2, 3, 4, 5, 6, 7]


Also, considering List[_] is a monoid, isn't there a scan that uses the monoid zero?
This is a fold, not a scan. A scan produces something with the same number of elements, and change the elements. A fold produces something new.

def firstDistinct[T](s: Seq[T]) = s.foldLeft(Seq[T]() -&gt; false) {
  case (result @ (_, true), _)           =&gt; result
  case ((seq, _), el) if seq contains el =&gt; seq -&gt; true
  case ((seq, _), el)                    =&gt; (seq :+ el) -&gt; false
}._1