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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:27:35+00:00 2026-05-25T03:27:35+00:00

I’m doing a code review for the Queue3 example in the Programming in Scala

  • 0

I’m doing a code review for the Queue3 example in the Programming in Scala 2nd edition by Odersky et al. And I guess i’m stuck.

Here’s the code:
http://booksites.artima.com/programming_in_scala_2ed/examples/type-parameterization/Queues3.scala

object Queues3 {
  class Queue[T](
    private val leading: List[T], 
    private val trailing: List[T] 
  ) {
    private def mirror = 
      if (leading.isEmpty)
        new Queue(trailing.reverse, Nil)
      else
        this

    def head = mirror.leading.head

    def tail = { 
      val q = mirror 
      new Queue(q.leading.tail, q.trailing) 
    }

    def enqueue(x: T) = 
      new Queue(leading, x :: trailing)
    override def toString = 
      leading ::: trailing.reverse mkString ("Queue(", ", ", ")")
  }

  object Queue {
    // constructs a queue with initial elements `xs'
    def apply[T](xs: T*) = new Queue[T](xs.toList, Nil)
  }

  def main(args: Array[String]) {
    val q = Queue[Int]() enqueue 1 enqueue 2
    println(q)
  }
}

So it’s trying to implement a queue in a functional programming way with speed similar to the imperative way.

So to do this it’ll split the queue into two part so we can append toward the end by constant speed. The whole queue is basically:

leading ::: trailing.reverse

The book is saying that worst case scenario is when the leading is empty.

So if code do this

val q = Queue[Int]() enqueue 1 enqueue 2

Then,
q.leading is List() and q.trailing is List(2,1)

So when I call q.head, the book stated that since leading is empty, mirror will copy everything from trailing, reverse it and set it as leading.

The problem is I don’t think this work because it’s a method? So it doesn’t persist via state. Because I made the code properties public and checked q.leading and q.trailing and the value is the same. What I’m expecting after I do q.head is:

q.leading is List(1,2) and q.trailing is List()

But it is not, am I missing something? Is this some FP paradigm that I’m missing? Because I think it can work the way I think it should work if head and tail methods is change to var.

Thank you for your time.

Edit making the properties public:

private val leading: List[T],
private val trailing: List[T]

edit:
chapter 19 in 1st edition:
http://www.artima.com/pins1ed/type-parameterization.html

  • 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-25T03:27:35+00:00Added an answer on May 25, 2026 at 3:27 am

    You problem is that head and tail methods doesn’t return new Queue. And you are inspecting the old one. Checkout this versions of head and tail. Now they return new Queue in a tuple.

    def head: (T, Queue[T]) = {
        val q = mirror
        (q.leading.head, q)
      }
    
    def tail: (Queue[T], Queue[T]) = {
      val q = mirror
      (new Queue(q.leading.tail, q.trailing), q)
    }
    

    As you can see, mirror works fine.

    val q = Queue[Int]() enqueue 1 enqueue 2
    println(q)
    printLT(q)
    val q1 = q.head
    println(q1._1)
    printLT(q1._2)
    
    def printLT[A](q: Queue[A]) {
      println("leading: " + q.leading)
      println("trailing: " + q.trailing)
    }
    

    Output:

    Queue(1, 2)
    leading: List()
    trailing: List(2, 1)
    1
    leading: List(1, 2)
    trailing: List()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.