Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created May 29, 2020 08:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonymorris/730e4984cff0ee20e5e74bcf39621929 to your computer and use it in GitHub Desktop.
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

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
Author

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

@pedrofurla
Copy link

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