Created
November 27, 2011 17:53
-
-
Save damionjunk/1397899 to your computer and use it in GitHub Desktop.
String balanced reduction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Indeed! I had originally written the piece of code to only consider [ and went back and added the ls and rs!