Skip to content

Instantly share code, notes, and snippets.

@mads-hartmann
Created October 28, 2011 10:24
Show Gist options
  • Save mads-hartmann/1322034 to your computer and use it in GitHub Desktop.
Save mads-hartmann/1322034 to your computer and use it in GitHub Desktop.
wish you could use extractors in arguments in scala
(for {
x <- 0 to 10
} yield (42 -> x)).foldLeft(HashMap[Int,HashSet[Int]]()) {
(acc,current) =>
val (y,z) = current
acc.updated(y, acc.getOrElse(y, HashSet[Int]()) + z)
}
// I wish the folding function could be defined like this
//
// (acc, (y,z)) => acc.updated(y, acc.getOrElse(y, HashSet[Int]()) + z)
//
@gseitz
Copy link

gseitz commented Oct 28, 2011

Just slap a case in there:

(for { 
  x <- 0 to 10 
} yield (42 -> x)).foldLeft(HashMap[Int,HashSet[Int]]()) { 
  case (acc,(y,z)) => 
    acc.updated(y, acc.getOrElse(y, HashSet[Int]()) + z)
}

@mads-hartmann
Copy link
Author

Ah great. Thanks :)

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