2

As a Clojure newbie, I'm bothered with this small problem:

I would like to iterate through a sequence and execute a split, and then a str (concatenation) function over the sequence elements.

Here is my sequence:

(("2.660.784") ("2.944.552") ("44.858.797"))

What I want to get is something like this:

("2660784" "2944552" "44858797")

And this is my attempt of creating recursive solution for my problem:

(defn old 
      [squence]
      (let [size (count squence)]
        (loop [counter 1]
          (if (<= counter size)
            (apply str (clojure.string/split 
                   (first (first squence))
                   #"\b\.\b"
                   ))
            (old (rest squence)))
          )))

And of course, this is not a solution because it applies split and str only to one element, but I would like to repeat this for each element in squence. The squence is product of some other function in my project.

I'm definitely missing something so please help me out with this one...

4 Answers 4

6

The simplest way to write it is with replace, rather than split/str. And once you've written a function that can do this transformation on a single string, you can use map or for to do it to a sequence of strings. Here I had to destructure a bit, since for whatever reason each element of your sequence is itself another sequence; I just pulled out the first element.

(for [[s] '(("2.660.784") ("2.944.552") ("44.858.797"))]
  (clojure.string/replace s #"\b\.\b" ""))
Sign up to request clarification or add additional context in comments.

3 Comments

Note that in clojure one should prefer vectors over quoted lists when applicable.
Hi,first of all thanks for reply. The reason why are these strings in separated lists (within a list) is that they are product of mine clojure/enlive selector steps functions, therefore, I can't mess with it. I'am doing this, so I could parse them in to numbers (integers to be exact), basically on resulting collection I would like to apply Java Integer.parse ("string") method. Am I on right track, or is there some easier way to do this?
@SridharRatnakumar: Performance. Vectors are arrays, lists are linked lists. Arrays are generally faster except for when it comes to adding or removing items.
3
user=> (defn reject-char-from-string
  [ch sequence]
  (map #(apply str (replace {ch nil} (first %))) sequence))
#'user/reject-char-from-string
user=> (reject-char-from-string \. '(("2.660.784") ("2.944.552") ("44.858.797"))
)
("2660784" "2944552" "44858797")

Comments

1

Tried this?

=> (flatten '(("2.660.784") ("2.944.552") ("44.858.797")))
("2.660.784" "2.944.552" "44.858.797")

Comments

1

Is it as simple as this?

(def data '(("2.660.784") ("2.944.552") ("44.858.797")))
(require '[clojure.string :as string])
(map #(string/replace (first %1) "." "") data)
;=> ("2660784" "2944552" "44858797")

2 Comments

Pretty much, and after I have to convert the strings into numbers (parsing with Java's integer.parse method). Is there some more elegant solution?
How about this? (map #(-> (first %1) (string/replace "." "") (Integer/parseInt)) data)

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.