2

Given an array contains elements such as:

[":test" ":do_this" "dont" ":_another_one"]

I need to remove the : and replace it with a empty string which can be done using:

(clojure.string/replace element #":" "")

However how can I apply this to every element? I have looked at using the apply function but it doesn't seem to modify the elements, is this because they are in fact immutable?

(apply function collection)

Is what I've been looking at doing but no luck so far.

1
  • 3
    To clarify the functionality of the apply and map functions (as a supplement to the correct answer by Lee), apply will start as a call like this: (apply f [1 2 3]) and do this: (f 1 2 3), evaluating the function once by giving it all the elements of the list as arguments, resulting in a single output value; map, OTOH, will start as this: (map f [1 2 3]) and do this ((f 1) (f 2) (f 3)), where the function will be applied to each element of the original list, resulting in a new list. Commented Sep 1, 2015 at 12:25

1 Answer 1

6

Use map:

(let [input [":test" ":do_this" "dont" ":_another_one"]
      updated (map #(clojure.string/replace % #":" "") input)]
  ...)

this returns a sequence instead of a vector, so use mapv if you want to create a new vector.

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

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.