Big thanks to everyone, the suggestions given are all very welcome,
even if I didn't really needed a better version as my use of mappad is
really simple for now. It was just curiosity on my part.

The lazy version by Heinz could be quite useful in other situations,
I've added it to my toolbox. Using lazy sequence with recursion is a
really elegant way to resolve most problems, it make up for the lack
of TCO in lots of cases. Also, the use of fnil is interesting. It
seems like a quite useful function, but it's a little bit confusing,
need more studying on that one. So to wrap up this post here's the
code for both lazy and non-lazy version of mappad:

(defn extend-tuple
  "Lazily creates tuples from given colls, filling the ones that are
  empty before the rest with the default value."
  [default & colls]
  (if (some (complement empty?) colls)
    (cons
      (map (fn [l] (if (empty? l) default (first l))) colls)
      (lazy-seq (apply extend-tuple default (map next colls))))
    nil))

(defn append-default
  "Returns the given colls padded with the given default value."
  [default & colls]
  (let [lengths (map count colls)
        maxlen (apply max lengths)]
    (map #(concat %1 (repeat (- maxlen %2) default)) colls lengths)))

(defn mappad
  "Like map but if collections aren't all of the same size, the
smaller
  ones are padded with the given default value."
  [f default & colls]
  (apply map f (apply append-default default colls)))

(defn lazy-mappad
  "Like mappad but lazy."
  [f default & colls]
  (map #(apply f %) (apply extend-tuple default colls)))

As you see, I renamed it extend-tuple as tupel was probably a typo
unless I'm mistaken. I'd like to find a better name, but these kind of
functions are always hard to name. I'll eventually release these and
other simple helpers in a small library where I keep these things. But
maybe one of these version (the lazy one at least) warrant being
included in clojure.contrib.seq-utils.  I'll open a ticket for it in
the Assembla page later this week.

Thanks again and happy new year!

- budu

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