over-documented #scala code sample for beginner
Switch branches/tags
Nothing to show
Clone or download
Latest commit a4c3a5d Aug 29, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
project First pass Jun 13, 2018
src/main/scala Adding all section 4 Aug 29, 2018
.gitignore Initial commit Jun 11, 2018
README.md refactoring the cats tutorial Aug 27, 2018
build.sbt refactoring the cats tutorial Aug 27, 2018

README.md

Scala Cookbook and other advices for beginners

This repository has some recipes and other code to reuse when starting with Scala. It is NOT an authoritative resource about how to write good code in Scala: it is rather a good resource with over-documented code that can help you started with this new language.

Scala learning curve

I documented my Scala learning curve on my blog there:

In short:

  1. Scala for the Impatient is a very good resource to start but you really need more. For example, it skips most of the important concepts you need to learn about Functional Programming. Take it to have a taste and quick overview of the syntax and how the language works, nothing more.
  2. The best list of articles are the one from Daniel Westheide and his suite of blog post The Neophyte's Guide to Scala
  3. Once you are really comfortable with the majority of Scala, read Programming in Scala by Odersky
  4. If you use any Twitter code, do read about Twitter util, a must read

Also

  1. Do not take the Coursera classes on Scala or Functional Programming. I love Coursera but the Scala classes are garbage (that it a pitty!).
  2. Always read the API docs on scala-lang.org/api

WARNING: if you are using Finatra, Finagle and other Twitter open-source projects (also available on github). Twitter has its own way of doing things but as a very early Scala adopter, does not use some of the very basic scala libraries. One good example is the Future, the implementation of the Twitter Future differs from the implementation of the Scala Future (and you can convert one into the other)!

Topics

List usage

The ListUsage.scala file contains different usage patterns of the List Scala class. It shows how for loops are translated into filter/map calls.

Types Classes

The TypeClasses.scala file contains usage for Types Classes. You can find more information on Daniel Westheide blog post.

Another great resource for type classes

This code will definitively help you understand the underlying concept of types classes and how to use it.

Concurrency: Future and Promise

Concurrency.scala shows how to use a Future and how it execute code asynchronously. It also shows the principle of a Promise

Optional

Scala does not have the concept of null. Instead, when you do not have a value, you return the singleton value None. At first, it does not seem very useful but this is a very powerful programming concept.

OptionUsage.scala shows how to use the Option class.

Exception Handling

ExceptionManagement.scala contains a good example to handle exception in your code. It defines a function that can return an exception and how to filter/print the exception.

Implicit value

ImplicitValUsage.scala is an example to declare implicit val and how to declare functions that uses implicit value. The main implicit you are going to meet in many codebase is the ExecutionContext that is defined in scala.concurrent.ExecutionContext.Implicits.global.

Implicit Conversion

ImplicitConversion.scala is an example to declare implicit function that is used to implicitly convert from one type to another. In that example, we convert from an Int to a Metric.

Stream

StreamUsage.scala shows how to use Stream with a function or a lazy val.

Tail recursion

TailRec.scala shows you how to use the @tailrec annotation to make sure a function is tail recursive and does not kill your stack.

Partial Functions

PartialFunctions.scala gives an example of partial functions. Partial functions are used where only some arguments are used/defined and that become functions themselves. If this is not clear enough, read the code!

By name parameters

ByNameParameter.scala shows how to define a by name parameter, where we only declare a block of code that returns something. Amazing description on the tpolecat blog.

Cats library

The directory cats contains the solutions for the book Scala with cats.

These are good examples to use the cats library that enables more functional programming patterns with Scala.

It shows:

  • Type classes basics (section 1.3)
  • How to use Show[A] with Cats (section 1.4)
  • How to use Eq[A] (section 1.5)
  • What are Monoid (section 2.3 and 2.4)