one more segmenter
Jul. 31st, 2014 12:27 amGiven a
So this line:
Seq[T]
, and two predicates, p0:T=>Bool
, p1:T=>Bool
, extract segments of the sequence that start with elements satisfying p0
and end with elements satisfying p1
.def extractChunks[T](src:Seq[T], p0:T=>Boolean,p1:T=>Boolean) = { val found:(List[T], List[List[T]]) = ((Nil:List[T], Nil:List[List[T]])/:src) { case ((Nil, out), x) => if (p0(x)) (List(x), out) else (Nil, out) case ((seg, out), x) => if (p1(x)) (Nil, (x::seg)::out) else (x::seg, out) } found._2 map (_.reverse) reverse }
So this line:
println(extractChunks[Char]("abracadabra", 'b'==, 'a'==))
Will print List(List(b, r, a), List(b, r, a))
.