Queue3 Chapter 19 in Programming in Scala 2nd edition Question -
I'm reviewing a code for example QA 3 in programming in Scala 2 version by Odarsky et al. And I think I'm stuck
Here's the code:
Object Queues3 {class queue [T] (Head of Personal Val: List [T], Private Val follow: list [t]} {private def mirror = if (leading .isEmpty) new Q (trailing.reverse, zero) and it def define = mirror.leading.head def tail = {val q = mirror new queue (q .leading.tail, q.trailing)} Def N (x: T) = New Line (Key, X :: Indexed) Override def toString = Leading ::: trailing.reverse mkString ("queue (", "," ")"}} Object Q {// prepares a queue with the initial element `xs' def [T] (xs: (*) * New line [T] (Exits list, zero)} Def main (args: array [string]] {val q = que [int] nq1nq 2 printlen (q)}} Therefore, it is trying to implement the queue in a functional programming manner whose speed is inevitably.
To do this, it will split the queue into two halves so that we can The whole queue is basically:
Leading: : Trailing.reverse It is saying that the worst case occurs when the key is empty. / P>
So if the code does this
val q = queue [int] () ninety 1 nu 2 then, q. The leading list is () and q.trailing list (2,1)
So when I say q.head, the book said that since the head is empty, the mirror will copy everything from the back, it Inverted and set it as the leading
The problem is that I do not consider this work because it's a way? So it continues through the 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 Q.head is:
is q.leading list (1,2) and q.trailing list () But it is not, am I missing something? Is this some FP paradigm, which I am missing?
Thank you for your time.
Edit to create properties:
Personal Wall Head: List [T], Personal Val Followers: List [T]
Edit: Chapter 1 19th Edition:
The problem is that head and tail methods do not return new Q and you are inspecting the old one < Check out these versions of code> head and tail . Now they return cue to a new tube. Def head: (T, Qi [T]) = {val q = mirror (q.leading.head, Q)} Def tails: (Q [T], Q [T] = {Val q = mirror (new qi (q.leading.tail, q.trailing), q)} As you can see, mirror Works fine. val q = queue [int] () nq1nq 2 printlen (q) printLT (q) val q1 = q.head println (q1._1) printLT ( Q1._2) def printLT [A] (Q: Qi [A]) {println ("leading:" + q.leading) println ("trailing:" + q output: qi (1, 2) Leading: List () Following: List (2, 1) 1 Leading: List (1, 2) Following: List ()
Comments
Post a Comment