Scala

Install Scala on Raspberry Pi

Posted in Linux, Programming, Scala on June 4th, 2015 by Pavel – 3 Comments

Raspberry Pi loves ScalaThis article shows how to install and use Scala programming language on Raspberry Pi.

Traditionally, the programming language of choice for Raspberry Pi was Python, while JVM-based languages were set aside. That was reasonable, because JVM platform is rather resource-intensive, especially in interpreted mode, and the first version of Raspberry Pi was hardly apt for such a task.

However, things have changed: on the one hand, Raspberry Pi 2 now offers a 900MHz quad-core CPU and 1GB of RAM, on the other hand, Oracle released JDK 8 for ARM (with HardFP, JIT and server VM), which provides >10X performance boost (comparing to Zero VM from OpenJDK), so Scala runs nicely even on Raspberry Pi 1 (and, RPi 2 can run IntelliJ IDEA, if you wish).

Contents:

  1. Installing Java
  2. Installing Scala
  3. Using Scala
  4. Installing SBT
  5. Using SBT

Some of the commands will require root privileges, so either login as root user or use sudo to start an interactive shell via sudo -i.
Continue reading →

Scala Collections Tips and Tricks

Posted in Programming, Scala on March 14th, 2015 by Pavel – 32 Comments

Library bookshelfThis article presents a list of simplifications and optimizations of typical Scala Collections API usages.

Some of the tips rest upon subtle implementation details, though most of the recipes are just common sense transformations that, in practice, are often overlooked.

The list is inspired by my efforts to devise practical Scala Collections inspections for the IntelliJ Scala plugin. We’re now in process of implementing those inspections, so if you use the plugin in IDEA, you’ll automatically benefit from static code analysis.

Nevertheless, the recipes are valuable by themselves and can help you to deepen your understanding of Scala Collections and to make your code faster and cleaner.

Update:
If you feel adventurous, you can learn how to contribute to IntelliJ Scala plugin and try your hand at “up for grabs” collection inspections that are to your liking.

The article is also (independently) translated into Russian and Chinese (one more translation).

Contents:

  1. Legend
  2. Composition
  3. Side effects
  4. Sequences
    4.1. Creation
    4.2. Length
    4.3. Equality
    4.4. Indexing
    4.5. Existence
    4.6. Filtering
    4.7. Sorting
    4.8. Reduction
    4.9. Matching
    4.10. Rewriting
  5. Sets
  6. Options
    6.1. Value
    6.2. Null
    6.3. Processing
    6.4. Rewriting
  7. Maps
  8. Supplement

All the code examples are available as a GitHub repository.
Continue reading →

Design Patterns in Scala

Posted in Programming, Scala on October 11th, 2013 by Pavel – 44 Comments

Scala puzzleThis article shows how Scala adopts and transforms the classical software design patterns.

The content of the article is also (independently) translated into Russian and Chinese.

Design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished code, but rather a template for how to solve a problem that can be used in many different situations.

Patterns are formalized best practices of effective design, which helps to prevent subtle issues, improve code readability and speed up the development process.

The classical design patterns (mostly, the GoF patterns) are object-oriented. They show relationships and interactions between classes and objects. These patterns are less applicable in pure functional programming (see Haskell’s Typeclassopedia and Scalaz for “functional” design patterns, kind of), however, since Scala is an object-functional hybrid language, these patterns are still very relevant, even in “functional” Scala code.

Sometimes, design patterns may be a sign of some missing feature in a programming language. In such cases, the patterns might be simplified or eliminated when a programming language provides the required features. In Scala, it’s possible to implement most classical design patterns directly, relying on the expressive language syntax.

While Scala may employ additional, language-specific design patterns, this article focuses on the well-known, classical patterns, because these patterns also serve as the means of communication between developers.

Sure we can use the Force all the power of Scala to completely transcend all these patterns, but in this article I intentionally tried to avoid more advanced techniques in favor of more simple and pictorial ones, in order to provide a clean mapping between languages.

Creational patterns:

Structural patterns:

Behavioral patterns:

So, here goes the patterns (all the code is available as a GitHub repository).
Continue reading →

ToyIDE

Posted in Programming, Scala, Software on October 22nd, 2011 by Pavel – Comments Off on ToyIDE

ToyIDE LogoToyIDE is an imitation of a full-featured IDE plus toy languages (imperative, functional) with complete IDE support, interpreters and compilers. All the parts were built from scratch in Scala, without relying on any existing code or libraries.

The project is purely educational. It is a product of a long vacation in the country and a desire to learn how all this stuff really works.

Although I applied evolutionary design in the development (to uncover the reasons behind architecture), it turns out that many techniques in the code come close to commonly used patterns. I learned a lot from the project and hope it might be useful to the people who want to know more about lexers, parsers, AST, Java bytecode and other similar fun, but tricky things.

Update: Jason Zaugg endorsed the project as “open-source Scala application with simplicity and taste”

Source code is 100% Scala (including ~800 unit tests which run in < 3 seconds).

Download binaries: toyide-1.2.4-bin.zip (7.5 MB)

ToyIDE: Main Window
Continue reading →

Scala for Project Euler

Posted in Programming, Scala on August 21st, 2010 by Pavel – 46 Comments

Scala logo, e^ipi + 1 = 0Project Euler is a collection of interesting computational problems intended to be solved with computer programs. Most of the problems challenge your skills in algorithm design rather than your knowledge of mathematics.

Thanks to short concrete problems and number-only solutions you don’t have to thinker with IDEs, GUI designers, programming libraries and frameworks, so Project Euler is an excellent tool for learning a new programming language or improving your core programming skills.
Continue reading →