On Nov 17, 7:56 am, Konrad Hinsen <[EMAIL PROTECTED]> wrote:
> On Nov 17, 2008, at 13:33, mb wrote:
>
> > vals returns a clojure.lang.APersistentMap$ValSeq, which
> > is not a list. Hence list? returns false and you get the true
> > branch, ie. the thing itself.
>
> Ahhhh..... It looks like a list, but it isn't a list.
>
> > I know. It doesn't help much, but it shows, that you have to take
> > care to distinguish the abstraction vs. the specific implementation.
>
> That's fine with me in principle, but not if the language makes an
> effort to hide the implementation from me.
>
> A fix that works (at least for the examples I tried) is
>
> (defn replace-syms
> [sym-map expr]
> (let [replace #(replace-syms sym-map %)]
> (cond (contains? sym-map expr) (get sym-map expr)
> (vector? expr) (into [] (map replace expr))
> (map? expr) (into {} (map replace (seq expr)))
> (set? expr) (set (map replace expr))
> (seq? expr) (map #(replace-syms sym-map %) expr)
> :else expr)))
>
> I have moved the seq? test to the end just in case that one day
> vectors, sets, or maps implement the seq interface as well.
>
It's best to do this as generally as possible, building on something
like this:
(defn map-same [f coll]
(let [ret (into (empty coll) (map f coll))]
(if (seq? coll)
(reverse ret)
ret)))
(map-same inc [1 2 3 4])
-> [2 3 4 5]
(map-same inc '(1 2 3 4))
-> (2 3 4 5)
(map-same inc (seq [1 2 3 4]))
-> (2 3 4 5)
(map-same inc #{1 2 3 4})
-> #{2 3 4 5}
(map-same (fn [[k v]] [k (inc v)]) {:a 1 :b 2 :c 3})
-> {:a 2, :c 4, :b 3}
Rich
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---