humbletrader
humbletrader

Reputation: 414

what's the difference when mapping after a zip operation and mappping after zipped?

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

Upvotes: 0

Views: 69

Answers (2)

Joe K
Joe K

Reputation: 18424

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.

Upvotes: 3

vahdet
vahdet

Reputation: 6729

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:

Upvotes: 0

Related Questions