The language, not the opera house in Milan
Overview
I don’t want to repeat here what others have already written.
My aim with this effort is a faster, easier way to show how to build a full application using Scala, not just to impress others about supposedly cool concepts.
So this is organized to introduce the Scala programming language sequenced for quick learning.
Compilation is covered in the Ecosystem page. The big thing about Scala is that it enables developers to alter compiler behavior by programming macros (user-defined functions that are called by the compiler) and implicit classes, to create your own operators or override existing ones (essentially being unary and binary functions with non-alphanumeric identifiers), also known as the “pimp my library” pattern. Unfortunately, language tools such as IntelliJ have found it difficult to help with that aspect.
Language features
- Functional programming
-
Applications
- Data Types
- Variables
- Internationalization
- Exceptions + try catch finally
-
Logging
-
Arrays
- Integration
- Classes
-
Singleton and Companion Objects
-
Traits
- if/then/else
-
<a href=’#MatchCase”>Match (case) Expressions</a>
- while
- for
Scala vs. Java
With Scala:
- Functions as objects.
- All types are objects.
-
Immutable objects
- Type inference
- Domain specific language (DSL) support
- Traits
- Closures (vs Java 8)
- Concurrency support (inspired by Erlang
Static Code Analyzer
I think that Static Code Analyzers should be run on the most basic of code, by the most junior of developers. This is so one doesn’t develop bad habits being introduced by most programming tutorials available, which use naming conventions not useful for enterprise work.
Some “best practices” documents:
- twitter.github.io/effectivescala presents “best practices” for Scala at Twitter. Useful for understanding idioms in Twitter’s code.
github.com/alexandru/scala-best-practices makes use of RFC 2119 - Key words for use in RFCs to Indicate Requirement Levels
(“Cargo-cult” - sounds like a rock band.)
Alex divides his rules this way:
- hygienic-rules
- language-rules
- architecture
- concurrency-parallelism
- actors
It’s a rather “chicken or the egg” issue - how can someone understand what to avoid unless they understand what it is they are avoiding?
Exception mechanism similar to Java’s
Scala has an exception mechanism similar to Java’s.
Let’s look at an I/O call:
Traits like an abstract class
Scala offers traits instead of Java interfaces.
Traits in Scala are best described as “interfaces that can provide concrete members.” A class gets its concrete members for free by mixing in a trait and implementing the abstract members.
https://www.safaribooksonline.com/blog/2013/05/30/traits-how-scala-tames-multiple-inheritance/
In addition to serving as pure interfaces, traits can also provide bundles of functionality like Java’s AbstractFoo (e.g. AbstractList, etc.) classes. A trait can have a few abstract methods and a number of concrete members implemented in terms of those abstract members. A trait can be added to any Scala class.
Like interfaces with implementations or controlled multiple inheritance.
According to Wikipedia, “traits are a set of methods that can be used to extend the functionality of a class.”
Twitter defines: “Traits are collections of fields and behaviors that you can extend or mixin to your classes.” and offers this code example which extends traits using keywords:
class BMW extends Car with Shiny {
val brand = "BMW"
val shineRefraction = 12
}
The above is dependent upon these definitions:
trait Car {
val brand: String
}
trait Shiny {
val shineRefraction: Int
}
class BMW extends Car {
val brand = "BMW"
}
- A class can extend only one class, but
- a class can extend several traits.
Methods as operators
println(3 max 4) // => 4
Case Classes
A case in front of a class definition makes it a factory method which creates getter classes:
case SomeClass(arg1:String)
With Scala, a companion object is where static objects are defined.
Scala is a New Paradigm from Java
m map { t ==> val (s, i) = t; (s, i+1) }
with “Sytactic sugar” removed to Java map function:
m.map({ t ==> val (s, i) = t; (s, i+1) })
A Tuple is a fixed list which can be typed differently and can be a container f or other data types. Since Sashmi and Onigiri are instances of the same type Sushi:
val bento:(Sushi, Sushi) = (new Sashimi, new Onigiri)
Arity of 22
Scala thus is able to identify issues at compile time.
Tests
package X
class X extends FlatSpec{
}
import org.salatest.FlatSpec
Run Tests
The tilde prefix detects if code changed and runs:
~test
Pattern Matching
Read XML
Arrays
Define an array containing 10 items (0-9):
var myArray : Array[String] = new Array[String](10);
Match case
Scala simplifies on Java’s switch statement by not falling through cases until it hits a break operator. Thus, Scala doesn’t need a break operator.
Also, Scala’s match can return a value (myResult):
var myVar = "theValue";
var myResult =
myVar match {
case "someValue" => println(myVar + " 1");
case "thisValue" => println(myVar + " 2");
case "theValue" => println(myVar + " 3");
case "doubleValue" => println(myVar + " 4");
}
println(myResult);
Resources
-
https://learnxinyminutes.com/docs/scala/
-
http://bigdatauniversity.com/courses/scala-course/ 6-hour Introduction to Scala by Jamie Allen created by Typesafe
-
http://bigdatauniversity.com/courses/scala-data-science/ 6 - 8 hours works with Apache Spark with built-in modules for streaming, SQL, machine learning and graph processing.
YouTube videos
Scala Language Basics by Mark Lewis
Scala Tutorial by Derek Banas of http://newthinktank.com doesn’t waste your time, so covers the topic thoroughly in a short time. http://goo.gl/O1CuGM
Busy Java Developer’s Guide to Scala: Thinking by NewCircle Training
Scala versus Java by NewCircle Training
Intro to Functional Programming in Scala by Joe Barnes
Videos gradually revealing each box as I talk can be seen in a YouTube playlist: https://goo.gl/AKEFKj for the expanded URL: https://www.youtube.com/playlist?list=PLsnl23XQgokHh5u2C4dbegMJdqfzY8K9k
The QR code is provided of the link is provided here in case you prefer to watch videos on your smartphone.
https://www.toptal.com/scala/why-should-i-learn-scala
Learning Resources
Scala: Getting Started by Justin Pihony (@JustinPhihony http://justin-pihony.blogspot.com)
Scala for Java Developers by Toby Weston (@jamanifin baddotrobot.com) on imperative Java
Play! 2 for Scala by James Hughes james@yobriefca.se