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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:32:36+00:00 2026-06-18T09:32:36+00:00

I want to have a binary operator cross (cross-product/cartesian product) that operates with traversables

  • 0

I want to have a binary operator cross (cross-product/cartesian product) that operates with traversables in Scala:

val x = Seq(1, 2)
val y = List('hello', 'world', 'bye')
val z = x cross y    # i can chain as many traversables e.g. x cross y cross w etc

assert z == ((1, 'hello'), (1, 'world'), (1, 'bye'), (2, 'hello'), (2, 'world'), (2, 'bye'))

What is the best way to do this in Scala only (i.e. not using something like scalaz)?

  • 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-18T09:32:37+00:00Added an answer on June 18, 2026 at 9:32 am

    You can do this pretty straightforwardly with an implicit class and a for-comprehension in Scala 2.10:

    implicit class Crossable[X](xs: Traversable[X]) {
      def cross[Y](ys: Traversable[Y]) = for { x <- xs; y <- ys } yield (x, y)
    }
    
    val xs = Seq(1, 2)
    val ys = List("hello", "world", "bye")
    

    And now:

    scala> xs cross ys
    res0: Traversable[(Int, String)] = List((1,hello), (1,world), ...
    

    This is possible before 2.10—just not quite as concise, since you’d need to define both the class and an implicit conversion method.

    You can also write this:

    scala> xs cross ys cross List('a, 'b)
    res2: Traversable[((Int, String), Symbol)] = List(((1,hello),'a), ...
    

    If you want xs cross ys cross zs to return a Tuple3, however, you’ll need either a lot of boilerplate or a library like Shapeless.

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

Sidebar

Related Questions

I have a binary that I want to convert to standard MIME-compliant base64 string.
I am writing some Natural Language Processing code and want to have a binary
I have one binary file and I want to read this file like first
I have a binary file to which I want to append a chunk of
I have a binary tree where each node can have a value. I want
I have a Binary tree for a mathematical expression(infix), i want to convert directly
Say I have a binary file of 12GB and I want to slice 8GB
If I have a 32-bit binary number and I want to replace the lower
I want to create a Binary file of my project. I have set my
i have the variable $a = 5; i want to show it in binary

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.