On Mon, Mar 19, 2012 at 9:40 AM, Bojan <bojandoli...@hotmail.com> wrote: > Hi! > > I'm a beginner at clojure and enjoying the learning process. While writing > my first nontrivial program, I noticed that I'm transforming only first > elements in the list, so I factored this transformation out by writing next > function: > > (defn map-first [f coll] > (map #(cons (f (first %)) (rest %)) coll)) > > And it almost works as expected: > Clojure> (map-first #(* 2 %) '((2) (3 3) (3 [2 4]) [4 10])) > ((4) (6 3) (6 [2 4]) (8 10)) > > Note that the last element of the list is a vector, but ends up as a list. > Sure, my program doesn't complain and I don't see a scenario where this > might be a problem, but I wondered if there is some way to preserve vectors.
Cons returns lists. (Well, seqs. For the most part the distinction is unimportant.) Doing what you ask is tricky. Changing cons to conj doesn't help (as rest returns a seq too). This: (defn map-first [f coll] (map #(into (empty %) (cons (f (first %)) (rest %))) coll)) reverses all the non-vectors. I doubt it can be done without the ugliness of introducing a conditional that checks for vector-ness or seq-ness. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en