background preloader

Scala

Facebook Twitter

Scala School - Type & polymorphism basics. This lesson covers: What are static types? Why are they useful? According to Pierce: “A type system is a syntactic method for automatically checking the absence of certain erroneous behaviors by classifying program phrases according to the kinds of values they compute.” Types allow you to denote function domain & codomains. For example, from mathematics, we are used to seeing: f: R -> N this tells us that function “f” maps values from the set of real numbers to values of the set of natural numbers. In the abstract, this is exactly what concrete types are. Given these annotations, the compiler can now statically (at compile time) verify that the program is sound. Generally speaking, the typechecker can only guarantee that unsound programs do not compile. With increasing expressiveness in type systems, we can produce more reliable code because it allows us to prove invariants about our program before it even runs (modulo bugs in the types themselves, of course!).

Types in Scala Type inference. Effective Scala. Table of Contents Other languages 日本語Русский简体中文 Introduction Scala is one of the main application programming languages used at Twitter. Scala provides many tools that enable succinct expression. Above all, program in Scala. This is not an introduction to Scala; we assume the reader is familiar with the language. This is a living document that will change to reflect our current “best practices,” but its core ideas are unlikely to change: Always favor readability; write generic code but not at the expensive of clarity; take advantage of simple language features that afford great power but avoid the esoteric ones (especially in the type system).

And have fun. Formatting The specifics of code formatting — so long as they are practical — are of little consequence. This is of particular importance to Scala, as its grammar has a high degree of overlap. We adhere to the Scala style guide plus the following rules. Whitespace Indent by two spaces. Naming Use short names for small scopes Use vals Prefer: Scala Documentation - Scala Documentation. Akka Work Pulling Pattern to prevent mailbox overflow, throttle and distribute work. I just gave a talk about the Work Pulling Pattern at NY-Scala (slides), so I figured it's a good time to update my previous article on the Work Pulling Pattern.

It's based on Derek Wyatt's blog post who should get all credit for it - I've just picked it up and explain it in isolation. This pattern ensures that your mailboxes don't overflow if creating work is fast than actually doing it - which can lead to out of memory errors when the mailboxes eventually become too full.

It also let's you distribute work around your cluster, scale dynamically scale and is completely non-blocking. At Movio we use it in a number of places, and Victor Klang and Ronald Kuhn from the core Akka team told me that they use it aswell - that should be reason enough to have it in your toolbox. Scenario Let's assume we want to send out a couple of million emails. Case Go ⇒ val batch = findWorkBatch() batch foreach { work ⇒ emailCreator ? Note that emailCreate ? A few 'solutions' with caveats Benefits Getting started. Scala, Modeling and Machine learning.