scala

Tutorials about the Scala programming language.

A “Minority Report” Monte Carlo simulation in Scala alvin June 9, 2017 - 8:17am


“The Precogs are never wrong. But occasionally they do disagree.”
~ Minority Report
 

This article shares the source code for a Monte Carlo simulation that I wrote in Scala. It was inspired by the movie Minority Report, as well as my own experience.

Background

For the purposes of this simulation, imagine that you have three people that are each “right” roughly 80% of the time. For instance, if they take a test with 100 questions, each of the three individuals will get 80 of the questions right, although they may not get the same questions right or wrong. Given these three people, my question to several statisticians was, “If two of the people have the same answer to a given question, what are the odds that they are correct? Furthermore, if all three of them give the same answer to a question, what are the odds that they are right?”

A sample .gitignore file for Scala/SBT projects alvin June 5, 2017 - 2:15pm

This is a sample .gitignore file that I use for Scala SBT projects:

How to use ScalaCheck in the SBT console alvin June 5, 2017 - 9:05am

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.)

Learning Functional Programming in Scala (pdf)

Table of Contents1 - What’s new2 - Buying the book3 - One more thing4 - Preview5 - Some time in the future ...

My new book, Learning Functional Programming in Scala, is now available as a PDF you can purchase for just $10 US. Details are listed below.

Back to top

What’s new

If you’ve been following along with me recently, the latest changes are:

STRef, or StateRef (using a State monad)

Here’s a link to a page by James Earl Douglas that I don’t quite understand yet, but also don’t want to forget. Here’s his intro to the problem, and then the image shows his solution.

Problem: You have a mutable variable (a var in Scala) that is both read from and written to outside of a tightly-scoped block.

Solution: Remodel the block as functions that take an initial value of the variable, and return both the final value of the variable and the original return value of the expression.

Scalameta: Meta programming (reflection) for Scala

If you’re interested in “meta” programming in Scala, check out the Scalameta project. It’s described on its website like this:

“Scalameta is a clean-room implementation of a metaprogramming toolkit for Scala, designed to be simple, robust and portable. We are striving for scalameta to become a successor of scala.reflect, the current de facto standard in the Scala ecosystem.”

“Scalameta provides functionality that's unprecedented in the Scala ecosystem. Our killer feature is abstract syntax trees that capture the code exactly as it is written — with all the original formatting and attention to minor syntactic details.”