Raphael Roth Raphael Roth - 1 year ago 80
Scala Question

Scala: take stream "while increasing"

I want to traverse a sequence:

val data = Seq(1,2,3,4,5,5,6,7,8)

My goal is to build a new sequence but only take data up to the point where the their values stop increasing. So the desired output would be
in this case.

I think the solution lies in using
(to access the next value), but I cannot figure out how to combine them.

Answer Source

Tricky, but working:

  .scanLeft(Option.empty[Int]) {
    case (None, item) => Option(item)
    case (Some(prev), item) => Option(item).filter(cur => cur > prev)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download