On Mon, Mar 19, 2012 at 2:40 PM, 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))

If coll is always a collection of vectors, then you can simply use
'assoc' since vectors support near-constant-time random access. Also,
the name "map-first" is a bit misleading in my opinion, since it does
not map f over the first element in coll, but over the first element
of each collection in coll.

Here's an example using assoc:

    (defn map-first [f v]
      (assoc v 0 (f (nth v 0))))

    (defn map-firsts [f coll]
      (map #(map-first f %) coll))

    (map-firsts #(* 2 %) [[2] [3 3] [3 [2 4]] [4 10]])
    ;=> ([4] [6 3] [6 [2 4]] [8 10])

The complexity of map-firsts is O(log_32(n) * m). The log_32(n) part
comes from assoc, which creates a new updated vector with n elements.
(log_32 is practically close to constant. log_32 of four billion is
about 6 or 7) The m part comes from
 mapping over the outer collection (of size m). The complexity can be
approximated to just O(m) if you approximate log_32 as constant time.
The 'assoc' here is very cheap.

// raek

-- 
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

Reply via email to