0

I know it's related to the usage of tuples or a bug in Intellij (IntelliJ Community 2018) because it reports both map methods to require f: (Int, String) => B but when I provide such a function it tells me that I have a compilation failure:

List(1, 2, 3).zip(List ("a", "b", "c")).map((a, b) => "blah") //this does not compile 
(List(1, 2, 3), List("a", "b", "c")).zipped.map((a, b) => "blah") //this compiles

enter image description here

humbletrader
  • 396
  • 2
  • 10

2 Answers2

3

List(1, 2, 3).zip(List ("a", "b", "c")) creates a List[(Int, String)]. When calling .map on a List, you must pass a function that takes exactly one argument, which in this case is a tuple: (Int, String). You need a ((Int, String)) => B.

(List(1, 2, 3), List("a", "b", "c")).zipped creates a Tuple2Zipped[Int, List[Int], String, List[String]]. That class's .map method must be provided with a function that takes two arguments, the first of which is an Int and the second of which is a String. You need a (Int, String) => B.

(a, b) => "blah" is valid syntax for a function taking two arguments, not for one taking a single argument that is a tuple. So it's fine for the latter but not the former.

Joe K
  • 18,204
  • 2
  • 36
  • 58
  • so Intellij should report ( in the zip case ) that map needs an f: ((Int, String)) => B not an f : (Int, String) => B, right ? – humbletrader Jul 09 '18 at 22:28
  • Yes, I would say so. It could be that it's dropping some parens or something, but that message is definitely unhelpful. – Joe K Jul 09 '18 at 22:31
0

It's not a bug, you can try to compile the expressions on some another place (like the playgrund here).

For your case,

List(1, 2, 3).zip(List ("a", "b", "c")).map((a, b) => "blah")

should be rewritten as:

List(1, 2, 3).zip(List ("a", "b", "c")).map({case(a, b) => "blah"})

You should tuple the List(1, 2, 3).zip(List ("a", "b", "c")) portion be able able to use like (a, b).

For detailed explanation, see this post:

vahdet
  • 6,357
  • 9
  • 51
  • 106