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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:31:06+00:00 2026-05-25T12:31:06+00:00

From googling around, I found that using while loops or using variables is discouraged.

  • 0

From googling around, I found that using while loops or using variables is discouraged.

Now I implemented a very simple algorithm that will read characters from an inputstream and parse accordingly: if input is 10:abcdefghej it will parse out 10 then read next 10 bytes after the colon.

The thing I am kinda lost with is how I can refactor this so it does not rely on variables.


(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)))

Also, I understand that the only way to declare variables is using the with-local-vars 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?

  • 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-25T12:31:06+00:00Added an answer on May 25, 2026 at 12:31 pm

    A bit late to this party, I suppose, but the problem is much simpler if you just treat the string as a sequence of characters and use Clojure’s sequence-handling primitives:

    (defn read-prefixed-string [stream]
      (let [s (repeatedly #(char (.read stream)))
            [before [colon & after]] (split-with (complement #{\:}) s)
            num-chars (read-string (apply str before))]
        (apply str (take num-chars after))))
    
    user> (let [in (java.io.StringReader. "10:abcdefghij5:klmnopqrstuvwxyz")]
            (repeatedly 2 #(read-prefixed-string in)))
    ("abcdefghij" "klmno")
    

    A summary:

    • Convert the ugly, side-effectful input stream into a lazy sequence of characters, so that we can pretend it’s just a string for the rest of this operation. As you can see, no more characters are actually read from the stream than are necessary to compute the result.
    • Split the string into two parts: the first half characters before the first colon, and the second half whatever is left.
    • Use destructuring to bind those parts to locals named before and after, and strip out the : while we are at it, by binding it to an unused local, named colon for descriptiveness.
    • Read before to get its numerical value
    • Take that many characters from after, and mash them all together into a string with (apply str)

    Svante’s answer is an excellent example of how to write loop-ish code with Clojure; I hope that mine is a good example of assembling the built-in functions so that they do what you need. Certainly both of these make the C solution look anything but “very simple”!

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

Sidebar

Related Questions

I'm now switching to VIM from TextMate. I found ^+W in INSERT mode very
Update III: After googling around I've found that this is not an uncommon problem.
From googling around it looks like Xcode (3.1 in my case) should be at
I'm trying to understand something that , by googling around, seems like a bug
I have been googling, a lot, and found quiet many similar issues around the
I am looking for information on how to draw flame fractals from googling around
I've seen on StackOverflow and googling around that the most used way to get
I have been googling around for about an hour, and I still don't found
I've been messing around with Android development. I made a simple form that has
So I have been Googling and I am quite sure my problem emerge from

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.