Design Patterns in Scala

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

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

The content is a base for my talk at JavaDay conference (video, slides). 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 – 1 Comment

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

See also: Scala for Project Euler.
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 a toy language with an interpreter and a compiler. 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 employed an 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.
Continue reading →


Posted in Programming, Scala on July 25th, 2011 by Pavel – Be the first to comment

Scalathon roomAs a part of JetBrains Scala team I had a chance to visit Scalathon 2011 to represent our project – Scala plugin for IntelliJ IDEA.

An initial projector malfunction made me a little bit nervous about speaking without my thoroughly prepared slides, however, everything was resolved and my presentation seemed to went fine.

In addition to project presentations we had two days for hacking Scala code. It was a pleasure to see so many projects and so much involvement. These people are really passionate about what they do!

As turned out (surprise), many programmers were using IDEA with the Scala plugin as their primary development environment. Usually I encounter only “virtual” plugin users, but there I had a chance to provide a truly personal support. I was both proud of our work and ashamed of whatever bug it may have :)

Right before the Scalathon I had been evaluating ScalaCL compiler plugin for use in our project, so I was really glad to meet Olivier Chafik there in person. Our talk shed light on many tricky parts of ScalaCL implementation.

Being among all those people form mailing lists, tweets, blogs, forums and even books makes you feel a part of community (and Scala community is an amazing community to be part of). I would like to thank all the organizers for their great work!

Here are some photos of the Philadelphia, Scalathon, ENIAC and me.

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 – 38 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 – 2 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 – 15 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.