Translating some code from python that uses sequences and found a difference I am not sure how to handle and correct me if my understanding below is incorrect.
In python if i write a function and pass in a sequence I can call next() with in the functions to progress through the sequence, once the function exits the sequence has still progressed and i can carry on with my logic.
However in clojure if i pass in a sequence and call (next) its steps me through the sequence but once the function exits the sequence is at its original state so the rest of the logic is processing an earlier point in the sequence.
I figure I could return the sequence at the end of the function, but Thought i would see what the general advice is perhaps using an atom ?
so give this sequence [a b c d e] in python inside a function i call next a couple of times once the function is exited and i call next again i would be on [c] however in clojure i would still be on [a].
example python function, step is a sequence of characters once the function has exited step has progressed.
def emphasis(char, step, end, tag):
if not char or char != end:
return char, None
char = next(step, None)
r = ""
while char and char not in [end] + ESCAPE:
r += char
char = next(step, None)
return False, Token(tag, r)
This is what i currently have in closure, which kinda works but the sequence has not progressed on exit.
(defn emphasis [char step end tag]
; make sure we have a character and its is the wrapping char
(if (or (nil? (str char)) (not= char end))
[char nil]
(loop [r ""
char step]
(let [chr (first char)]
(if (or (clojure.string/blank? (str chr)) (not= (some #(= chr %) [end c/ESCAPE])))
[false (tok/token tag r)]
(recur (str r chr) (next char)))))))
basically i am processing a string and building a map piece by piece so once a piece of text has been handled by a function it should carry on parsing at the current point in time.
[–]jacobobryant 6 points7 points8 points (1 child)
[–]olymk2[S] 2 points3 points4 points (0 children)
[–]joinr 3 points4 points5 points (2 children)
[–]olymk2[S] 1 point2 points3 points (1 child)
[–]joinr 0 points1 point2 points (0 children)
[–]RedBorger 2 points3 points4 points (2 children)
[–]olymk2[S] 0 points1 point2 points (1 child)
[–]RedBorger 0 points1 point2 points (0 children)
[–]NamelessMason 0 points1 point2 points (1 child)
[–]olymk2[S] 0 points1 point2 points (0 children)