1

I have list of Integer like this:

val aRowcol: List[List[Int]]] = List(List(0, 0), List(0, 1), List(0, 2)), List(List(1, 0), List(1, 1), List(1, 2)), List(List(2, 0), List(2, 1), List(2, 2)), List(List(0, 0), List(1, 1), List(2, 2)), List(List(2, 0), List(1, 1), List(0, 2)), List(List(1, 0), List(0, 1), List(0, 2)), List(List(1, 0), List(2, 1), List(2, 2)) val aAlpha: List[List[String]] = List( List("a","b","c","d"), List("e","f","g","h"), List("i","j","k","l","m")) val i = 4 val resNum:List[List[Int,String]] = (0 to i) { _map => List( aRowcol.take(i).head.head, aRowcol.take(i).head(1), aAlpha(aRowcol.take(i).head.head)(aRowcol.take(i).head(1))} .toList 

But the result I want for val resNum is:

List( List(0,0,"a"), List(1,0,"e"), List(2,0,"i"), List(0,0,"a"), List(2,0,"i"))

(0,0) means first row first column, we have "a" on that possition, so i will define how many aAlpha we will have. I think it will be much easier if we do i++, but you know that we couldn't do i++ in scala.

4
  • 1
    can you better clarify what you're trying to achieve? I can't figure out the logic here. Plus - list.take(i).head is equivalent to list.head, because take(n) returns the first n items and head returns the first - so "first of first n" is the same as just "first" Commented Oct 31, 2016 at 16:03
  • 3
    And should aRowcol be List[List[List[Int]]]? Your sample code doesn't compile... Commented Oct 31, 2016 at 16:09
  • I don't see aNumber list in the code sample Commented Oct 31, 2016 at 16:12
  • Please explain your use case and what are you trying to do here ? Commented Oct 31, 2016 at 16:13

1 Answer 1

2

I'm guessing that you want to treat the first element in each "list of lists" in aRowcol as the "coordinates" of a letter in aAlpha, and want to append that letter to each of these "first elements".

If so:

val result: List[List[Any]] = aRowcol.take(5) // only 5 first rows .map(_.head) // first List(i, j) only, the rest is ignored .map { case List(i, j) => List(i, j, aAlpha(i)(j)) } // append the right letter to list result.foreach(println) // List(0, 0, a) // List(1, 0, e) // List(2, 0, i) // List(0, 0, a) // List(2, 0, i) 

If that's not what you meant - please clarify.

EDIT: as for your version - it can work (and achieve the same goal) with a few fixes:

  • list.take(i) doesn't return the i-th element, it returns a list with the first i elements, I think you're trying to use list.apply(i) which returns the i-th element, or it's shorthand version: list(i)
  • If you want to map the numbers 0..4 - call map and then name the argument of the anonymous function you pass i - don't use a var declared outside of the method and expect it to increment

With these corrections (and some more), your version becomes:

val resNum: List[List[Any]] = (0 to 4).map { i => List( aRowcol(i).head.head, aRowcol(i).head(1), aAlpha(aRowcol(i).head.head)(aRowcol(i).head(1))) } .toList 

Which works as you expect; But above is a similar yet simpler version.

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

5 Comments

Thanks @Tzach let me try.
do you think the i will return if we use for (i <- 1 to 4) ??
Also take 5 means the list number (0 to 4) isn't it?
Not sure I understand the question... Why would you need it? It's much easier (and more efficient!) to map existing collections (i.e. calling map on the first 5 items in aRowcol) then creating these index variables. aRowcol.take(5) does not create any index or list of numbers, it simply creates a sublist of the original one, and mapping over it gives you what you need.
For sure your answer is the most efficient way. I just want to know if for also usable for this case. Now I understand why we didn't use for in many scala case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.