Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created May 29, 2020 08:01
Show Gist options
  • Select an option

  • Save tonymorris/730e4984cff0ee20e5e74bcf39621929 to your computer and use it in GitHub Desktop.

Select an option

Save tonymorris/730e4984cff0ee20e5e74bcf39621929 to your computer and use it in GitHub Desktop.
// Which of f or g are more readable, and if possible, why?
def f(x: Seq[Int]): Int =
x.toList match {
case Nil => 99
case lst => lst.sum
}
def g(x: Seq[Int]): Int =
x.toList match {
case Nil => 99
case lst@(_::_) => lst.sum
}
@pedrofurla
Copy link
Copy Markdown

The less noise the better, so the answer is f.
For what "readability" arguments are concerned, the real question is often how much is the reader familiar.

@tonymorris
Copy link
Copy Markdown
Author

I agree less noise the better, hence the answer is g.

@pedrofurla
Copy link
Copy Markdown

Yeah, I didn't take overlapping into account. It's subtly deceptive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment