Skip to content

Instantly share code, notes, and snippets.

@cmiles74
Created May 26, 2010 20:06
Show Gist options
  • Save cmiles74/414973 to your computer and use it in GitHub Desktop.
Save cmiles74/414973 to your computer and use it in GitHub Desktop.
(defn piece-line-breaks
"Returns a vector of the indexes of the line breaks in the piece of text"
[buffer piece]
(let [text-buffer (if (= (piece :buffer) :source)
@(buffer :source)
@(buffer :add))]
(reduce (fn [breaks index]
(if (= \newline (. text-buffer get index))
(conj breaks (+ 1 index))
breaks))
[] (range 0 (. text-buffer length)))))
@cemerick
Copy link

Yup, you got it. :-)

Quickie tips, 'cause I can't help myself I guess :-)

  • (range (.length text-buffer)) will do
  • use (inc index) instead of (+ 1 index) -- the former has a faster inlining

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment