itaied
itaied

Reputation: 7107

How to modify string by index in Clojure?

I want to modify a string by applying a function to some of its chars (by starting index and length).
For example, I want to increment the ascii representation of the string "aaaaa" from the 2nd index to the 4th.

[start=1 length=3]
"aaaaa" => "abbba"

The only way I could think of is applying map, but it goes over all the sequence.

Upvotes: 1

Views: 998

Answers (4)

Ertuğrul Çetin
Ertuğrul Çetin

Reputation: 5231

Here you go:

(defn replace-str
  [s start-i end-i]
  (apply str (map-indexed (fn [index val]
              (if (and (>= index start-i)
                       (<= index end-i)) 
                  (char (+ (int val) 1))
                  val))
                s)))

(replace-str "aaaa" 1 2)
;=> "abba"

Upvotes: 0

Chris Murphy
Chris Murphy

Reputation: 6509

You could use subs to get the portions you do and don't want to modify. After modification use str to concatenate the result together:

(defn replace-in-str [f in from len]
  (let [before (subs in 0 from)
        after (subs in (+ from len))
        being-replaced (subs in from (+ from len))
        replaced (f being-replaced)]
    (str before replaced after)))

You can call it:

(replace-in-str 
 (fn [sub-str] (apply str (map #(char (+ 1 (int %))) sub-str))) 
 "aaaaa" 
 1 
 3)

Upvotes: 2

ez121sl
ez121sl

Reputation: 2429

Indeed map applies the function to every element in the sequence. One way to get around that is to start with map-indexed. Unlike map, map-indexed passes the element's index as the first argument to the mapping function. When we have element's index, we can use it to choose if we need to perform the operation or just return the element as is.

A solution might look like this:

(defn inc-char [c]
  (char (inc (long c))))

(defn if-in-range [from to f] 
  (fn [i x & args] 
    (if (<= from i (dec to)) 
      (apply f x args)
      x)))

(defn map-subs [from to f s]
  (apply str (map-indexed (if-in-range from to f) s)))

(map-subs 1 4 inc-char "aaaaa")
;; "abbba"

Upvotes: 1

itaied
itaied

Reputation: 7107

I thought of using map-index to execute the operation only on the specified index:

((fn [op start length] (map-indexed (fn [i m] (if (<= start i length)
                         (op m)
                         m)) "aaaaa"))
  #(char (+ 1 (int %)))
  1
  3)

=> (\a \b \b \b \a)

Upvotes: 0

Related Questions