Skip to content

Instantly share code, notes, and snippets.

@damionjunk
Created November 27, 2011 17:53
Show Gist options
  • Save damionjunk/1397899 to your computer and use it in GitHub Desktop.
Save damionjunk/1397899 to your computer and use it in GitHub Desktop.
String balanced reduction.
(defn b-reduction
"Reduces a string to un-balanced left and right enclosing characters."
[s & {:keys [ls rs] :or {ls \[ rs \]}}]
(reduce (fn [v ch]
(if (= ch ls)
(conj v ch)
(if (= ch rs)
(if (= ls (peek v))
(pop v)
(conj v ch))
v)))
[]
s))
@damionjunk
Copy link
Author

Indeed! I had originally written the piece of code to only consider [ and went back and added the ls and rs!

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