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