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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:00:46+00:00 2026-05-25T10:00:46+00:00

I’m going through Programming In Scala 2nd Edition by Odersky, Spoon, and Venners, and

  • 0

I’m going through Programming In Scala 2nd Edition by Odersky, Spoon, and Venners, and this example threw me for a loop since it seemed to go against what I thought was true about functional programming and immutability. In the example (and earlier in the book in Ch. 18), the authors claim that operations on an object can still be “purely functional” even when those operations might internally mutate the state of the object. The example, which is on p.442, Ch. 19, is this:

 class Queue[+T] private (
   private[this] var leading: List[T], 
   private[this] var trailing: List[T]
 ) {

   private def mirror() = 
     if (leading.isEmpty) {
       while (!trailing.isEmpty) {
         leading = trailing.head :: leading
         trailing = trailing.tail
       }
     }

   def head: T = { 
     mirror()
     leading.head 
   }

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

   def enqueue[U >: T](x: U) = 
     new Queue[U](leading, x :: trailing)
 }

The justification given is that, so long as it side effects are not visible to clients, something like this can be considered functional. I guess I can get behind that…I mean strictly speaking that’s what defines a function. But admittedly (and I’m not really too savvy about what the JVM memory model guarantees), but aren’t there potential problems with that in this code?

For instance, if two threads are running operations on this queue, which looks like this to start:

Leading: Nil
Trailing: List(1,2,3,4)

Isn’t it possible that one thread could call head() getting to this point in mirror() before being descheduled:

private def mirror() = 
     if (leading.isEmpty) {
       while (!trailing.isEmpty) {
         leading = trailing.head :: leading
         > trailing = trailing.tail
       }
     }

At which point the queue looks like this:

Leading: List(1)
Trailing: List(1,2,3,4)

And when a second thread calls tail() while the first is not active, this would be returned:

Leading: Nil
Trailing: List(1,2,3,4)

Whereas if the first thread’s head() call were to finish, this would be returned after a subsequent tail() call on that list:

Leading: List(2,3,4)
Trailing: Nil

I’m admittedly not great at this kind of stuff and concurrent programming is really mindbending for me, as I’m sure it is for a lot of people, I’m just curious what I’m missing here.

  • 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-25T10:00:47+00:00Added an answer on May 25, 2026 at 10:00 am

    You’re correct: functional-looking code can be rendered non-functional when used by multiple threads if it uses internal state as part of its implementation. You can fix this by synchronizing every method that touches the mutable variables (i.e. with a synchronized block). Unfortunately, this is not ideal for performance, which is why in a threaded application functional implementations are often preferred.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string

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.