Design Patterns in Scala

Posted in Programming, Scala on October 11th, 2013 by Pavel – 43 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 →


Posted in Programming on January 15th, 2012 by Pavel – 5 Comments

"99" in digital rainThe famous Ninety-Nine Prolog problems (by Werner Hett) transcended their original purpose and became a popular set of exercises for languages other than Prolog.

Solving these puzzles (and comparing your solutions with solutions of others) is a good way to “get a feel” for programing language and to explore idiomatic approaches to particular kind of problems.

Today we can easily find solutions to the problems in almost any wide-spread programming language. However most of the attempts use a single programing language and provide only one solution to each problem.

By providing multiple types of solutions we can enrich our programming “toolbox” and learn pros and cons of various methods. By using several programming languages simultaneously we can easily correlate different programming paradigms and explore limits of language expressive power.

Here goes my take on Ninety-Nine Problems in:

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


Posted in Programming, Scala, Software on October 22nd, 2011 by Pavel – Be the first to comment

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: (7.5 MB)

ToyIDE: Main Window
Continue reading →

SleepArchiver is open sourced

Posted in Programming, Software on March 20th, 2011 by Pavel – Be the first to comment

SleepArchiver code with GPLv3 licenseWhile I am currently working hard on Scala plugin for IntelliJ IDEA at JetBrains, I have no time to continue development on SleepArchiver. However, there are still many useful features that could be implemented, so I decided to open source SleepArchiver and put the project on SourceForge under GPLv3 license.
Continue reading →

Scala for Project Euler

Posted in Programming, Scala on August 21st, 2010 by Pavel – 44 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 →


Posted in Programming, Software on August 18th, 2009 by Pavel – 5 Comments

SleepArchiver Logo - CatSleepArchiver is an open source, cross-platform data manager for Sleeptracker-series watches.

The aim of the program is to provide powerful, yet user-friendly capabilities to collect, store and analyze sleep data.
Continue reading →

Sleeptracker Pro Review

Posted in Gadgets on March 7th, 2009 by Pavel – 6 Comments

Sleeptracker Pro watch displayI bought Sleeptracker Pro gadget from Innovative Sleep Solutions LLC a month ago. The device is a watch, aimed to find best waking moments, so that waking up has never been easier.

Sleeptracker monitors your movements while you sleep and wakes you up at a time, when you are in an almost-awake state, so that you wake up easily and feeling refreshed. Moreover, Sleeptracker Pro allows to upload your sleep data to a computer for analysis that helps you to improve your sleep habits.

I considered the watch as a good investment despite the price, because I am really awful at waking up. Does the product come up to the expectations aroused by the manufacturer’s promises? My experience shows that, although Sleeptracker Pro have a few shortcomings, the watch does what it claims.
Continue reading →

The Best Player for Audiobooks and Podcasts

Posted in Gadgets on January 25th, 2009 by Pavel – 22 Comments

Tape with audiobooks and podcastsAlthough all portable players can play audiobooks and podcasts, few of them can do it well. In this article I suggest, based on my personal experience, what the perfect player for audiobooks and podcasts should look like.
Continue reading →

Hello World!

Posted in Programming on January 25th, 2009 by Pavel – 1 Comment

This is the post to cherish a tradition.