refactored re-seq-with-pos ONE MORE TIME

This commit is contained in:
Richard Harrington 2013-07-14 21:45:36 -04:00
parent 1aedb1ee29
commit b5c392114a

View File

@ -12,17 +12,15 @@
#{"TO" "IF" "GOTO" "GOSUB" "ENDSUB"}))
(defn re-seq-with-pos
"Like re-seq, but returns a sequence of 2-element vectors
in the form [match pos] where pos is match's position in the original string"
[re initial-s]
(loop [s initial-s
acc []]
(if-let [match (re-find re s)]
(let [match-pos (.indexOf s match)]
(recur (subs s (+ match-pos (count match)))
(conj acc [match
(+ match-pos (- (count initial-s) (count s)))])))
acc)))
"like re-seq, but returns a sequence of 2-element sequences
in the form [match pos], where pos is the index of match in s"
[re s]
(let [matches (re-seq re s)
indexes (reduce (fn [idxs match]
(conj idxs (.indexOf s match (inc (or (last idxs) -1)))))
[]
matches)]
(map list matches indexes)))
(defn build-lex-metadata
[s n]