Skip to content

Instantly share code, notes, and snippets.

@fsvehla
Last active December 22, 2015 00:09
Show Gist options
  • Save fsvehla/6387428 to your computer and use it in GitHub Desktop.
Save fsvehla/6387428 to your computer and use it in GitHub Desktop.
def transformKeys[A,B,C](m: Map[A, B], transformFunc: (A => C)): Map[C, B] = {
m.foldLeft(Map.empty[C, B]) { (acc, entry) =>
acc + (transformFunc(entry._1) -> entry._2)
}
}
val numMap: Map[Int, String] = Map(1 -> "Two", 2 -> "Four", 3 -> "Six")
val transformed = transformKeys(numMap, { (key:Int) => key * 2 })
// without (key:Int) = error: missing parameter type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment