score:8
Accepted answer
You are looking for the scan combinator.
List(1,4,5,6,6,7,9).scanLeft(0)(_ + _)
res1: List[Int] = List(0, 1, 5, 10, 16, 22, 29, 38)
Remove the leading element with tail if you want I am not aware of a version of scan that does not take an initial value. The complexity is O(n) for this guy and you could implement it yourself with folding the list via an accumulator and a list (that contains past accumulators). The latter you take as a result.
score:4
@uberwatch's answer is the right one, but for the sake of completeness, here's the more "generic" functional solution using foldLeft
:
val xs = Vector(1,4,5,6,6,7,9)
val (sumList, sum) =
xs.foldLeft((Vector.empty[Int], 0)) {
case ((list, total), x) =>
val newTotal = x + total
(list :+ newTotal, newTotal)
}
// sumList: Vector(1, 5, 10, 16, 22, 29, 38)
// sum: Int = 38
Source: stackoverflow.com
Related Query
- Scala partial sum with current and all past elements in the list
- traverse list in scala and group all the first elements and second elements
- Scala creating a list with some data at specific indices and 0 at all the rest indices
- How to get the sum of two elements in a list of Arrays in Scala
- Transform a Scala Stream to a new Stream which is the sum of the current element and the previous element
- Sum of int elements in list and vector using single function in Scala
- Scala groupBy all elements in the item's list
- Skip the current row COUNT and sum up the other COUNTS for current key with Spark Dataframe
- Scala sum of all elements in the leaves of a tree using pattern matching
- Scala create a List with Future in a for loop and check the result of each Future
- How to get the current and next element of List of list of options in scala
- i want to join two datasets with rdd in spark scala and save results to file. It was executing in all nodes but the last task running forever
- How to define case class with a list of tuples and access the tuples in scala
- Compare the Elements of List and creating a [key value Pairs or Maps] based on logic in Scala
- Scala : How to group by key and sum the values up in scala and return the list in expected return type
- How to do sum on the elements from list of Objects in Scala in play html template?
- Max subsequence sum in the array with no two adjacent elements in Scala
- Getting the sum and pick the max key value pair with out parallelize in spark scala
- Get a list of all directories and a list of all files on the computer with Scala.
- What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala alternatives?
- Scala Get First and Last elements of List using Pattern Matching
- How to return all positives and the first negative number in a list using functional programming?
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- What is Scala way of finding whether all the elements of an Array has same length?
- How can I idiomatically "remove" a single element from a list in Scala and close the gap?
- Finding elements in a scala list and also know which predicate has been satisfied
- Scala enumerations with Singleton objects as enumeration elements and a possibility to iterate over them?
- Zip elements with odd and even indices in a list
- Scala: Calculating the Moving Sum of a List with a fixed window
- How can one list all csv files in an HDFS location within the Spark Scala shell?
More Query from same tag
- How to map files in IntelliJ?
- scala: generating tuples from a list
- scala help need to understand AssociationRules creation
- Import does not bring implicits in scope
- play-json format macro fails for case classes with genericly typed properties
- Scala map to HashMap
- Recursive value needs type - Scala Swing
- How to convert a dataframe or RDD to a List of Tuples?
- How to add new field to struct column?
- Why my Scala imperative style map creation snippet is slower than Java one?
- Scalameta Decl.Def not works on a trait def method
- Regex on Spark RDD[String] with Regex on multiline
- sbt: publish to corporate Nexus repository unauthorized
- sbt-native-packager scala play dockerfile not running with default config
- Storing values obtained from for each loop Scala
- Calculating differences of subsequent elements of a sequence in scala
- org.dbunit.database - junit.framework.ComparisonFailure - table ordering is different every run
- Program never ends when use other implementation of Execution Context for Futures
- Sending a PoisonPill to an Actor in Java
- FSM actor is not firing onTransition after goto to the same state
- How do I get sbt behind a corporate firewall?
- How to increment counter in Gatling?
- FP or typelevel tools to groupBy on deep leaf in nested data?
- Have Scalatra exit if LifeCycle added with Context.setInitParameter throws exception
- Define spark udf by reflection on a String
- Spark Scala Jaas configuration
- Log timestamp of an exception in Play?
- Explode Array[(Int, Int)] column from Spark Dataframe in Scala
- Scala: determine method result type for use in generics
- missing parameter type on Scala df Iterator