A Task[T]
represent a chunk of work that yields a value of type T
on completion.
Tasks allow fine control over what threads run the task and with what level of concurrency. They also allow the separation
of the definition of work (when tasks are created and chained together) from its execution (when tasks are run and any
side-effects occur). In this way, they play a similar role to the IO
wrappers of Haskell, Cats-Effect or Scalaz.
We will use Monix 3.x Tasks.
We choose Monix over Cats Effect IO because Monix offers a better default Execution Model for Tasks and more options than Cats-Effect does, and is relatively similar in other aspects.
We choose Monix tasks over Scala's default Futures because of Monix's Execution Model has more control and much more performant defaults than Scala Futures.
- In the classic version, method
scanReport
returnedString
. What is its return type now? What about methodpathScan
?
-
The top level
main
still returnsUnit
. It builds a program, a chain ofTask
s, but they'll have no effect until they're run. UserunSyncUnsafe(1.minute)
to run the tasks. The word Unsafe in its name indicates that it will cause side-effects. The idea with Tasks is to run them at only one at the top of your top program. -
There is a compile error in
pathScan
. Themap
method no longer has the correct type, because we want to traverse a list of subdirectories, converting each into aTask
yielding their scan, and then roll the traversal into a single overall Task. Replacemap
withtraverse
, which is "like map, but for mapping to effectful values" -
One more will appear problem. Tasks can be run asynchronously, so they need an implicit
Scheduler
available to dispatch them to. Addimplicit val s = Scheduler(ExecutionModel.BatchedExecution(32))
at the top ofScanner
object. TheBatchedExecution(32)
means that a chain of up to 32 Tasks will be run batched together in one thread before any context switch.
Run the tests to verify your task based implementation still gives the correct output.
- Examine the tests for the Task version. Notice that the introduction of Task, which makes this version purely functional hasn't made the tests any easier to write. Still lots of messy files and directories created and cleaned-up. Why is this and what could you do about it?