background preloader

Effective Scala

Effective Scala
Table of Contents Other languages 日本語Русский简体中文 Introduction Scala is one of the main application programming languages used at Twitter. Much of our infrastructure is written in Scala and we have several large libraries supporting our use. Scala provides many tools that enable succinct expression. Above all, program in Scala. This is not an introduction to Scala; we assume the reader is familiar with the language. This is a living document that will change to reflect our current “best practices,” but its core ideas are unlikely to change: Always favor readability; write generic code but not at the expensive of clarity; take advantage of simple language features that afford great power but avoid the esoteric ones (especially in the type system). And have fun. Formatting The specifics of code formatting — so long as they are practical — are of little consequence. This is of particular importance to Scala, as its grammar has a high degree of overlap. Whitespace Indent by two spaces. Naming to Use

Scala School Other Languages: About Scala school started as a series of lectures at Twitter to prepare experienced engineers to be productive Scala programmers. Approach We think it makes the most sense to approach teaching Scala not as if it were an improved Java but instead as a new language. Most of the lessons require no software other than a Scala REPL. Also You can learn more elsewhere:

Learn Getting Started Install Scala on your computer and start writing some Scala code! Dive straight into the API. Tutorials Digest bite-size pieces of the essentials. Online Learning There are a few interactive resources for trying out Scala, to get a look and feel of the language: Functional Programming Principles in Scala, free on Coursera. Books There are more and more books being published about Scala. Bleeding Edge If you are interested in finding out about the hottest, most pressing issues of tomorrow in the Scala world, have a look at the Scala Improvement Process (SIP) page. Older Documentation The documentation below may be a bit outdated, but provides insights into the (historical) design principles of the language : Brief Scala Tutorial: a 20 page introduction to scala and some of the basic concepts and a good place to start.

Programming in Scala, First Edition Programming in Scala, First Editionby Martin Odersky, Lex Spoon, and Bill VennersDecember 10, 2008 Martin Odersky made a huge impact on the Java world with his design of the Pizza language. Although Pizza itself never became popular, it demonstrated that object-oriented and functional language features, when combined with skill and taste, form a natural and powerful combination. Pizza's design became the basis for generics in Java, and Martin's GJ (Generic Java) compiler was Sun Microsystem's standard compiler starting in 1.3 (though with generics disabled). Since that time, we at Sun tried to simplify program development by extending the language with piecemeal solutions to particular problems, like the for-each loop, enums, and autoboxing. Lately, there has been a backlash against statically typed languages. Scala is a tastefully typed language: it is statically typed, but explicit types appear in just the right places. Will Scala be the next great language? Who should read this book

Learning Scala in small bites Learning with the REPL One of the useful features for learning Scala is its REPL (read-eval-print-loop) support. If you want to try something out in Scala, just run: % scala and then try it out at the prompt. Examples Each well-commented script below demonstrates a different facet of Scala. [Values.scala] #! /* Scala has a rich set of value types, and a rich literal syntax to support them // Integers:val anInt = 3 // Floating point:val aDouble = 4.0 // Charaters:val aCharacter = 'c' // Strings:val aString = "Google" // Symbols:val aSymbol = 'foo // XML:val anXMLElement = <a href=" // Tuples:val aPair = (aString,aDouble) // Lists:val aList = List(1,2,3,4) // Ranges:val aRange = 1 to 5 // Maps:val aMap = Map(3 -> "foo", 4 -> "bar") // Sets:val aSet = Set(8,9,10) // Arrays:val anArray = Array(1,2,3,5) // Unit:val unit = () // Null:val nullValue = null // Functions:def incImplicit(x : Int ) = x + 1 val incAnonymous = ((x : Int) => x + 1) [Functions.scala] // f(x) => f.apply(x)

Real-World Scala: Dependency Injection (DI) ← Posted: 2008-10-06 In this second post in the Real-World Scala series I am going to discuss how to implement/achieve Depenency Injection (DI) in Scala. Scala is a very rich and deep language that gives you several ways of doing DI solely based on language constructs, but nothing prevents you from using existing Java DI frameworks, if that is preferred. Using the Cake Pattern The current strategy we are using is based on the so-called Cake Pattern. Note: I will try to explain things in steps which I refactor towards the final version (this is only to help with the understanding), so please wait with yelling: “This sucks!” First, let’s create a UserRepository (DAO) implementation. Here we could have split up the implementation in a trait interface and its implementation, but in order to keep things simple I didn’t see the need. Now let’s create a user service (also a dummy one, merely redirecting to our repository). Here you can see that we are referencing an instance of the UserRepository.

Testing Akka with Specs2 This year I have been working on several systems based on Akka, usually in combination with the excellent Spray framework to build fully asynchronous actor-driven REST servers. All in all this has been going very well but recently I had to reinvent a certain wheel for the third time (on the third project) so I thought it might be good to blog about it so others can benefit from it too. The problem is very simple: Akka has great unit test support but unfortunately (for me) that support is based on ScalaTest. Now there is nothing wrong with ScalaTest but personally I prefer to write my tests using Specs2 and it turns out that mixing Akka TestKit with Specs2 is a little tricky. The Akka documentation does mention these problems and it gives a brief overview of ways to work around them, but I could not find any current example code online. Based on that original example, here is my slightly improved and commented version:

ScalaTest Scala Tests with IntelliJ Idea, Maven and ScalaTest Why you are reading this: you want to be able to run unit tests in IntelliJ for your Scala project, using either the built-in support for Scalatest and/or Maven goals. The tools discussed are version sensitive so be careful… Putting together a Scala 2.9.1 project in IntelliJ Idea 11.1.1 to use Maven 3.0.4 and Scalatest 1.7.1 was painful. I’m going to go through things I did and problems I ran into chronologically. 3. The dependencies section between lines 11-29 tell Maven what to include in our project, this is a very basic implementation using only a tiny fraction of the settings available. The build section between lines 31-46 tell Maven what our project structure is, along with any plugins we will be using. 4. This test class exists in the location described by the <testSourceDirectory> tag we defined in the Maven POM. 5. There is an easy way to run Scalatest tests in the IntelliJ Idea, put focus on the test class by clicking in the source of the class and push CTRL+SHIFT+F10.

Related: