scala

Tutorials about the Scala programming language.

Notes on Scala for expressions, map, flatMap, and Option alvin March 10, 2017 - 1:12pm

Without any explanation, these are some of my working notes from my upcoming book on Scala and Functional Programming about a) for expressions, b) map, c) flatMap, d) Option, and e) nested flatMap and map calls.

These are equivalent (map and for)

this:

val y = x.map(_ * 2)

and this:

Scala: How to fill/populate a list (same element or different elements)

As a quick note, if you ever need to fill/populate a Scala list with the same element X number of times, a simple solution is to use the fill method, like this:

scala> val x = List.fill(3)("foo")
x: List[String] = List(foo, foo, foo)

If you want to populate a list with different element values, another approach is to use the tabulate method:

Strategic Scala Style: The Principle of Least Power alvin March 1, 2017 - 5:52pm

Li Haoyi has a nice blog post titled, Strategic Scala Style: Principle of Least Power.

A simple Akka (actors) remote example

While doing some crazy things with SARAH, I realized that the best way to solve a particular problem was to use remote Akka actors. I haven’t had the opportunity to work with Akka much since finishing the Scala Cookbook, so I dug around trying to find a simple Akka remote “Hello, world” example. Unable to find a good one, I read some stuff, and created it myself.

Researching the use of an IO Monad in Scala

As I was researching who might be using an “IO Monad” in Scala, I found this quote from Martin Odersky in the Google Group titled “scala-debate”:

“The IO monad was a neat trick for combining side effects with lazy evaluation ... there is only one lazily evaluated language in wide usage today and even its creators have said that laziness was probably a mistake. Strict languages don’t need the IO monad, and generally don’t have it, even though they could. Bob Harper’s posts in his ‘existential type’ series are a good explanation on why not.”

Here’s a link to Bob Harper’s The Point of Laziness article.