0

I'm working on a function, which takes a vector (possibly nested vector) as input along with some quantity y and index n. Essentially the function would append y after the nth element in the vector and adjoin the remaining elements. So far I have the following written, which isn't not working as planned:

(defn funcs [x y n]
(concat (take (- n 1) x) (concat (take-last (- (count x) n) y))))
sunspots
  • 1,047
  • 13
  • 29

1 Answers1

3

If you want to return a vector as the final value, you'll have to concatenate your vectors using into (in time linear in the size of the right operand) or core.rrb-vector's catvec (logarithmic time, but the resulting vector will be somewhat slower overall).

As for the actual implementation, assuming you want to go with core.rrb-vector:

(require '[clojure.core.rrb-vector :as fv])

(defn append-after-nth [x y n]
  (fv/catvec (fv/subvec x 0 n) y (fv/subvec x n)))
Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212
  • so much better than what my naive solution would have been! – Jed Schneider Jan 13 '14 at 16:52
  • The dependency `[org.clojure/core.rrb-vector "${version}"]` is giving me troubles. It says there is an illegal character in path at index 59. This is the first time I've ever experienced such issues with lein deps. – sunspots Jan 13 '14 at 17:08
  • 1
    You need to provide the actual version you want in lieu of "${version}", e.g. "0.0.10". – A. Webb Jan 13 '14 at 17:16