-2

I'm trying to create a Map with idiomatic Scala with a sequenced key and a value of zero. I was thinking something like this:

(0 to 445) map (_ => _ -> 0).toMap

but that fails the IDE (IDEA) complains with cannot resolve symbol ->

and the compiler complains about missing parameter type

What do I do wrong?

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
dr jerry
  • 9,768
  • 24
  • 79
  • 122

2 Answers2

3

You can only use the placeholder _ once in a function declaration for each argument. What you want is this:

(0 to 445) map (_ -> 0).toMap

or an explicit value like this

(0 to 445) map (x => x -> 0).toMap
Tim
  • 26,753
  • 2
  • 16
  • 29
1

Another option you have is to use zipAll:

0.to(445).zipAll(Seq.empty[Int], 0, 0).toMap

or zip:

0.to(445).zip(Seq.fill(445)(0)).toMap

or:

Map.from(0.to(445).map(_ -> 0))

Code run at Scastie.

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35