Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to map all but the first item?

Tags:

clojure

I feel like there must be a cleaner way to do this, but I don't see it.

(defn map-rest
  "Passes through the first item in coll intact. Maps the rest with f."
  [f coll]
  (concat (list (first coll)) (map f (rest coll))))
like image 629
duelin markers Avatar asked Oct 22 '12 16:10

duelin markers


1 Answers

Destructuring and using cons instead of concat:

(defn map-rest [f [fst & rst]] (cons fst (map f rst)))

REPL output:

user=> (map-rest inc [1 2 3 4 5])
(1 3 4 5 6)
like image 74
noahlz Avatar answered Nov 17 '22 11:11

noahlz