0

I have aList[List[(Char,Int)]]. Suppose it is

List(List(('a',10), ('b',12))) 

What I want to do is concatenate a bunch of elements to it but they should also be from a list. To make myself a bit more clear:

I have the list:

List(List(('a',9),('a',8) ... ('a',1))) 

What I want to do with this list is take each element (immediately made me think of map) and add them to my original list but as a different list. I am not sure if this makes any sense so let me just show you exactly what I want in a visual manner:

// The original list should look something like this List(List(('a',10),('b',12)), List('a',9), List('a',8) ... List('a',1)) 

What I have so far does not return the correct result as it transforms the entire list into a list itself and not every element into a list. Here is what I have so far:

//This is hard coded. List(List(('a',10) , ('b',12))) ::: List((for(c <- 1 to 10) yield ('a',c)).toList) 

This would return the following:

List(List(('a',10),('b',12)), List( ('a',10), ('a',9 )... ('a', 1))) 

My mind is stuck at the way you would do it in ruby ( with an each statement).

Could you please point me to the right function that I should be able to use for such a thing? Is there an easier way to do something like such?

9
  • Should the last statement be List(List('a', 11), ('b', 12)), List( ('a',10), ('a',9 )... ('a', 1))) ? Commented Jun 12, 2014 at 17:23
  • You mean what I am returned currently? Commented Jun 12, 2014 at 17:24
  • I mean, what should be the result for this List(List(('a',10) , ('b',12))) <your-concat> List((for(c <- 1 to 10) yield ('a',c)).toList)? Because, I almost got what you want to achieve by first example and completely lost in last second example. Commented Jun 12, 2014 at 17:27
  • 2
    Your post is not clear. I read it twice and I still don't understand. Also, maybe you could explain the larger context: it's quite possible that you are using the wrong data structure to start with. Commented Jun 12, 2014 at 17:52
  • 1
    I join the bunch of confused people. Could you explain your input, desired output and process to get there? Also, could you change the title to something more explanatory? Commented Jun 12, 2014 at 18:47

2 Answers 2

2
val l1 = List(List(('a',10), ('b',12))) val l2 = List(List(('a',9),('a',8),('a',1))) val concat = l1 ::: l2.flatMap(x=>x.map(y=>List(y))) 

Every element of l2 added to l1 encapsulated into its own List.

How to think 'functional' : Figure out the steps to transform our input into your output by applying a function each time:

  • l1 is ok, so we leave it as is
  • l2: The easiest way is to go "inside" the first List structure and transform each 2nd level element into a list-encapsulated element.

You could think of is a nested map:

l2.map(internalList => internalList.map(element => List(element))) res10: List[List[List[(Char, Int)]]] = List(List(List((a,9)), List((a,8)), List((a,1)))) 

But as you can see, this leaves the external structure in place. To break the "grouping shield" you use flatMap instead of map, effectively flattening the container structure, and liberating the List(elem) elements inside.

l2.flatMap(internalList => internalList.map(element => List(element))) res11: List[List[(Char, Int)]] = List(List((a,9)), List((a,8)), List((a,1))) 

This is the type that matches the first list, so a simple list concatenation will bring the two together:

l1 ::: l2.flatMap(internalList => internalList.map(element => List(element))) res12: List[List[(Char, Int)]] = List(List((a,10), (b,12)), List((a,9)), List((a,8)), List((a,1))) 
Sign up to request clarification or add additional context in comments.

1 Comment

need one more parenthese.
0

What I want to do with this list is take each element (immediately made me think of map) and add them to my original list but as a different list.

From what I understand :

val l1 = List(List(('a',10), ('b',12))) val l2 = List(List(('a',9), ('a',8), ('a',1)), List(('b',12))) val result = l1 ::: List(l2.flatten) res3: List[List[(Char, Int)]] = List(List((a,10), (b,12)), List((a,9), (a,8), (a,1), (b,12))) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.