signaturefoki.blogg.se

Scala foreach
Scala foreach




The code below shows how to loop through all elements in the donut sequence using the foreach method. Step 1: How to initialize a Sequence of donutsĮlements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)Ģ. How to loop through all the elements in the sequence using the foreach function You should see the following output when you run your Scala application in IntelliJ: Val donuts: Seq = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") Println("Step 1: How to initialize a Sequence of donuts") The code below shows how to initialize a sequence of donut elements where each element in the sequence is of type String.

scala foreach scala foreach

It also provides a discussion of how a for expression is translated into a flatMap operation.1.

scala foreach

If you’re using multiple futures to yield a single result, combine them in a for expressionĪlso, as you saw with the import statements in these examples, the Scala Future depends on an ExecutionContext.įor more details about futures, see Futures and Promises, an article that discusses futures, promises, and execution contexts.The value inside a Future is always an instance of one of the Try types: Success or Failure.You handle the result of a future with callback methods like onComplete and andThen, or transformation methods like filter, map, etc.When you work with futures you don’t have to concern yourself with the low-level details of thread management.A benefit of futures over threads is that they work with for expressions, and come with a variety of callback methods that simplify the process of working with concurrent threads.A future starts running as soon as you construct it.Futures are intended for one-shot, potentially long-running concurrent tasks that eventually return a value they create a temporary pocket of concurrency.You construct futures to run tasks off of the main thread.To summarize, a few key points about futures are: Hopefully those examples give you an idea of how Scala futures work. Val res1: concurrent.Future = Future(Success(4)) When you run that application, you see output that looks like this: " ) // important for a little parallel demo: keep the jvm alive sleep ( 3000 ) Import import .global import scala.util. To demonstrate how this works, let’s start with a Future example in the REPL.įirst, paste in these required import statements: An example in the REPLĪ future is used to create a temporary pocket of concurrency.įor instance, you use a future when you need to call an algorithm that runs an indeterminate amount of time-such as calling a remote microservice-so you want to run it off of the main thread. While an actor may live forever, a future eventually contains the result

scala foreach

When you think about futures, it’s important to know that they’re intended as a one-shot, “Handle this relatively slow computation on some other thread, and call me back with a result when you’re done” construct.Īs a point of contrast, Akka actors are intended to run for a long time and respond to many requests during their lifetime. You’ll also see examples of methods that are used to handle the value in a future once it returns. In this chapter you’ll see how to use futures, including how to run multiple futures in parallel and combine their results in a for expression. sleep ( 500 ) 42 val x = aShortRunningTask () println ( "Here" )Ĭonversely, if aShortRunningTask is created as a Future, the println statement is printed almost immediately because aShortRunningTask is spawned off on some other thread-it doesn’t block.






Scala foreach