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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:16:59+00:00 2026-05-22T19:16:59+00:00

Consider such a map: Map(one -> Iterable(1,2,3,4), two -> Iterable(3,4,5), three -> Iterable(1,2)) I

  • 0

Consider such a map:

Map("one" -> Iterable(1,2,3,4), "two" -> Iterable(3,4,5), "three" -> Iterable(1,2))

I want to get a list of all possible permutations of elements under Iterable, one element for each key. For this example, this would be something like:

// first element of "one", first element of "two", first element of "three"
// second  element of "one", second element of "two", second element of "three"
// third  element of "one", third element of "two", first element of "three"
// etc.
Seq(Iterable(1,3,1), Iterable(2,4,2), Iterable(3,5,1),...)

What would be a good way to accomplish that?

  • 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-22T19:17:00+00:00Added an answer on May 22, 2026 at 7:17 pm
    val m = Map("one" -> Iterable(1,2,3,4), "two" -> Iterable(5,6,7), "three" -> Iterable(8,9))
    

    If you want every combination:

    for (a <- m("one"); b <- m("two"); c <- m("three")) yield Iterable(a,b,c)
    

    If you want each iterable to march up together, but stop when the shortest is exhuasted:

    (m("one"), m("two"), m("three")).zipped.map((a,b,c) => Iterable(a,b,c))
    

    If you want each iterable to wrap around but stop when the longest one has been exhausted:

    val maxlen = m.values.map(_.size).max
    def icf[A](i: Iterable[A]) = Iterator.continually(i).flatMap(identity).take(maxlen).toList
    (icf(m("one")), icf(m("two")), icf(m("three"))).zipped.map((a,b,c) => Iterable(a,b,c))
    

    Edit: If you want arbitrary numbers of input lists, then you’re best off with recursive functions. For Cartesian products:

    def cart[A](iia: Iterable[Iterable[A]]): List[List[A]] = {
      if (iia.isEmpty) List()
      else {
        val h = iia.head
        val t = iia.tail
        if (t.isEmpty) h.map(a => List(a)).toList
        else h.toList.map(a => cart(t).map(x => a :: x)).flatten
      }
    }
    

    and to replace zipped you want something like:

    def zipper[A](iia: Iterable[Iterable[A]]): List[List[A]] = {
      def zipp(iia: Iterable[Iterator[A]], part: List[List[A]] = Nil): List[List[A]] = {
        if (iia.isEmpty || !iia.forall(_.hasNext)) part
        else zipp(iia, iia.map(_.next).toList :: part)
      }
      zipp(iia.map(_.iterator))
    }
    

    You can try these out with cart(m.values), zipper(m.values), and zipper(m.values.map(icf)).

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

Sidebar

Related Questions

Consider the following comma separated file. For simplicity let it contain one line: 'I
Consider the following list of checkboxes - a list of online and a list
Consider the following (tested with Scala 2.8.1 and 2.9.0): trait Animal class Dog extends
Consider I have the following dictionary: dic={} dic[var1]=val1 dic[var2]=val2 Now I have another object
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
consider the following html structure: <div> <div class=top> <div id=id1>123</div> <div> <div class=top> <div
I'm using Microsoft's idea for storing resource and booking information. In short, resources, such
I'm learning how to sanitize my forms and know that using the PHP function
Any portable code that uses bitfields seems to distinguish between little- and big-endian platforms.

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.