Functional Scala: Pattern Matching on product types

Welcome to another episode of Functional Scala!

This time, we’ll finally discover how to pattern match on product types. Within the last episodes we saw how to use pattern matching on some rather simple sum types. The one question left is how to use pattern matching and therein refer to the single components of a given value constructor for a product type.

So let’s start right away with a little warm-up and revisit our well known product type Shape. To make things a bit more interesting, let’s add Triangle (considered right-angled for simplicity sake) as another value constructor. Here it comes: Read the rest of this entry »

Functional Scala: Combinatoric Pattern Matching

Welcome to another episode of functional Scala!

In the last episode we were introducing ourself to the idea of Pattern matching – a powerful mechanism for operating on algebraic datatypes by examining different occurrences for a certain type separately, each within an individual case expression. So far, we only matched single values of some sum types by their given value constructors. In this episode we’re going to see how to match against a combination of sum type values. Read the rest of this entry »

Functional Scala: Pattern Matching – the basics

Welcome to another episode of functional Scala!

Within the last episodes, we discovered how to define so called algebraic datatypes. So far, we’ve only implemented some of them and saw how to construct some values for that types. Havin’ said that, we only walked on the construction side of algebraic datatypes so far. What’s wrong about that? We’re able to define new datatypes and bring some of their values into life happily. Can’t ask for more! Well, what’s missing is how to operate on them! So within that espisode we’re crossing the border and starting to deconstruct values of a given datatype, using pattern matching. We’ll see that pattern matchin is a very powerful way for writing comprehensible, well-structured functions over algebraic datatypes, while doing some case analysis. Read the rest of this entry »

Posted in Scala. 10 Comments »

Functional Scala: Algebraic Datatypes – ‘Sum of Products’ Types

Welcome to another episode of Functional Scala!

We’re not finished yet exploring algebraic datatypes! We’ve already seen some simple sum types and a pure product type in the last two episodes. However, the vast majority of algebraic datatypes constitute a combination of a sum and product type, that’s why they’re happily called ‘sum of products type’. If you’re now wondering what a funny thing such a ‘sum of products type’ might be, or even better – how they could be defined in Scala – don’t worry: in this episode we’re going to see and implement some of these hybrid types, so you’ll getting a good feelin’ for that. Read the rest of this entry »

Posted in Scala. 7 Comments »

Functional Scala: Algebraic Datatypes – Sum and Product Types

Welcome back to another episode of Functional Scala!

This one is the continuation of the last episode, where we introduced algebraic datatypes. Within this installment we’ll broaden the scope and find a more general definition for algebraic datatypes. Of course we don’t stop with definitions. Since we wanna write code (that’s Scala for, isn’t it?), we’ll see how to implement those extended new forms of algebraic datatypes (we’re going to meet today) within Scala! Read the rest of this entry »