scala

Tutorials about the Scala programming language.

“Monad transformers are not too intuitive” alvin September 19, 2017 - 9:50am

“Monad transformers are not too intuitive, especially in Scala, and are known to produce hard to understand code structure.”

~ Debasish Ghosh, Functional and Reactive Domain Modeling

A look at how exceptions work with Scala Futures and the onComplete ‘Failure’ case alvin September 18, 2017 - 12:53pm

Here’s a little example of how exceptions work with Scala Futures, specifically looking at the onComplete ‘Failure’ case.

In this example I start three Futures that run for different lengths of time, and the shortest-running Future throws an exception:

Version 0.1.4 of “Learning Functional Programming in Scala” alvin September 12, 2017 - 8:35pm

Version 0.1.4 of “Learning Functional Programming in Scala” is now available. The latest changes are:

- 13 new chapters on the StateT monad and monad transformers
- Ten new chapters on Domain Modeling
- Two new chapters on ScalaCheck
- New appendices on Anonymous Functions, and using def vs val in traits
- The PDF now has small 113 chapters, four appendices, and is 983 pages long

To accompany this latest update, the book is on sale for a few days.

The next release of Learning Functional Programming in Scala alvin September 10, 2017 - 5:19pm

The next release of Learning Functional Programming in Scala should be available by the end of the day (my day here in Colorado) Tuesday, September 12, 2017. If I add everything in, it will contain over 30 new lessons.

Scala type aliases (syntax, examples) alvin August 26, 2017 - 3:35pm

In Scala you can declare a type alias. Typically you do this to create a simple alias for a more complex type.

Using a type alias to simplify a complex type

For example, on this page I note a good example where someone on StackOverflow first defined a type alias named Row, and then created a second type alias named Matrix as a list of rows:

Fun with Scala functions (andThen and compose)

Here’s a little fun with Scala functions, including the use of andThen and compose:

scala> val add1 = (i: Int) => i + 1
add1: Int => Int = <function1>

scala> val double = (i: Int) => i * 2
double: Int => Int = <function1>

scala> val addThenDouble = add1 andThen double
addThenDouble: Int => Int = <function1>

scala> addThenDouble(1)
res0: Int = 4

scala> val doubleThenAdd = add1 compose double
doubleThenAdd: Int => Int = <function1>

scala> doubleThenAdd(1)
res1: Int = 3

(Inspired by the book, Functional and Reactive Domain Modeling, and my own book, Learning Functional Programming in Scala.)