mirror of
https://github.com/richardharrington/robotwar.git
synced 2024-05-31 11:41:29 +00:00
refactored re-seq-with-pos ONE MORE TIME
This commit is contained in:
parent
1aedb1ee29
commit
b5c392114a
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue
Block a user