0

If I have a list

("foo" "bar" "" "baz")

and I need to change any "" to "biz", what is a good way to go about that?

1
  • 1
    I'd use map, what did you try? Commented Feb 14, 2018 at 23:50

2 Answers 2

7

Just for completeness, here's an alternate method that uses a more specialized built-in:

(replace ; Replace all instances of... 
  {"" "biz"} ; "" with "biz"... 
  '("foo" "bar" "" "baz")) ; in the list

Which also returns a lazy sequence.

Note that the map given as the first argument can contain multiple entries. If you have multiple replacements, I'd definitely go with replace over an explicit map.

replace actually just uses map behind the scenes, but uses a map lookup instead of a equality check to do the replacements. I would expect them to preform similarly for one replacement, but for replace to be faster for more than once since it doesn't need to do a linear search over all the replacements like you would doing manual = checks.

Sign up to request clarification or add additional context in comments.

Comments

5
(map #(if (empty? %) "biz" %)
     '("foo" "bar" "" "baz"))

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.