lisp - Clojure Variables and Looping -
Googling around, I found out that while
are loops or frustrated using the variable .
I have now implemented a very simple algorithm that reads the letters from an inputstream and parse accordingly: If input is 10: abcdefghej
then it will be 10 < / Code> and then read 10 bites after the colon.
I have lost a little bit this is how I can refactor it, it does not depend on the variable.
(defn decode-string [input-stream indicator] (with-local-wares [length (str (four pointer)) delimiter (read input-stream) string (str "") counter 0] (Var () (var-set delimiter) (var-set delimiter) (var-set delimiter) (var-set delimiter) (Var-set delimiter) (Var-set length (New BigInteger (var) - Gate length)) (var-set length) (while (no? (Var-get string (Var-set string) ( Var-set string (str (var (get)) (var-get string))) (var-get string)))
In addition, I understand that the only way to declare variable is using the local-word
keyword is not ineffective to define all the variables in one block at the beginning, or Do I miss some important point?
This is a bit late for the party, I It seems, but the problem is very simple if you get the string as the sequence of characters The switch to only recognize and use sequence-handling Primitiws of closure are:
(Leave the predecessor-string [stream] (REPEAT (Repeat # (char (. Stream) (before the [colon and later]] [the partition (# {\:} supplement) Num-chars (read-string (apply str first)] (apply (take after num-chars) )) User & gt; ((Java.io.StringReader. "10: abcdefghij5: klmnopqrstuvwxyz")] (repeatedly # 2 (read-prefix-string in)) ("ABCDGG" "KLMano")
< P> A summary: - Convert the ugly, side-effective input stream to lazy sequences, so that we can show that it is a string for the rest of this operation. As you can see, more than the required letters are not actually read from the stream to calculate the result.
- Divide the string into two halves: the first half-letter before the first colon, and the other half left.
- Demolition to exclude those parts from the name
first
andfollowed by
and to remove:
Use - to get its numerical value
first
- Move that code to
after
, and mash all together in a string with
Swant's answer is an excellent example of writing loop-iis codes with the closure; I hope that my inbuilt work is a good example of uniting them so that they meet your needs. Of course, both of these solutions make anything to anything but "very simple"!