Skip to content

Instantly share code, notes, and snippets.

@seltzer1717
Last active October 24, 2021 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seltzer1717/577b145548ec8160ac97e5964fec58d3 to your computer and use it in GitHub Desktop.
Save seltzer1717/577b145548ec8160ac97e5964fec58d3 to your computer and use it in GitHub Desktop.
(defn shift
"Takes digit char, returns int offset from zero."
[nbr]
(- (int nbr) (int \0)))
(defn next-pair
"Reducing fn, takes acc, the destructured pair [chr nbr]
and returns next acc."
[acc [chr nbr]]
(conj acc chr (if nbr (char (+ (int chr) (shift nbr))))))
(defn replace-digits
"Takes alpha-digit string, returns digit replaced string."
[s]
(apply str (reduce next-pair [] (partition-all 2 s))))
;; For those who like brevity
(defn replace-digits [s]
(apply str
(reduce (fn [acc [chr nbr]]
(conj acc chr
(if nbr
(char (+ (int chr)
(- (int nbr)
(int \0)))))))
[]
(partition-all 2 s))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment