score:34
the reason to use sliding
instead of grouped
is really only applicable when you want to have the 'windows' be of a length different than what you 'slide' by (that is to say, using sliding(m, n)
where m != n
):
listtogroup.sliding(2,3).tolist
//returns list(list(1, 2), list(4, 5), list(7, 8))
listtogroup.sliding(4,3).tolist
//returns list(list(1, 2, 3, 4), list(4, 5, 6, 7), list(7, 8))
as som-snytt points out in a comment, there's not going to be any performance difference, as both of them are implemented within iterator
as returning a new groupediterator
. however, it's simpler to write grouped(n)
than sliding(n, n)
, and your code will be cleaner and more obvious in its intended behavior, so i would recommend grouped(n)
.
as an example for where to use sliding
, consider this problem where grouped
simply doesn't suffice:
given a list of numbers, find the sublist of length 4 with the greatest sum.
now, putting aside the fact that a dynamic programming approach can produce a more efficient result, this can be solved as:
def maxlengthfoursublist(list: list[int]): list[int] = {
list.sliding(4,1).maxby(_.sum)
}
if you were to use grouped
here, you wouldn't get all the sublists, so sliding
is more appropriate.
Source: stackoverflow.com
Related Query
- Is the Scala 2.8 collections library a case of "the longest suicide note in history"?
- Difference between object and class in Scala
- Scala vs. Groovy vs. Clojure
- Where does Scala look for implicits?
- Scala list concatenation, ::: vs ++
- What is the formal difference in Scala between braces and parentheses, and when should they be used?
- Difference between a Seq and a List in Scala
- Understanding implicit in Scala
- What are Scala context and view bounds?
- Difference between method and function in Scala
- Scala Programming for Android
- Appending an element to the end of a list in Scala
- Case objects vs Enumerations in Scala
- Scala 2.8 breakOut
- difference between foldLeft and reduceLeft in Scala
- What do <:<, <%<, and =:= mean in Scala 2.8, and where are they documented?
- Spark performance for Scala vs Python
- What Scala web-frameworks are available?
- Scala equivalent of Java java.lang.Class<T> Object
- Logging in Scala
- Scala best way of turning a Collection into a Map-by-key?
- What is the Scala identifier "implicitly"?
- How to read environment variables in 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 vs java, performance and memory?
- Why does the Scala compiler disallow overloaded methods with default arguments?
- Use of def, val, and var in scala
- Understanding what 'type' keyword does in Scala
- Difference between Array and List in scala
- What are type lambdas in Scala and what are their benefits?
More Query from same tag
- How to store a pointer to "pick up where you left off" while iterating in a spark scala dataframe?
- Writing into the existing directory Dataframe using partitionBy
- sbt throws error while launching it
- How to parse json with a single field in Playframework2?
- Debugging json4s read deserialization errors
- Chess programming, Scala, JVM: How costly is dynamic dispatch?
- How to write the function isFunction in scala?
- How can I use IntelliJ IDEA 10.5.2 to connect to a database for a Scala application
- generating join condition dynamically in spark/scala
- Unable to start Spray with AspectJWeaver
- Why does %AddJar command in Bluemix give an error?
- Finding the maximum sequence in a given range - Spark/Scala
- Using specs2 and FakeApplication() to test database fails evolution inserts
- Leave off underscore in function literal?
- Get ID names of documents using casbah
- Netty https ( TLS ) Session duration: why is renegotiation needed?
- Slick 3.0-RC3 fails with java.util.concurrent.RejectedExecutionException
- how can I connect a jar file to commits of the source code?
- Using spark context outside main
- Boolean return prooblem scala
- Apply functions on each group in Data Frame
- Decode Case class with nested Coproduct by discriminator
- Java/Scala remote HDFS usage
- Scala keep unique values in list
- How to return the original collection type from generic method
- How to update multiple fields in a row in phantom-dsl?
- Returning List[Double], Map[String, Double] from a list of Doubles
- Scala compile time error: No implicits found for parameter evidence$2: BodyWritable[Map[String, Object]]
- Reading a zst archive in Scala & Spark: native zStandard library not available
- Akka remote in docker container