Posts in the “scala” category

How to populate sample data when a Play Framework application starts up

If you ever need an example of how to do something when a Play Framework application starts up, check out the Zentasks application in the Play distribution samples directory. You'll find the following code in the Global.scala file in the app subdirectory of the project. It shows how to use the onStart method of the Global object to populate some sample data when the Play Framework app starts up.

Here's the source code for that class:

Generating Play Framework 2 CRUD forms with Cato

Summary: Cato is the name of a language-independent, template-based, database driven “CRUD Generator” I created, and in the video below I show how to use it to create a complete set of Play Framework 2 CRUD forms, including model, controller, view, and Anorm database code.

A Scala String chomp (or chop) function

Scala string FAQ: Does Scala have a String method like chomp or chop that you find in Perl?

If you're using Scala and want a string chomp or chop method that you're used to in languages like Perl to remove end of line characters, the stripLineEnd method will do what you want, as shown in the REPL:

Scala programming jobs

Every once in a while I query the job search engines to see what’s out there these days. I first did this in February, 2013, and have looked at them again every few months since then.

I especially like to look at Dice.com because most of those jobs are for contractors, and when businesses can't find full-time employees with certain skills -- such as new programming languages -- they hire contractors. This is also an indication of new projects that businesses are staffing up for.

Scala "lazy" means "performed on demand"

This blog post makes an interesting point early on. When talking about languages like Scala (or Haskell), the word “lazy” can be perceived in a negative manner. A more accurate way to describe how the lazy keyword in Scala works is to say “on demand,” or “performed on demand.”

A prime number algorithm in Scala (with the Scala Stream class)

Daniel Sobral posted this prime number algorithm in Scala on this SO page way back in 2010:

def primeStream(s: Stream[Int]): Stream[Int] =
    Stream.cons(s.head, primeStream(s.tail filter { _ % s.head != 0 }))
val primes = primeStream(Stream.from(2))

If you’re interested in the output of that algorithm you can paste that code into the Scala REPL, and then invoke it like this:

primes take 10 foreach println

If I have time I’ll write more about this algorithm in the future, but for now I’m digging into the Scala Stream class (I now have a Scala Stream class tutorial), and I just came across this example.

Note: The cons function shown is not defined in the Scala Stream class, but is in the Scala Stream object. The Scaladoc states that it is, “an alternative way of building and matching Streams using Stream.cons(head, tail).”

IntelliJ IDEA - Controlling indentation, tabs, and spaces with the Scala plugin

Note to self: when writing Scala code with IntelliJ IDEA, you can configure the indentation by going to Settings, then Editor, then Code Style, then Scala. The indentation and other code formatting options are under there. For some reason I couldn’t find this by searching the Settings for “indent” and similar phrases.

Lightbend announces Scala Center

Lightbend, formerly Typesafe, announces the create of the Scala Center. You can read about it here, and there is also this video by Martin Odersky:

Some important text from the link: “The new Scala Center, with support from its founding members and advisory board, will fund and sponsor initiatives that develop and maintain open source Scala libraries, create documentation and learning materials and develop various tools and services that benefit the Scala community as a whole. Scala Center is also offering four massive open online courses (MOOC) on Scala this year.”

Scala Native (Scala on LLVM)

Scala Native “is a new ahead-of-time compiler and lightweight managed runtime designed specifically for Scala ... Scala Native is compiled ahead-of-time via LLVM. This means that there is no sluggish warm-up phase that’s common for just-in-time compilers. Your code is immediately fast and ready for action.”

For more information, there’s a little bit of a story about it on JavaWorld.com, and this thread on ycombinator.com.

Back to work on the Scala/FP book

After almost a month’s absence, I finally got to work on my Scala/FP book again yesterday, and I have to say, it felt good to get back to writing and editing. Because of the health issues I have no timetable for the release of the book, but I’m thinking about releasing the first 5-10 chapters as a free PDF to gauge the interest in it.