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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:02:03+00:00 2026-05-27T15:02:03+00:00

Consider the following sketch for using a loop to empty a queue in Scala:

  • 0

Consider the following sketch for using a loop to empty a queue in Scala:

var queue = immutable.Queue[T]( /* .. some content ..*/ )
while( !queue.isEmpty ) {
   val (record, tempQueue) = queue.dequeue
   queue = tempQueue
   doSomethingWith(record)
}

Is there any trick to avoid the temporary variable tempQueue and get Scala to assign the returned Queue value directly to the loop variable queue? Having to introduce the extra symbol is annoying, plus presumably there may be some superfluous copying (although this might get optimized away, not sure).

Edit 1: of course, as Ionut G. Stan points out, I can skip the pattern matching and take apart the returned pair myself, as in:

while( !queue.isEmpty ) {
   val pair = queue.dequeue
   queue = pair._2
   doSomethingWith(pair._1)
}

So I should refine the question as follows: is there any way to use the syntactic sugar of pattern matching to do this more elegantly? I was hoping for something like this, which unfortunately does not compile:

var queue = immutable.Queue[T]( /* .. some content ..*/ )
var record : A = _
while( !queue.isEmpty ) {
   (record, queue) = queue.dequeue
   doSomethingWith(record)
}
  • 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-27T15:02:04+00:00Added an answer on May 27, 2026 at 3:02 pm

    If you insist on keeping that structure (the while loop, etc.), I don’t see how you can make it shorter, except perhaps:

    var queue = immutable.Queue[T]( /* some content */ )
    while( !queue.isEmpty ) queue.dequeue match {
      case (record, tempQueue) =>
        queue = queue.dequeue
        doSomethingWith(record)
    }
    

    Since you’re using an immutable queue, however, the simplest equivalent code is probably:

    for(record <- queue) {
      doSomethingWith(record)
    }
    

    See also this related question which confirms there is no way to assign to a pre-existing var with pattern-matching notation.

    The Scala Language Specification, Section 4.1, is also clear: pattern-matching-style assignments expand into val definitions, i.e. they will bind a new identifier.

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

Sidebar

Related Questions

Consider following XML document fragment: <Book> <Title>Example</Title> <Content> Some line </Content> <TOC> Again some
Consider following tables: How to skip and take groups from the table? Tried using
How to get the underlying type of an Observable collection using reflection? Consider following
Let's consider following html code: <p> Some text followed by <span>a span element</span> and
Consider following source files 1.cpp #include <iostream> using namespace std; struct X { X()
Consider following object, object A { def setX(x:Int) = { val x1 = x
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
I am using DBContext API from EF 4.1. Consider following entity model (A, B,
Consider following piece of code: declare @var bit = 0 select * from tableA

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.