Scala Futures, Traverse and Side Effects

Tags: , , , , , ,

Scala Futures, Traverse and Side Effects

Scala Futures, Traverse and Side Effects

A month ago I was developing some virtual file system. My code had should read a config file, take a path and create all folders from that path. I had an asynchronous API for that purpose. My code was like this: val list = ( “/a”, “/a/b”, “/a/b/c”, “/a/b/c/d”, “/a/b/c/d/e” ) def isDirExists(str: String): Future[Boolean] = ??? def createDir(str: String): Future[Unit] = ??? Future.traverse(list){dir => isDirExists(dir).flatMap{ isExists => if (isExists) Future.successful(()) else createDir(dir) } } The list consists of paths of folders to be sequentially created. The folder creation starts from the most upper folder. If it doesn’t exist it

Continue Reading

Scala.js With Monix Example

Scala.js With Monix Example

One of the annoying thing in front-end development is managing long calculations in a browser. JavaScript does things synchronously, and when you want to do something, that needs time for execution, JavaScript resists. Here is an example of such long calculation. At first, I shall demonstrate synchronous code, then asynchronous. Scala has the great library Monix, that works well with Scala.js. I have already told about Monix in some of my previous posts. Of cause, I’m going to use this library in my asynchronous example. My son goes to school. One time he was doing his math homework and he

Continue Reading

Scala.js and unidirectional data flow 2

Scala.js and unidirectional data flow 2

Last time we talked about using Monix as a tool of creating unidirectional data flow in Scala.js applications. The tiny Dispatcher class was made for that purpose. We considered the Counter example, which demonstrated all the power of this approach. Today I intend to widen our horizons and to consider another example: more complex one: which will demonstrate not only usage of the Dispatcher but also usage of Sortable.js with React. But before that I want to add some new methods to the Dispatcher class. Here is the full code of the Dispatcher class: import monix.execution.Ack import monix.execution.Ack.Continue import monix.reactive.{Observable,

Continue Reading

Scala.js and unidirectional data flow

Scala.js and unidirectional data flow

During the last two years the one of the most popular subjects for discussion in the JavaScript society  is so-called “unidirectional data flow”. Those happy years, when we used JavaScript for organizing of some effect on the page or for loading some widget by Ajax, gone to the past. Today JavaScript is used for building client-side web applications. There are several frameworks for doing such things. I used React in my last projects. It’s a great library. It allows doing everything you want. Except unidirectional data flow. You have to use Flux or Redux for that purpose. But what about

Continue Reading