How to flatten nested tuples in scala


In a project which I’ve been working on, I encountered a situation to flatten a nested tuple but couldn’t come up with a way to do so, hence out of curiosity I started googling about it and came to the following conclusion.

As for an example I had a structure something similar to the one mentioned below, though not identical:


val structureToOperateOn = List(List("a1","a2","a3"), List("b1","b2","b3") , List("c1","c2","c3"), List(10,1,11))

and, supposedly I wanted to make structureToOperateOn something like this:


"a1", "b1", "c1", 10
"a2", "b2", "c2", 1
"a3", "b3", "c3", 11

So the first thing that came to my mind was to use foldLeft:


val operatedStructure = (structureToOperateOn.tail.foldLeft(structureToOperateOn.head)((a,b) => a zip b)).asInstanceOf[List[(((String,String),String),Int)]]

which resulted in something like this:


List(((("a1","b1"),"c1"),10), ((("a2","b2"),"c2"),1), ((("a3","b3"),"c3"),11))

Next, I thought of flattening the tuples and came across Shapeless. Although I think scala should have something to flatten tuples, the best way it could be done as of now is to use Shapeless library. Anyways, this is how flattening tuples using Shapeless works:


import shapeless._
import shapeless.ops.tuple.FlatMapper
import syntax.std.tuple._

object NestedTuple {
trait LowPriorityFlatten extends Poly1 {
implicit def default[T] = at[T](Tuple1(_))
}

object flatten extends LowPriorityFlatten {
implicit def caseTuple[P <: Product](implicit fm: FlatMapper[P, flatten.type]) =
at[P](_.flatMap(flatten))
}

val structureToOperateOn = List(List("a1","a2","a3"), List("b1","b2","b3") , List("c1","c2","c3"), List(10,1,11))
val operatedStructure = (structureToOperateOn.tail.foldLeft(structureToOperateOn.head)((a,b) => a zip b)).asInstanceOf[List[(((String,String),String),Int)]]

val flattenedTuples = operatedStructure map (tuple => flatten(tuple))   // This should be List((a1,b1,c1,10), (a2,b2,c2,1), (a3,b3,c3,11))

}

After messing around with nested tuples, I finally thought it’d be better to have an alternative way to get the required result instead of adding a new library in the project. Regardless, it could be very helpful in scenarios where you might get stuck and would want to ultimately flatten a tuple.

This was what I used as an alternative:


val operatedStructure = structureToOperateOn.transpose

which resulted in:


List(List("a1", "b1", "c1", 10), List("a2", "b2", "c2", 1), List("a3", "b3", "c3", 11))

So to conclude, you can use Shapeless in order to flatten complex nested tuples if need be.

 

 

Advertisements
This entry was posted in Scala and tagged , , , . Bookmark the permalink.

5 Responses to How to flatten nested tuples in scala

  1. scott m gardner says:

    I don’t think that is the standard meaning of flatten. My recollection is that flattening your original list should produce: (a1, a2, a3, b1, b2, b3, c1, c2, c3, 10, 1, 11).

    • Hi Scott, in the example I wanted to flatten (((“a1″,”b1″),”c1”),10), (((“a2″,”b2″),”c2”),1) and (((“a3″,”b3″),”c3”),11) individually after using foldLeft so as to get something like:
      “a1”, “b1”, “c1”, 10
      “a2”, “b2”, “c2”, 1
      “a3”, “b3”, “c3”, 11
      I modified the flattening example a bit so as to make things more clear.

  2. Travis Brown says:

    It would be nice to provide some links and attribution here, since your implementation was copied verbatim from the Shapeless examples.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s