Scala pairs using map and flatten

Vandexel

I'm working on a problem to take two lists, for example (1,2,3) and (a,b) and return a list ((1,a)(1,b)(2,a)(2,b)(3,a)(3,b)) using only map and flatten.

This problem requires me to define a function as follows:

def product[A](xs: List[A], ys: List[A])= {

And within this function get the result. I'm rather new to Scala and I am used to languages like python and java.

I've gotten this far:

def product[A](xs: List[A], ys: List[A])= {  
    for(y <- ys){
   println(xs.map(x=> (x,y)))  
    }
  }

This will return something like this:

List((1,a), (2,a), (3,a))

List((1,b), (2,b), (3,b))

I'm not sure how can combine these lists now. In python I would do something like create a new list variable, append both of these lists to that list, and then flatten it so I would have one list. However, I'm rather confused by scala as it seems as if I am not allowed to define a new variable within a function. How can I combine these lists and flatten them at this point?

Ori Popowski

You can solve it using for comprehension. It's actually a syntactic sugar for map and flatMap:

def product[A](xs: List[A], ys: List[A])= {
    for {
      x <- xs
      y <- ys
    } yield {
      x -> y
    }
}

For-comprehensions is the Scala idiomatic way to achieve this. It's more readable and maintainable, and the underlying operations are still map and flatMap. In fact, even for types that are not collections, but still have map and flatMap, it's common to use for comprehensions (Futures, Options, Trys, etc.)

Edit

If you want to continue with your solution and save the lists and combine them, you'll have to drop the println and add a yield, and then flatten the main list that was created:

def product[A](xs: List[A], ys: List[A]) = {
  for (y <- ys) yield {
    xs.map(x => (x, y))
  }
}

val res = product(List(1, 2, 3), List("a", "b"))

println(res.flatten)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to flatten map values using java streams

Java - Flatten nested map using Stream

How to find the number of (key , value) pairs in a map in scala?

Scala: Use case for Map.flatten?

How do you flatten a nested List using round robin in Scala?

Flatten a Map of Option to Map

Flatten the map and associate values using Java 8 Stream APIs

Iterative summary by column pairs using purrr map

Scala cats map EitherT Futures with key->value pairs

Why do .map.flatten and flatMap on a Scala Map return different results?

Flatten a Seq of Maps to Map using Type polymorphism in Scala, Spark UDF

Flatten any nested json string and convert to dataframe using spark scala

Scala sequence groupBy then map different functions to groups then flatten it

Scala flatten a map with list as key and string as value

Using scala map in Java

scala parallel array with map and flatten

How do I return multiple key-value pairs in Scala using Spark's map transformation?

How to make function for flatten pairs of arrays in Scala?

Using scala's filter on a list of pairs

Adding key-value pairs to Map in scala

Scala How to flatten nested Map[String, Any]

Understanding using a method in map in Scala

Using underscore in scala map function

Elegant way from map to csv scala (flatten a map)

Scala Spark map by pairs of RDD elements

Scala - Flatten a List of Maps to Map

How to flatten d3.nest using javascript map() method?

scala - map & flatten shows different result than flatMap

Is there a performance difference between using flatten(...map()...) vs flatMap()?