The List and Seq libraries have lots of interesting as well as obscure methods. Mastering them is fun an challenging.
Given a sequence, (in my case it its a List), and two candidate elements, a and b, what is the best way to determine whether the list contains a followed immediately by b somewhere in it? Here is my attempt, but I hope there’s an easier way.
(!list.isEmpty &&
(list.head :: list).tails.exists {
case x :: y :: _ => x == a && y == b
case _ => false
})
I have to use (list.head::list).tails because tails does not contain the list itself. Could also use list::(list.tails).