bad.robot

good robots do what they're told

Type Classes in Scala

Type classes provide ad-hoc inheritance which means that we can use them to create polymorphic functions that can be applied to arguments of different types. This is a fancy way of saying that we can create common behaviour for classes without resorting to traditional (extends) polymorphism.

From the Neophytes Guide, Daniel Westheide describes type classes, slightly paraphrased, as follows.

Home Brew Temperature Logger

Using a Raspberry Pi Zero, some cheap components and some custom software, you can build a data logger to track ambient temperature in your home for around £10. Track days, weeks and months worth of temperature data and display some pretty graphs via the web.

'The "temperature machine" in action'

Pair Testing Doesn’t Work

Either when looking for work or looking to recruit, I’ve been doing pair tests in one form or another since 2008. I’ve only recently come to the conclusion that they just don’t work. At least not reliably.

I’m left wondering why we still use “pair tests” for recruitment. Is it to see how candidates problem solve? How they’d be to work with? The only way to assess these things is actually to do them. Pair tests are a poor simulation. If you want to see how someone works, work with them. Don’t pretend to work with them.

Easily Switch JDK on Mac

I have several versions of Java installed on my Mac. Trouble is, I can never remember where any of them are. So switching Java versions using the JAVA_HOME environment variable was always a pain. Then I discovered the handy java_home command.

/usr/libexec/java_home -V

It shows the Java versions are available and where there are. For example, on my machine, the output looks like this.

Implicit Functions in Scala

In the previous post, we looked at implicit parameters; parameters that will be automatically passed values annotated as implicit. In this post, we’ll take a look at implicit functions and how they can be useful to convert things of one type to things of another.

Implicit Parameters in Scala

Scala “implicits” allow you to omit calling methods or referencing variables directly but instead rely on the compiler to make the connections for you. For example, you could write a function to convert from and Int to a String and rather than call that function explicitly, you can ask the compiler to do it for you, implicitly.

In the next few posts, we’ll look at the different types of implicit bindings Scala offers and show some examples of when they can be useful.

Scala Learning Curve

If you’ve just started to learn Scala and are wondering what to expect, it’s typical to experience a quick ramp up in skill followed by a slower adoption of the more sophisticated features. In this post, I talk about what I think of as a typical learning curve.

Taken from my Pluralsight course, the chart shows experience (or time) along the x axis and some measure of “learning” on the y.

Scala Mixins: The Right Way

Scala traits are interesting because they can be used for inclusion polymorphism and to mixin behaviour. I’ve found tension here though, as the former uses inheritance and the later is more about code re-use. So when a Scala class extends a trait with behaviour, it seems to go against the generally accepted view that using inheritance as a mechanism for code re-use is a bad idea.

It can be tricky not break the inheritance vs. composition principle when using traits with behaviour. Is it clear to you when you might be?