Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand correctly every time you call execute on the object, it shall use a different element. Then because the object has to encapsulate state, so there's no way around the <code>var</code></p> <pre><code>var hosts = collection.immutable.Queue ++ optHosts.getOrElse(List()) def url: String = { val(element,queue) = hosts.pop hosts = queue.enqueue(element) element } </code></pre> <p>Regarding your questions...</p> <blockquote> <p>Somehow I would have to remember an index right?</p> </blockquote> <p>Yes, see above. But also no, see below. </p> <blockquote> <p>Is that one of the cases where it doesn't make sense to use immutable types?</p> </blockquote> <p>Depends. If you want to keep your <code>object RoundRobin</code> then clearly, that object is mutable, and you so you only have the choice between mutable vals and immutable vars. (well you could also have vars that point to mutable structures, but why would you do that?)</p> <p>On the other hand you can also opt for a totally different approach:</p> <pre><code>class RoundRobin private(left: List[String], all: List[String]) { def this(all :List[String]) = this(all, all) assume(all.nonEmpty) val theElement = left.headOption.getOrElse(all.head) def nextRoundRobin = new RoundRobin(if(left.nonEmpty) left.tail else all, all) def execute = { // do something with theElement println(theElement) // return an object that will execute on the next element nextRoundRobin } } new RoundRobin(List("1","2")).execute.execute.execute // prints 1 2 1 </code></pre> <p>When you use this version of RoundRobin, every time you call execute it will give you a round robin object that will use the next element in round-robin fashion. Clearly the object using RoundRobin can again either be mutable or immutable. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload