1

My scala list contains following elements :

val A = List(12,1,34,34,45,56,7)

now I want to subtract list as below :

  List((12-1),(1-34),(34-34),(34-45),(45-56),(56-7))

so final result will be :

val result = List(11,-33,0,-11,-11,49)

6 Answers 6

5

I think you need sliding (groups elements in fixed size blocks by passing a "sliding window" over them):

A.sliding(2,1).toList.map(x => x(0) - x(1))
5
  • 3
    1 is the default step, so .sliding(2) suffices. Aug 6, 2015 at 6:52
  • 1
    You have a problem with singleton lists. You need either to add a .filter(_.size > 1) before the map, or replace the map by .collect{case List(x,y) => x - y}. Aug 6, 2015 at 15:37
  • 1
    A.sliding(2).map(_.reduce(_-_)).toList
    – jwvh
    Aug 6, 2015 at 16:04
  • @HelderPereira true, also for empty lists (where all other answers also fail) :) In any case, I find Chris Martin answer the most correct one (assuming empty list is a special case which should be handled separately).
    – jarandaf
    Aug 7, 2015 at 6:06
  • @jarandaf Definitively, empty list is a special, as the general rule seems to be final_size = initial_size - 1, it raises the question what to do with empty lists. In most cases, returning empty list will be OK (that's what your solution does), or maybe changing the return type to Option[List[Int]] and return None would make more sense, or if getting an empty list here is the diagnosis of a bigger problem that happened before, maybe we should throw an Exception instead of letting it silently returning an empty list. But that depends on specification and it was not specified by the OP. Aug 7, 2015 at 6:55
3

And let's don't forget zipped:

val A = List(12,1,34,34,45,56,7)
(A,A.drop(1)).zipped.map(_-_)
2

Just to throw one more option into the mix:

(A zip A.drop(1)).map({ case (a, b) => a - b })

This gives you a little bit more type safety than sliding because Tuple2 codifies the fact that the intermediate collection's elements are pairs.

0
1

You could do:

val list = List(12, 1, 34, 45)
list.sliding(2).map {
 case a :: b :: Nil => a - b
}.toList

It should return what you need.

2
  • 1
    You could use collect instead of map to avoid exception on List(12).
    – senia
    Aug 6, 2015 at 7:18
  • Wow, you're correct. I'd expected List(12).sliding(2) to return an empty iterator.
    – ntn
    Aug 6, 2015 at 8:01
0

I'm not impressed by the performance of the zip/map solutions. This could be done with a simple iterator, overriding the next() method. But I prefer streams

def subNeighbours(xs: List[Int]): Stream[Int] = xs match {
  case x :: y :: rest => (x - y) #:: subNeighbours(y :: rest)
  case _ => Stream.Empty
}

(subNeighbours(A)).toList

Inputs and outputs:

  • List(12, 1, 34, 34, 45, 56, 7) => List(11, -33, 0, -11, -11, 49)
  • List(1, 3, 2, 9, 0, 5) => List(-2, 1, -7, 9, -5)
  • List(1) => List()
  • List() => List()

Stream solution only traverses the list once.

1
  • 1
    Actually, this solution is slower than the zip/zipped solutions. Aug 6, 2015 at 16:09
-2

Well, The thinking behind this would be:

for(int i=0; i<list.length-1; i++){
list[i]= list[i]-list[i+1];
}

Now just add in the small litter details like setting result to the final values and you are golden.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.