Transform a Scala Stream to a new Stream which is the sum of the current element and the previous element
问题 How to transform a Scala Stream of integers so that we have a new Stream where the elements are the sum of this element and the previous element. By example if the input stream is 1, 2, 3, 4 ... then the output stream is 1, 3, 5, 7. Also a second question, how would you make the sum use the previous one in the output stream so that the output would be 1, (2+(1)), (3+(2+1)), (4+(3+(2+1))). 回答1: Just zip your stream with a shifted version of itself and sum the two elements. val s1 = Stream.from