How to flatten a nested tuple?
Asked Answered
S

5

25

I have a nested tuple structure like (String,(String,Double)) and I want to transform it to (String,String,Double). I have various kinds of nested tuple, and I don't want to transform each manually. Is there any convenient way to do that?

Stitt answered 4/12, 2012 at 8:51 Comment(0)
B
25

If you use shapeless, this is exactly what you need, I think.

Bravar answered 4/12, 2012 at 9:19 Comment(2)
you should put some code exemple. link only answers don't follow stackoverflow guidelinesSpirt
@Spirt see code example hereAlanna
F
10

There is no flatten on a Tupple. But if you know the structure, you can do something like this:

implicit def flatten1[A, B, C](t: ((A, B), C)): (A, B, C) = (t._1._1, t._1._2, t._2)
implicit def flatten2[A, B, C](t: (A, (B, C))): (A, B, C) = (t._1, t._2._1, t._2._2)

This will flatten Tupple with any types. You can also add the implicit keyword to the definition. This works only for three elements. You can flatten Tupple like:

(1, ("hello", 42.0))   => (1, "hello", 42.0)
(("test", 3.7f), "hi") => ("test", 3.7f, "hi")

Multiple nested Tupple cannot be flatten to the ground, because there are only three elements in the return type:

((1, (2, 3)),4)        => (1, (2, 3), 4)
Ferromagnetic answered 4/12, 2012 at 9:17 Comment(0)
C
2

Not sure about the effiency of this, but you can convert Tuple to List with tuple.productIterator.toList, then flatten the nested lists:

scala> val tuple = ("top", ("nested", 42.0))
tuple: (String, (String, Double)) = (top,(nested,42.0))

scala> tuple.productIterator.map({
     |   case (item: Product) => item.productIterator.toList
     |   case (item: Any) => List(item)
     | }).toList.flatten
res0: List[Any] = List(top, nested, 42.0)
Cladophyll answered 19/4, 2018 at 9:31 Comment(1)
Unfortunately, with this approach, you will lose the static typing of the elements.Threadgill
A
1

Complement of answer above

Paste this utility code:

import shapeless._
import ops.tuple.FlatMapper
import syntax.std.tuple._
trait LowPriorityFlatten extends Poly1 {
  implicit def default[T] = at[T](Tuple1(_))
}
object flatten extends LowPriorityFlatten {
  implicit def caseTuple[P <: Product](implicit lfm: Lazy[FlatMapper[P, flatten.type]]) =
    at[P](lfm.value(_))
}

then you are able to flatten any nested tuple:

scala> val a = flatten(((1,2),((3,4),(5,(6,(7,8))))))
a: (Int, Int, Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6,7,8)

Note that this solution does not work for self-defined case class type, which would be converted to String in the output.

scala> val b = flatten(((Cat("c"), Dog("d")), Cat("c")))
b: (String, String, String) = (c,d,c)
Alanna answered 8/3, 2021 at 22:53 Comment(0)
D
0

In my opinion simple pattern matching would work

scala> val motto = (("dog", "food"), "tastes good")
val motto: ((String, String), String) = ((dog,food),tastes good)

scala> motto match {
     | case ((it, really), does) => (it, really, does)
     | }
val res0: (String, String, String) = (dog,food,tastes good)

Or if you have a collection of such tuples:

scala> val motto = List(
     | (("dog", "food"), "tastes good")) :+ (("cat", "food"), "tastes bad")
val motto: List[((String, String), String)] = List(((dog,food),tastes good), ((cat,food),tastes bad))

scala> motto.map {
     | case ((one, two), three) => (one, two, three)
     | }
val res2: List[(String, String, String)] = List((dog,food,tastes good), (cat,food,tastes bad))

I think it would be convenient even if you have several cases.

Depute answered 2/3, 2021 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.