How to shuffle (randomize) a list in Scala

As a quick note today, to shuffle a list in Scala, use this technique:


Here’s what this approach looks like in the Scala REPL:

How to use ScalaCheck in the SBT REPL (command line)

If you add ScalaCheck to an SBT project like this:

libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.4" % "test"

it’s only available in the SBT “test” scope. This means that when you start a Scala REPL session inside of SBT with its console command, the ScalaCheck library won’t be available in that scope.

To use ScalaCheck with the SBT console (REPL), don’t use its console command — use test:console instead. A complete example looks like this:

$ sbt

> test:console

scala> import org.scalacheck.Gen.choose

Note that after you type test:console your project may be compiled, so that step may take a few moments.

In summary, use SBT’s console command to start a “normal” Scala REPL inside SBT, and use test:console to start a REPL that you can run tests inside of. (Note that this same advice also applies to using ScalaTest or specs2.)

Evaluating the state and development of Scala alvin December 20, 2016 - 2:17pm

O’Reilly has an article titled Scaling Scala. The article talks about Scala, Akka, Spark, Kafka, Dotty, Cats vs Scalaz, and more.

Scala ‘for’ loop examples and syntax alvin November 27, 2016 - 4:19pm

Besides having a bad memory, I haven’t been able to work with Scala much recently, so I’ve been putting together this list of for loop examples.

This page is currently a work in progress, and as of tonight I haven’t tested some of the examples, but ... if you’re looking for some Scala for loop examples — technically called a for comprehension — I hope these examples are helpful.

ScalaJ-HTTP examples: GET, POST, handling redirects alvin November 22, 2016 - 4:34pm

If you ever need some good ScalaJ-HTTP examples, see the test files in the project, including this HttpBinTest.scala file. That file currently shows a number of good ScalaJ-HTTP examples, including GET, POST, redirect examples with Scala.

See that page for a full list of examples, but for my own use, here are a few of them.