title: Clojure Variables and Looping
body: <p>From googling around, I found that using <code>while</code> loops or using variables is discouraged.</p>
  
  <p>Now I implemented a very simple algorithm that will read characters from an inputstream and parse accordingly: if input is <code>10:abcdefghej</code> it will parse out <code>10</code> then read next 10 bytes after the colon.</p>
  
  <p>The thing I am kinda lost with is how I can refactor this so it does not rely on variables.</p>
  
  <pre><code>
  (defn decode-string [input-stream indicator]
  
    (with-local-vars [length (str (char indicator) )
              delimiter (.read input-stream ) 
              string (str "")
              counter 0 ]
  
      (while (not(= (var-get delimiter) 58 ))
         (var-set length (str (var-get length) (char (var-get delimiter)) ))
         (var-set delimiter (.read input-stream )))
  
      (var-set length (new BigInteger (var-get length)) )
      (var-set counter (var-get length))
  
      (while (not(zero? (var-get counter) ))
         (var-set string (str (var-get string) (char (.read input-stream ))  ))
         (var-set counter (dec (var-get counter))))
      (var-get string)))
  </code></pre>
  
  <p>Also, I understand that the only way to declare variables is using the <code>with-local-vars</code> keyword. Isn't it kind of unpractical to define all the variables in one block at the beginning, or am I missing some crucial point?</p>