2

I'm having problems creating a function that replaces all occurrences of a value in a sequence.

Example: replace 'a' to 'z'; input:

((a b) f ((a b c) (e r) a) a)

expected output:

((z b) f ((z b c) (e r) z) z)

Any ideas?

2 Answers 2

5

prewalk-replace is slightly simpler than @mobyte's answer if you're strictly swapping one value for another:

(def thing '( (a b) f ( (a b c) (e r) a ) a ))

(use '[clojure.walk :only [prewalk-replace]])

(prewalk-replace {'a 'z} thing)
; ((z b) f ((z b c) (e r) z) z
Sign up to request clarification or add additional context in comments.

Comments

1
(use '[clojure.walk :only (postwalk)])

(postwalk #(if (= % 'a) 'z %) '( (a b) f ( (a b c) (e r) a ) a ))
-> ((z b) f ((z b c) (e r) z) z)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.