Scala recursive Case class reduction

ساخت وبلاگ

Vote count: 0

I have a case class like this:

case class Test(i: Int, t: Option[Test])

I would now like to reduce this case class and add all the i's. So for example., if I have an input like this

val x = Test(1, Some(Test(2, Some(Test(3,None)))))

I expect the result to be 6 and here is what I have tried so far:

def all(acc: Int, test: Test): Int = { if (test.t.isDefined) all(acc, test.t.get) else test.i
}

This gives me 3 and I see that I'm missing a minor detail somewhere in the recursion and I'm not able to see where! Any clues?

asked 1 min ago

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : نویسنده : استخدام کار backsoft بازدید : 295 تاريخ : دوشنبه 31 خرداد 1395 ساعت: 22:43