No one will argue that IT is one of the fastest developing areas of engineering. New tools, approaches and ideas complete or even supersede the existing ones. One of the quickest growing technology stacks is the Scala language stack. In this blog we explore what makes this language awesome.
Scala was designed to help write thread safe and laconic code. It overcomes some JVM limitations and provides features that could not be achieved in Java. Scala has a clean, expressive, and extensible syntax with lots of built-in shorthands for most common cases.
Since less code needs to be written to accomplish the same task, the programmer can now focus on the problem’s solution instead of spending the time for boilerplate code. It is especially nice if you pay your programmers for SLOC. Furthermore, Scala reduces the number of places mistakes can be made and hence improves implementation quality.
Here is a short list of Scala language and compiler features:
- Type inference – in most cases compiler can automatically detect types of values:
val i = 1 // Int
val s = “Hello, world!” // String
- Named and default function arguments:
class Point(x: Int = 0, y: Int = 0)
new Point(y = 1) // Point(0, 1)
- Tail recursion optimization – recursive function self-calls transforms to loops – no more StackOverflowError.
- Support of both imperative object-oriented and functional programming – in Scala object composition, methods, state encapsulation and inheritance, traits and mixins combined with lazy evaluations, algebraic data types, pattern matching, type classes and, of course, first class functions.
- Lots of immutable and mutable, finite and infinite collections with many implemented transformations like map, reduce and filter:
users.filter(_.lastVisitDate before today).map(_.email).foreach(sendNotification)
// Sending notifications to all users that visited our site too long ago
- Concurrency through mechanisms of actors and futures:
val sum = sumActor ? List(1, 2, 3) // sum == Future(6)
val x = sum.map(_ * 2) // x == Future(12)
- Generics – JVM does use type erasure and knows nothing about real types of generics in runtime, but Scala keeps that information.
- Compile-time meta programming – in addition to runtime reflection, Scala compiler supports macroses – functions evaluated during compilation.
Distributed Computations and Big Data
One of the fields where Scala found its widest application is distributed computing. Scala has great mechanisms for working with data sequences even in a cluster, which is why it is frequently used by Big Data engineers and data scientists. Here is a list of the most well-known technologies that use Scala:
- Akka – a framework for creating distributed systems. It is based on actors model and makes it easier to implement concurrent applications without race conditions and explicit synchronization.
- Spark – a very popular batch data processing framework. Spark has integration with different data sources such as Cassandra, HBase, HDFS, and Parquet files. In addition, it has a Streaming extension that provides tools for building stream processing pipelines. One of the most powerful features of Spark is the ability to run quick ad-hoc tasks to check hypothesis. Such functionality is reached by Spark’s design that in some cases gives more than 100 times performance impact in comparison with Hadoop jobs.
- Kafka – a high performance message queue, one of the key middleware components in data streaming systems. It is distributed by design and usually acts like a data buffer in streaming systems on the different stages of the processing and as a media between different system parts.
- Samza – one more framework for stream processing. It is similar to Spark Streaming but works in a different way. It does not create micro batches as Spark does. Instead, it processes data pieces as soon as they arrive, which makes Samza more preferable in some cases.
In addition to these tools, it is also possible to use Scala for implementing good old Hadoop map-reduce jobs, directly or utilizing Scalding. Anyway Scala is a great choice for data processing and distributed computing.
Compatibility with Java
One more important thing is compatibility with libraries written in Java. Java code can be used in Scala directly and without limitations. This allows to keep existing modules without the need to re-implement them. It could be quite helpful when it is needed to use a rare library, legacy code, or API that have implementations only for Java.
To sum it up, Scala is well designed and very extensible. It has special processes (SIP and SLIP) that let any Scala developer propose enhancements. In conjunction with the large community, Scala ecosystem has been growing rapidly. Scala has its own stack of tools and is compatible with existing Java code. It brings new effective approaches that give the programmer an ability to do their job more efficiently. All these features make Scala one of the most attractive modern programming languages.
Contact us at email@example.com to learn more or if you have a Scala project to discuss.