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.

Community
  • 1
  • 1
Deden Bangkit
  • 598
  • 1
  • 5
  • 19

1 Answers1

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.

Tzach Zohar
  • 37,442
  • 3
  • 79
  • 85
  • Thanks @Tzach let me try. – Deden Bangkit Oct 31 '16 at 16:28
  • do you think the i will return if we use `for (i <- 1 to 4)` ?? – Deden Bangkit Oct 31 '16 at 16:33
  • Also take 5 means the list number (0 to 4) isn't it? – Deden Bangkit Oct 31 '16 at 16:37
  • 1
    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. – Tzach Zohar Oct 31 '16 at 16:39
  • 1
    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. – Deden Bangkit Oct 31 '16 at 16:52