Quotes from Clean Code

Back in 2013 I read the book Clean Code by Robert C. Martin, and in an effort to keep that book alive with me a little while longer, I decided to make my own “Cliffs Notes” version of the book on this page. One of my favorite notes from below is that a language named LOGO used the keyword to in the same way that Scala uses def, so a method named double would be defined as to double... instead of def double..., which seems like it would help developers name methods better.

Scala: How to append and prepend items to Vector and Seq

Table of Contents1 - Solution2 - Example data3 - Append a single item4 - Append multiple elements5 - Prepend a single item6 - Prepend multiple elements7 - Seq works just like Vector8 - How to remember the method names9 - A possible problem

Scala FAQ: How do I append or prepend one or more elements to a Vector or Seq class?

Back to top


To append or prepend one or more elements to a Vector or Seq, use these methods:

A Scala method to run any block of code slowly

The book, Advanced Scala with Cats, has a nice little function you can use to run a block of code “slowly”:

def slowly[A](body: => A) = try body finally Thread.sleep(100)

I’d never seen a try/finally block written like that (without a catch clause), so it was something new for the brain.

In the book they run a factorial method slowly, like this:

slowly(factorial(n - 1).map(_ * n))

FWIW, you can modify slowly to pass in the length of time to sleep, like this:

def slowly[A](body: => A, sleepTime: Long) = try body finally Thread.sleep(sleepTime)

How to define Scala methods that take complex functions as parameters (syntax)

Problem: You want to define a Scala method that takes a function as a parameter, and that function may have one or more input parameters, and may also return a value.

Solution: Following the approach described in the previous recipe, define a method that takes a function as a parameter. Specify the function signature you expect to receive, and then execute that function inside the body of the method.

Scala: How to define a generic method parameter that must extend a base type

In today’s installation of “how to have fun with Scala,” if you want to define a method that takes a parameter that has a generic type, and want to further declare that the parameter must extend some base type, use this syntax:

def getName[A <: RequiredBaseType](a: A) = ???

That example says, “The parameter a has the generic type A, and A must be a subtype of RequiredBaseType.”

This is a page from my book, Functional Programming, Simplified

Partially-Applied Functions (and Currying)


My motivations for writing this lesson are a little different than usual. Typically I think, “You’ll want to know this feature so you can use it like ___,” but the first motivation for this lesson goes like this: You’ll want to know about the concept of “currying” because experienced FP developers talk about it a lot, especially if they have Haskell programming experience. (I did mention that Haskell was named after Haskell Curry, didn’t I?)

This is a page from my book, Functional Programming, Simplified

How to Write and Use Scala Functions That Have Multiple Parameter Groups

“Logic clearly dictates that
the needs of the many outweigh the needs of the few.”

Spock in Star Trek II: The Wrath of Khan


Scala lets you create functions that have multiple input parameter groups, like this: