Week 4 Day 2 – Scala reveals its functional side

I’m a little behind this week, but i’m happy to be back into a bit more Scala. Today we learned about Scala’s functions, higher order functions (functions that take anonymous functions as parameters), currying functions (functions with multiple parameters that get transformed into other functions), and more about Lists, Maps and HashMaps.

On day 1 Scala reminded me strongly of Java. On day 2 it’s suddenly a lot more like Ruby. And then there are weird things that remind me of nothing i’ve ever seen before, such as “Everything inherits from ‘Any’, and ‘Nothing’ inherits from everything.” SRSLY, WUT?!

The foldLeft function

I find foldLeft very much like Ruby’s inject (or reduce). The book said this version would be harder to understand, but to me this looks just like Ruby. Here i am counting the number of letters in a list of words:

words.foldLeft(0) ((sum, word) => sum + word.length)

But Scala also has a weird version!

(0 /: words) {(sum, word) => sum + word.length }

I think /: is a very weird operator.

Replacing swear words

This was a hugely fun exercise! I chose Tim Minchin’s Fuck the Pope song which is the sweariest song i know! ;)

I used a Map for the swear words and their replacements. I found that a Map is unordered, so i converted it to a Sequence so that i could sort by length of swear word. (It helps to convert the longest words first, otherwise it would match partial words within longer words.)

The cleanVersion function went into a Censor trait that could be included into a RudeSong. It’s very much like a Ruby mixin, but i found that i needed a superclass Song to extend from, in order to mix in the Censor trait. I wish i could just do this:

class Song(val lyrics: String) with Censor

But that didn’t work. It had to be this:

class RudeSong(override val lyrics: String)
  extends Song(lyrics) with Censor

I found a way to read the song lyrics from a text file:

val lyrics = scala.io.Source.fromFile("lyrics.txt").mkString

You can find the full program at week4-scala/day2/curseWords.scala – please let me know if i can improve the style of the code at all, as i’m not at all familiar with Scala.

Here is a clean version of the last verse of Tim Minchin’s song!

So fornicate the mother lover, and fornicate you mother lover
If you’re still a mother loving papist.
If he covered for a single mother lover whos a kiddy-fornicator,
Fornicate the mother lover, he’s as evil as the rapist.
And if you look into your mother loving heart and tell me true
If this mother loving stupid freaking song offended you,
With its filthy freaking language and its freaking disrespect,
If it made you feel angry, go ahead and write a letter,
But if you find me more offensive than the freaking possibility
The pope protected priests when they were getting freaking fiddly
Then listen to me mother lover – this here is a fact,
You are just as morally misguided as that mother loving,
Power-hungry, self-aggrandized bigot in the stupid freaking hat.

And just because it’s a fun song, here’s the original!