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

  • Home
  • SEARCH
  • 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 4026314
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:57:17+00:00 2026-05-20T10:57:17+00:00

I’m trying to figure out if I can convert List(Vector(1), Vector(2,3)) to Vector(List(1), List(2,3))

  • 0

I’m trying to figure out if I can convert List(Vector(1), Vector(2,3)) to Vector(List(1), List(2,3)) or any two traversable classes in a generic way.

This is what I have so far:

import collection.generic.CanBuildFrom
import collection.breakOut
import collection.mutable.ListBuffer

def f[A, CC[X] <: Traversable[X], DD[Y] <: Traversable[Y]](outer: DD[CC[A]])
  (implicit cbf1: CanBuildFrom[Traversable[_], A, DD[A]],
  cbf2: CanBuildFrom[Traversable[_], DD[A], CC[DD[A]]])
  : CC[DD[A]] = {
  val builder2 = cbf2()
  outer.foreach { inner =>
    val builder1 = cbf1()
    builder1 ++= inner
    builder2 += builder1.result
  }
  builder2.result
}

f(Vector(List(1), List(2,3)))(breakOut, breakOut) 
f(ListBuffer(Vector(1), Vector(2,3)))(breakOut, breakOut) 

If I remove the breakOuts, it will not compile:

f(Vector(List(1), List(2,3))) // won't work

could not find implicit value for parameter cbf1: scala.collection.generic.CanBuildFrom[Traversable[_],Int,scala.collection.immutable.Vector[Int]]

Are there implicits I can import? Any other way to approach this problem that would not require the breakOuts?


edit based on retronym’s answer:

import scalaz.CanBuildAnySelf
def f[A, CC[X] <: Traversable[X] : CanBuildAnySelf, 
         DD[Y] <: Traversable[Y] : CanBuildAnySelf](outer: DD[CC[A]])
           : CC[DD[A]] = {
  val builder2 = implicitly[CanBuildAnySelf[CC]].builder[DD[A], DD[A]].apply()
  outer.foreach { inner =>
    val builder1 = implicitly[CanBuildAnySelf[DD]].builder[A, A].apply()
    builder1 ++= inner
    builder2 += builder1.result
  }
  builder2.result
}

It’s nice and impressive to see that the CanBuildAnySelf context bound can be used completely separately.

  • 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-05-20T10:57:18+00:00Added an answer on May 20, 2026 at 10:57 am

    We get it done in Scalaz, so it’s certainly possible.

    scala> import scalaz._
    import scalaz._
    
    scala> import Scalaz._
    import Scalaz._
    
    scala> List(Vector(1, 2, 3)).sequence
    res1: scala.collection.immutable.Vector[List[Int]] = Vector(List(1, 2, 3), List(1, 2, 3), List(1, 2, 3))
    
    scala> Seq(List(1), List(3)).sequence
    res2: List[Seq[Int]] = List(List(1, 3))
    
    scala> Vector(ArraySeq(1, 2, 3)).sequence
    res3: scala.collection.mutable.ArraySeq[scala.collection.immutable.Vector[Int]] = ArraySeq(Vector(1, 2, 3), Vector(1, 2, 3), Vector(1, 2, 3))
    

    We only consider ‘well-behaved’ collections, that is ones that can contain arbitrary element types, unlike BitSet. This is witnessed with https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/CanBuildAnySelf.scala

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

Sidebar

Related Questions

No related questions found

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.