score:-1

Something like this, perhaps:

list.zip(list.drop(1)).map { case(x,y) => y-x }

You can also do it with foldRight:

list.foldRight(List.empty[Int]) {
  case (x, Nil) => List(x)
  case (x, y :: tail) => x :: (y - x) :: tail
}.drop(1)

But may I ask what is it that you are really trying to do here?

Or, if you insist on only using map and reduce for some obscure reason (what would that be), something like this will work:

 list
  .map { x => List(x) }
  .reduce { (a,b) => 
    b.head :: (b.head - a.head) :: a.tail 
  }.drop(1).reverse

This really is cheating though, because the function you give to reduce must be commutative and associative, and this case it is neither. It just happens to work in toy applications, because there is no parallelism, but do not attempt to use this in real life, because it will probably break very badly ... and also because there is no reason to do that.


More Query from same tag