Hello everyone,

I found that a vector being associative really enlightening and
useful. But merge-with and other functions don't work, even though I
think it is not unreasonable to expect that. That is because
(seq [1 2 3 4]) is the seq of Long while (seq {0 1, 1 2, 2 3, 3
4}) is a seq of MapEntry, which merge-with relies on to work.

There are various ways to address this problem: map comes immediately to
mind. However I think there is an inconsistency in the definition of a
seq. Which could be addressed this way:

(defn associative-seq
  ([vector]
     (associative-seq 0 vector))
  ([current-idx vector]
     (reify
       clojure.lang.ILookup
       (valAt [this idx]
         (nth vector idx))
       (valAt [this idx not-found]
         (nth vector idx not-found))

       clojure.lang.Associative
       (containsKey [this idx]
         (< idx (count vector)))
       (entryAt [this idx]
         (clojure.lang.MapEntry. idx (nth vector idx)))
       (assoc [this idx val]
         (assoc vector idx val))

       clojure.lang.ISeq
       (first [this]
         (if-let [fst (first vector)]
           (clojure.lang.MapEntry. current-idx fst)
           nil))
       (next [this]
         (if (seq vector)
           (associative-seq (inc current-idx) (next vector))
           (seq (first this))))

       clojure.lang.Seqable
       (seq [this]
         (if-let [nxt (next this)]
           (cons (first this) nxt)
           nil)))))

Actually to make it consistent, the seq would only ever return the vals
of a map, while associative-seq would always contain MapEntries,
when the underlying datastructure can be interpreted as associative.

Thoughts?

Regards

Robert

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to