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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:32:11+00:00 2026-05-21T04:32:11+00:00

the other day I was wondering why scala.collection.Map defines its unzip method as def

  • 0

the other day I was wondering why scala.collection.Map defines its unzip method as

def unzip [A1, A2] (implicit asPair: ((A, B)) ⇒ (A1, A2)): (Iterable[A1], Iterable[A2])

Since the method returns “only” a pair of Iterable instead of a pair of Seq it is not guaranteed that the key/value pairs in the original map occur at matching indices in the returned sequences since Iterable doesn’t guarantee the order of traversal. So if I had a

Map((1,A), (2,B))

, then after calling

Map((1,A), (2,B)) unzip

I might end up with

... = (List(1, 2),List(A, B))

just as well as with

... = (List(2, 1),List(B, A))

While I can imagine storage-related reasons behind this (think of HashMaps, for example) I wonder what you guys think about this behavior. It might appear to users of the Map.unzip method that the items were returned in the same pair order (and I bet this is probably almost always the case) yet since there’s no guarantee this might in turn yield hard-to-find bugs in the library user’s code.

Maybe that behavior should be expressed more explicitly in the accompanying scaladoc?

EDIT: Please note that I’m not referring to maps as ordered collections. I’m only interested in “matching” sequences after unzip, i.e. for

val (keys, values) = someMap.unzip

it holds for all i that (keys(i), values(i)) is an element of the original mapping.

  • 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-21T04:32:12+00:00Added an answer on May 21, 2026 at 4:32 am

    Actually, the examples you gave will not occur. The Map will always be unzipped in a pair-wise fashion. Your statement that Iterable does not guarantee the ordering, is not entirely true. It is more accurate to say that any given Iterable does not have to guarantee the ordering, but this is dependent on implementation. In the case of Map.unzip, the ordering of pairs is not guaranteed, but items in the pairs will not change they way they are matched up — that matching is a fundamental property of the Map. You can read the source to GenericTraversableTemplate to verify this is the case.

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

Sidebar

Related Questions

I was just wondering this the other day. I am not exactly sure how
I was using a custom method to deep clone objects the other day, and
I was reading this article the other day and was wondering why there was
The other day I was looking into Zend Server and I was wondering why
I was wondering the other day when typing you selectors in jQuery would it
I was wondering the other day if StackOverflow had an API I could access
The other day I came across a Python implementation called Jython. With Jython you
The other day this code was working. I changed some things and re-ran it
The other day, I came across this construct: static_cast<size_type>(-1) in some example C++ code,
The other day I saw a colleague of mine using sort to sort a

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.