score:4
a verbose scala version of your sumconcurrently,
def sumconcurrently(numbers: list[int]): future[int] = {
val (v1, v2) = numbers.splitat(numbers.length / 2)
for {
sum1 <- future(v1.sum)
sum2 <- future(v2.sum)
} yield sum1 + sum2
}
a more concise version
def sumconcurrently2(numbers: list[int]): future[int] = numbers.splitat(numbers.length / 2) match {
case (l1, l2) => future.sequence(list(future(l1.sum), future(l2.sum))).map(_.sum)
}
and all this is because we have to partition the list. lets say we had to write a function which takes a few lists and returns the sum of their sum's using multiple concurrent computations,
def sumconcurrently3(lists: list[int]*): future[int] =
future.sequence(lists.map(l => future(l.sum))).map(_.sum)
if the above looks cryptic... then let me de-crypt it,
def sumconcurrently3(lists: list[int]*): future[int] = {
val listoffuturesofsums = lists.map { l => future(l.sum) }
val futureoflistofsums = future.sequence(listoffuturesofsums)
futureoflistofsums.map { l => l.sum }
}
now, whenever you use the result of a future
(lets say the future completes at time t1
) in a computation, it means that this computation is bound to happen after time t1
. you can do it with blocking like this in scala,
val sumfuture = sumconcurrently(list(1, 2, 3, 4))
val sum = await.result(sumfuture, duration.inf)
val anothersum = sum + 100
println("another sum = " + anothersum)
but what is the point of all that, you are blocking
the current thread while for the computation on those threads to finish. why not move the whole computation into the future itself.
val sumfuture = sumconcurrently(list(1, 2, 3, 4))
val anothersumfuture = sumfuture.map(s => s + 100)
anothersumfuture.foreach(s => println("another sum = " + s))
now, you are not blocking anywhere and the threads can be used anywhere required.
future
implementation and api in scala is designed to enable you to write your program avoiding blocking as far as possible.
score:1
for the task at hand, the following is probably not the tersest option either:
def sumconcurrently(numbers: vector[int]): future[int] = {
val (v1, v2) = numbers.splitat(numbers.length / 2)
future(v1.sum).zipwith(future(v2.sum))(_ + _)
}
as i mentioned in my comment there are several issues with your example.
Source: stackoverflow.com
Related Query
- Java CompletableFuture is more concise and faster then Scala Future
- What are the differences between a Scala Future and a Java Future
- Scala Futures and java 8 CompletableFuture
- Scala and Java interop. of Future
- Is Scala faster than Java 7 for number crunching and for heavy string processing?
- Convert Scala Future and Promise to Java 8 ComletableFuture
- More functional and concise way to write this in Scala
- Converting Java CompletableFuture to a Scala Future
- What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala alternatives?
- Samples of Scala and Java code where Scala code looks simpler/has fewer lines?
- What are the differences and similarties between Scala traits vs. Java 8 interfaces?
- On Performance and Java Interoperability: Clojure vs. Scala
- Simple and concise HTTP client library for Scala
- Nashorn and Scala future to JS Promise conversion
- Java <-> Scala interop: transparent List and Map conversion
- Scala and Java BigDecimal
- Convert a Java Future to a Scala Future
- Compatibility between Scala closures and Java 8 closures
- Difference between Java Optional and Scala Option
- Simultaneous Java and Scala development within the same project
- Maven: mixing Java and Scala in one project
- Mixing Scala and Java files in an Eclipse project
- One Play 2 Framework App - use both java and scala
- Convert scala future to java future
- About generics in Java and Scala
- Scala and Java in same project
- Scala and the Java Memory Model
- How to set up Java and Scala in one single Module in Intellij IDEA 11?
- Best practices with Akka in Scala and third-party Java libraries
- Comparing Scala and Java Double.NaN
More Query from same tag
- Play Framework Stop responding after 2-3 days
- Use singletons in akka scala actor
- How to make an admin panel using Play framework 2.0/2.1
- How to skip tests in certain jenkins builds?
- How to package JARs in Spark (Scala) outside of a project?
- MiMa complains about loss of a synthetic method, even though I put the original line back in place
- fail to create a string with inner properties
- Spark's Column.isin function does not take List
- In Spark word count RDD, how to index value for a specific key
- play framework how to reuse and extend a form mapping
- implement triggers in Slick
- Is there any bulkUpdate (similar to bulkDelete_!!) in Mapper?
- Irregularities in the Scala class hierarchy
- GC Overhead Limit Exceeded for Large CSV Reads in Scala
- Convert scala byte to java byte when using containers
- ML Pipeline for Spark Scala
- Location of Scala: MatchError
- com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [int <-> java.lang.Long]
- Scala "match" help
- Play 2.5.3: Cryptic error message: Exception caught in Netty java.lang.NoClassDefFoundError
- How to shade a dependency for a non-executable Scala Library?
- Add image to Scalatest HtmlReporter
- Scala shapeless function corresponding to the vanilla "zipped" on regular lists?
- Changing Scala lazy val behaviour
- k-means Clustering geolocated data using Spark/Scala
- Java Object serialization in scala
- Type mismatch error in Scala. found : Long, required: String
- What are the advantages of increasing the partition size and decreasing partitions number in spark?
- How to make a sbt task use a specific configuration scope?
- scala varargs (Any*) wrapped twice