1

I am starting in Scala and I want to get an index while I am iterating over a list using the map function.

For example, I have this line of code:

val x= list.flatMap(l => anotherFunction(l.param1.param2List.size * l.multiplier, l, l.param1.param2List(0)))

And I wanted to do something like this:

val x= list.flatMap(l => anotherFunction(l.param1.param2List.size * l.multiplier, l, l.param1.param2List(index)))

Is there any way to add a counter or something like that in order to get the index as if I was looping through the list in a for cycle? Is it possible or should I try another approach?

2

1 Answer 1

1

I am sure this is a duplicate, use something like:

val l = List(1, 2, 4)
val r = l.zipWithIndex

Please read api documentation on collections:

http://www.scala-lang.org/api/current/scala/collection/immutable/List.html

What exactly are you trying to achieve ?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.