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 3798180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:31:48+00:00 2026-05-19T13:31:48+00:00

Suppose I have a list of strings and I use zipWithIndex to transform it

  • 0

Suppose I have a list of strings and I use zipWithIndex to transform it to a list of tuples:

List("a", "b", "c").zipWithIndex
res1: List[(java.lang.String, Int)] = List((a,0), (b,1), (c,2))

I’d like to write an unzipWithIndex method that performs the reverse transformation, i.e. a method which, when applied to a list of tuples whose second elements are a permutation of the first N integers, returns the first elements in unpermuted order:

List(("c",2), ("a",0), ("b",1)).unzipWithIndex
res2: List[java.lang.String] = List(a, b, c)

The method should work on any suitable collection of 2-tuples whose second elements are of type Int, preferably using the Pimp My Library pattern. How would I go about this with Scala 2.8 collections?

  • 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-19T13:31:49+00:00Added an answer on May 19, 2026 at 1:31 pm
    object Test {
      import collection.generic.CanBuildFrom
    
      class Unzip[T, CC[X] <: Traversable[X]]
        (coll: CC[(T, Int)])
        (implicit bf: CanBuildFrom[CC[(T, Int)], T, CC[T]]) {
        def unzipWithIndex: CC[T] = bf(coll) ++= (coll.toSeq sortBy (_._2) map (_._1)) result
      }
    
      implicit def installUnzip[T, CC[X] <: Traversable[X]]
        (coll: CC[(T, Int)])
        (implicit bf: CanBuildFrom[CC[(T, Int)], T, CC[T]]) = new Unzip[T, CC](coll)
    
      def main(args: Array[String]): Unit = {
        val x1 = util.Random.shuffle("abcdefgh".zipWithIndex)
    
        println("x1 shuffled = " + x1)
        println("x1.unzipWithIndex = " + x1.unzipWithIndex)
    
        val x2 = (1 to 10).toSet.zipWithIndex
    
        println("x2 = " + x2)
        println("x2.unzipWithIndex = " + x2.unzipWithIndex)
      }
    }
    
    % scala Test
    x1 shuffled = Vector((f,5), (g,6), (c,2), (d,3), (e,4), (a,0), (h,7), (b,1))
    x1.unzipWithIndex = Vector(a, b, c, d, e, f, g, h)
    x2 = Set((8,8), (2,5), (3,7), (5,0), (9,4), (4,9), (6,3), (10,1), (7,6), (1,2))
    x2.unzipWithIndex = Set(5, 10, 1, 6, 9, 2, 7, 3, 8, 4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a list of strings, like this: var candidates = new List<String>
Suppose I have a list of strings where each string is exactly 4 characters
Suppose I have a list of dates in the string format, 'YYYYMMDD.' How do
Suppose I have a list (or Set): List<String> testList = Lists.newArrayList(assocX,srcT,destA,srcX, don't care Y,
Suppose I have a list like this <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li>
Possible Duplicate: How do I use LINQ Contains(string[]) instead of Contains(string) Suppose I have
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
I have a list of strings (a List<String> ) that can have anywhere from
Suppose I have a function list_fun : int_list -> string list and in that
Suppose I have a list, in which no new nodes are added or deleted.

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.