Learning Scala for the Absolute Beginner

Learning Scala for the Absolute BeginnerSharing my tips for self-learning Scala (SKAH-lah)Dean SublettBlockedUnblockFollowFollowingJun 16Photo by JESHOOTS.

COM on UnsplashA PropositionLet’s say that I give you 4.

5 days toearn the certificate for completing Martin Odersky’s Scala coursegrasp the concepts behind the functional programming paradigmprepare a 10–15-minute lesson for a group who’s unfamiliar with ScalaCould you do it?I was tasked with these items this past week and failed miserably.

I realized that I didn’t have the prerequisites to get the most value out of Professor Odersky’s course, and I grew frustrated to the point of not finishing the course in my 4.

5 day timeline.

I’m here to share my Scala-learning journey so that you don’t have to stumble over the same obstacles.

If you’re reading this article, I’ll assume that you’re at least remotely interested in picking up Scala.

I’ll also assume that you’ve allotted yourself longer than one week to learn the language.

Some ObjectivesHere are some objectives for you:Begin to develop an understanding of functional programmingSet up your machine for writing ScalaFigure out next steps and further readingWhat are Programming Paradigms?Before we dive into an IDE (integrated development environment where we actually write Scala code), we’ll need to establish some motivation for why we might be interested in learning Scala.

While designing Scala, Prof.

Odersky sought to marry the object-oriented programming (OOP) and functional progamming (FP) paradigms.

Informally, a programming paradigm refers to a certain style or technique for solving problems using programming.

Object-oriented programming has three principles with their own features:Encapsulation: This principle is primarily concerned with access restriction.

Access restriction prohibits one object from accessing the state or values of another object.

Encapsulation can also allow for a name to carry different definitions/behaviors when used in separate contexts.

Inheritance: Broadly, this principle allows for one object to acquire (or inherit) the properties of another object.

Polymorphism: This principle refers to the same message resulting in different responses from different objects.

When given the same message, two objects might retain an abstract similarity but with different details.

Here’s an analogy from Dr.

Gary Shute (retired CS professor at University of Minnesota Duluth):“We frequently use the same phrases and sentences to mean different things in different contexts… For example, say the following sentence to two different architects and you will likely get two houses: ’Build me a house.

’ Abstractly, both architects will do the same thing but many of the details will differ.

”Please read more about object-oriented programming and its principles on Dr.

Shute’s page.

As for functional programming, we have the following aspects according to Prof.

Odersky:The absence of mutable variables and assignments.

Here, mutation refers to changing some feature of an object while its identity remains the same.

So if I say x = 5 and then reassign the value of x by saying x = ‘kombucha', I’ve changed the value of x while preserving its identity.

A strong focus on writing functions in programsFunctions are first-class citizens.

That is, functions (1) may be defined anywhere, even inside other functions, (2) may be passed as parameters and may be returned as results, and (3) there exist a set of operators to create functions.

If you’re in a time crunch to learn Scala like I was, the nuances of object-oriented programming and functional programming may not be as relevant to you, but it’s worth your while to gain a working knowledge of these two paradigms and appreciate their fusion in Scala.

Even though many programmers write Scala in the functional programming paradigm (Prof.

Odersky titles his course “Functional Programming”), object-oriented concepts occur in Scala and can’t be avoided in the learning process.

There’s a lot more to object-oriented and functional programming than what I’ve communicated here, but this brief overview should serve as a primer so that you can explore these topics in more depth later.

Setting Up ScalaFor simplicity’s sake and my own sanity, I’ll only discuss preparing machines running a Windows operating system.

Step 1: Install Oracle JDKFirst we’ll need the Java Development Kit (Scala was designed to interact well with Java).

If you plan to take Prof.

Odersky’s course, you’ll want the 1.

8 version of the JDK (also called Oracle JDK 8).

Download the installer from Oracle’s website and run it, of courseAdd the bin directory of the installed JDK to the PATH environment variableStep 2: Install Scala Build Tool (SBT)Next, we need the Scala Build Tool.

Download the installer from sbt’s website and run the installerVerify the installation by using the command line.

Windows Button → cmd → sbtYou should be able to access the sbt shell now.

If you can’t, you’ll need to reinstall or troubleshoot your installation.

Step 3: Install an IDEFor the sake of this article, I’ll only cover installing IntelliJ IDEA.

There are several other IDEs you may use (Eclipse is also another popular choice for Scala), and you can even use the command line if you prefer.

Download IntelliJ IDEA from the JetBrains website and run the installerFrom the “Welcome to IntelliJ IDEA” screen, Configure → Plugins → Browse JetBrains Plugins and install the Scala plugin.

If you’re already inside a project in the IDE, go to Preferences (Settings) → Plugins.

We also need to tell our IDE that we’re using the Oracle JDK.

To do this, from the “Welcome to IntelliJ IDEA” screen navigate to Configure → Project Defaults → Project Structure and select the JDK.

Now, you should be equipped to create an sbt project and a Scala worksheet where you can write and run Scala code.

I found the installation process painful when setting up my machine, so I hope that my condensed instructions are easy to follow.

Learning ScalaNow we actually need to learn Scala.

I was instructed to enroll in Prof.

Odersky’s course online, but I struggled since I had such shallow exposure to functional programming.

Be aware that the course presupposes at least one year of programming (not in any particular language or paradigm), some familiarity with basic data structures, and some underlying mathematical intuition.

If you fall within any of the following categories,Never programmedDon’t know JavaNever practiced functional programmingyou might be better off with an introduction to Scala that isn’t Prof.

Odersky’s online course.

If I could go back to Monday morning with the knowledge and experience that I have now, I’d recommend Programming in Scala to my past self.

Prof.

Odersky along with Lex Spoon and Bill Venners released this book as a “comprehensive step-by-step guide” to Scala.

The writing was clear, and I found myself following the material better than the online course.

If you have some experience with Java, then I would highly recommend Cay Horstmann’s Scala for the Impatient.

Once you get either or both of these books under your belt, then I’d recommend Prof.

Odersky’s online course to solidify your Scala knowledge and validate your abilities with the submitted programming assignments.

If you’re a Java rock star or just want a challenge in Scala, try the infamous “Red Book” — Functional Programming in Scala by Paul Chiusano and Runar Bjarnason.

This text focuses more on the functional programming paradigm than Scala syntax, which will help generalize functional programming concepts that can be applied to other languages that lend themselves to a functional style.

Before You GoI hope that reading this will set you up for success with Scala.

Functional programming is “hot” right now, and I’d encourage you to read Mikolaj Szabó’s article on why that’s the case.

Here’s a collection of different learning resources for you:Tour of Scala (official Scala documentation — always a great reference)Scala Tutorial (another quick reference with content written for beginners)Programming in Scala book (my personal recommendation for an intro to Scala)Functional Programming Principles in Scala Coursera courseScala for the Impatient (an alternative to Programming in Scala for those with Java experience)Functional Programming in Scala (for those who want a rigorous approach to learning functional programming with Scala).

. More details

Leave a Reply