scala

Tutorials about the Scala programming language.

“Pure functional programming is programming with mathematical functions” alvin August 6, 2017 - 2:13pm

“Pure functional programming is programming with mathematical functions.”

~ Erik Meijer

A Scala method to run any block of code slowly alvin August 5, 2017 - 5:36pm

The book, Advanced Scala with Cats, has a nice little function you can use to run a block of code “slowly”:

def slowly[A](body: => A) = try body finally Thread.sleep(100)

I’d never seen a try/finally block written like that, so it was something new for the brain.

In the book they run a factorial method slowly, like this:

slowly(factorial(n - 1).map(_ * n))

FWIW, you can modify slowly to pass in the length of time to sleep, like this:

def slowly[A](body: => A, sleepTime: Long) = try body finally Thread.sleep(sleepTime)
Scala “lint” tools alvin August 5, 2017 - 5:01pm

As a quick note, I was just looking into the state of Scala “lint” tools, and found ScalaStyle, WartRemover, and Scapegoat.

This 2014 underscore.io post states, “Those interested in FP purity in a Scala world, you’ll want WartRemover.” (Of course that recommendation may have changed by now.) The current ScalaStyle website states, “Scalastyle is used as part of the grading framework for the course Functional Programming Principles in Scala by Martin Odersky on Coursera.”

Scala 2.12: Either is biased, implements map and flatMap

While reading the excellent Scala/FP book, Advanced Scala with Cats, I was just reminded that Scala’s Either class was redesigned in Scala 2.12. Prior to 2.12, Either was not biased, and didn’t implement map and flatMap methods. As the image from the book shows, Either is redesigned in 2.12 to include those methods, so it can now be used in Scala for-expressions as shown.

(I write about biasing in my book, Learning Functional Programming in Scala.)

How to start a Scala REPL session inside SBT (Simple Build Tool)

Scala FAQ: How do I start a Scala REPL session from SBT (i.e., inside the Simple Build Tool)?

Answer: Use console or consoleQuick:

  • Type console to start a REPL session from inside SBT. This (a) compiles your Scala project and then (b) starts a REPL session.
  • Type consoleQuick if you don’t want to compile your project before starting a REPL session inside of SBT.

The console command process looks like this:

> sbt
[info] Loading project definition from /Users/al/Projects/Cats101/project
[info] Set current project to Cats101 (in build file:/Users/al/Projects/Cats101/)

> console
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131).
Type in expressions for evaluation. Or try :help.

scala> _

If you wanted to see how to start a Scala REPL session from inside SBT, I hope this helps.

Amazon Echo + Akka = Akkazon Ekko

Table of Contents1 - Running Ekko2 - Running Ekko commands3 - Listing commands Ekko understands4 - Written with Akka (and Scala)5 - Next steps6 - Add actors with reflection? (plugins)7 - The source code8 - More ...

I recently started working on a project that may or may not make it into my Scala/FP book. I’m currently calling it “Akkazon Ekko” — or “Ekko” — because it’s a little like the Amazon Echo, but written with Scala and Akka.