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:
- 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.
- The best list of articles are the one from Daniel Westheide and his suite of blog post The Neophyte's Guide to Scala
- Once you are really comfortable with the majority of Scala, read Programming in Scala by Odersky
- If you use any Twitter code, do read about Twitter util, a must read
- Do not take the Coursera classes on Scala or Functional Programming. I love Coursera but the Scala classes are garbage (that it a pitty!).
- 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)!
The ListUsage.scala file contains different usage patterns of the
Scala class. It shows how
for loops are translated into filter/map calls.
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
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
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.
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
that is defined in
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
TailRec.scala shows you
how to use the
@tailrec annotation to make sure a function
is tail recursive and does not kill your stack.
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
These are good examples to use the cats library that enables more functional programming patterns with Scala.
- Type classes basics (section 1.3)
- How to use
Show[A]with Cats (section 1.4)
- How to use
- What are Monoid (section 2.3 and 2.4)