MetallicPriest
MetallicPriest

Reputation: 30755

What is the difference between these two map expressions in Scala/Python?

Is there any difference between the two?

1. a = b.map(lambda (x,y): (y,x))
2. a = b.map(lambda x: (x[1], x[0]))

For those who work in Scala, I think it would be something like this.

1. a = b.map((x,y) => (y,x))
2. a = b.map(x => (x._2, x._1))

Upvotes: 2

Views: 194

Answers (1)

user1804599
user1804599

Reputation:

In Python 2 those are identical.

The difference in Python 3 is that the first one is a syntax error and the second one isn't.

The difference in Scala is that the first one is a binary function and the second one is a unary function taking a tuple. If you want to pattern match you have to pass a partial function, like this: y.map{ case (x,y) => (y,x) }.

Upvotes: 3

Related Questions