Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In clojure, how to write a function that applies several string replacements?

Tags:

clojure

I would like to write a function replace-several that receives a string and a set of replacements and apply all the replacements (where the replacements see the result of the previous replacements).

I thought about the following interface:

(replace-several "abc" #"a" "c" 
                       #"b" "l"
                       #"c" "j"); should return "jlj" 

Two questions:

  1. Is it the most idiomatic interface in clojure?
  2. How to implement this function?

Remark: To make a single replacement, there is replace available in clojure.string.

like image 223
viebel Avatar asked Mar 05 '12 14:03

viebel


1 Answers

(str/escape "abc" {\a "c" \b "l" \c "j"})

; => "clj"

Docs escape

like image 178
magpie Avatar answered Oct 10 '22 06:10

magpie