Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7957585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:16:20+00:00 2026-06-04T04:16:20+00:00

I have to often transpose a rectangular collection-of-collections in Scala, e.g.: a list of

  • 0

I have to often transpose a “rectangular” collection-of-collections in Scala, e.g.: a list of maps, a map of lists, a map of maps, a set of lists, a map of sets etc. Since collections can be uniformly viewed as a mapping from a specific domain to a co-domain (e.g.: a List[A]/Array[A] is a mapping from the Int domain to the A co-domain, Set[A]is a mapping from the A domain to the Boolean co-domain etc.), I’d like to write a clean, generic function to do a transpose operation (e.g.: turn a map of lists to the transposed list of maps). However, I’m having trouble because other than the () operator, Scala doesn’t seem to have a unified API to view collections abstractly as mappings ?

So I end up writing a separate transpose for each type of collection-of-collections as follows:

def transposeMapOfLists[A,B]( mapOfLists: Map[A,List[B]] ) : List[Map[A,B]] = {
  val k = ( mapOfLists keys ) toList
  val l = ( k map { mapOfLists(_) } ) transpose;
  l map {  v => ( k zip v ) toMap }
}

def transposeListOfMaps[A,B]( listOfMaps: List[Map[A,B]]) : Map[A,List[B]] = {
  val k = ( listOfMaps(0) keys ) toList
  val l = ( listOfMaps map { m => k map { m(_) } } ) transpose;
  ( k zip l ) toMap
}

def transposeMapOfMaps[A,B,C]( mapOfMaps: Map[A,Map[B,C]] ) : Map[B,Map[A,C]] = {
  val k = ( mapOfMaps keys ) toList
  val listOfMaps = k map { mapOfMaps(_) }
  val mapOfLists = transposeListOfMaps( listOfMaps )
  mapOfLists map { p => ( p._1, ( k zip p._2 ) toMap ) }
}

Can someone help me unify these methods into one generic collection-of-collections transpose ? It will also help me (and I am sure others) learn some useful Scala features in the process.

ps: I have ignored exception handling and have assumed the input collection-of-collections is rectangular, i.e., all of the inner collections’ domain elements constitute the same set.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:16:21+00:00Added an answer on June 4, 2026 at 4:16 am

    I’m sure the following messy version using type classes could be cleaned up a lot, but it works as a quick proof-of-concept. I don’t see an easy way to get the return types right without dependent method types (I’m sure it’s possible), so you’ll have to use -Xexperimental:

    trait Mapping[A, B, C] {
      type M[D] <: PartialFunction[A, D]
      def domain(c: C): Seq[A]
      def fromPairs[D](ps: Seq[(A, D)]): M[D]
      def codomain(c: C)(implicit ev: C <:< PartialFunction[A, B]) =
        domain(c).map(c)
      def toPairs(c: C)(implicit ev: C <:< PartialFunction[A, B]) =
        domain(c).map(a => (a, c(a)))
    }
    
    implicit def seqMapping[A, B <: Seq[A]] = new Mapping[Int, A, B] {
      type M[C] = Seq[C]
      def domain(c: B) = 0 until c.size
      def fromPairs[C](ps: Seq[(Int, C)]) = ps.sortBy(_._1).map(_._2)
    }
    
    implicit def mapMapping[A, B, C <: Map[A, B]] = new Mapping[A, B, C] {
      type M[D] = Map[A, D]
      def domain(c: C) = c.keys.toSeq
      def fromPairs[D](ps: Seq[(A, D)]) = ps.toMap
    }
    
    def transpose[A, B, C, M, N](m: M)(implicit
      pev: M <:< PartialFunction[A, N],
      qev: N <:< PartialFunction[B, C],
      mev: Mapping[A, N, M],
      nev: Mapping[B, C, N]
    ) = nev.fromPairs(nev.domain(mev.codomain(m).head).map(b =>
        b -> mev.fromPairs(mev.toPairs(m).map { case (a, c) => a -> c(b) })
    ))
    

    And now for some tests:

    scala> println(transpose(List(Map("a" -> 1, "b" -> 13), Map("b" -> 99, "a" -> 14))))
    Map(a -> Vector(1, 14), b -> Vector(13, 99))
    
    scala> println(transpose(Map('a' -> List(1, 2, 3), 'z' -> List(4, 5, 6))))
    Vector(Map(a -> 1, z -> 4), Map(a -> 2, z -> 5), Map(a -> 3, z -> 6))
    
    scala> println(transpose(Map("x" -> Map(4 -> 'a, 99 -> 'z), "y" -> Map(4 -> 'b, 99 -> 's))))
    Map(4 -> Map(x -> 'a, y -> 'b), 99 -> Map(x -> 'z, y -> 's))
    

    So it’s working as desired.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have often heard people talking about hashing and hash maps and hash tables.
I often have a situation in my Java code when I need to set
I quite often have rows of code like the following: UPDATE my_table SET name
I have often wanted to create a list of objects where each object must
I often have a list of pairs, as data = {{0,0.0},{1,12.4},{2,14.6},{3,25.1}} and I want
I have often encountered an error such as cannot convert from 'method group' to
I have often PATHs for files which do not exist in my codes. I
I have often seen the spinning gears OpenGL example ( I think originally done
I have often seen an init() within the constructor of AS3 classes, sometimes even
I have often heard complaints against Java for not having unsigned data types. See

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.