[Cs254f11] subst

Wm. Josiah Erikson wjerikson at hampshire.edu
Fri Nov 18 14:26:06 EST 2011


..and if I'm reading postwalk-replace's documentation correctly, I can 
create a map of things to look for and things to replace and it will do 
all of them. From the documentation:

Recursively transforms form by replacing keys in smap with their
   values.  Like clojure/replace but works on any data structure.  Does
   replacement at the leaves of the tree first.

Source:
(defn postwalk-replace
   "...docs..."
   {:added "1.1"}
   [smap form]
   (postwalk (fn [x] (if (contains? smap x) (smap x) x)) form))

So actually postwalk-replace will do all of my work for me. Yes?

     -Josiah



On 11/18/11 12:29 PM, Lee Spector wrote:
> Josiah and I discussed this in person, as a way to do what he's doing more simply, and I won't repeat all of our discussion here, but others might find it useful for similar or different purposes.
>
>   -Lee
>
> (use 'clojure.walk)
>
> (defn subst
>    "Returns the given list but with all instances of that (at any depth)
>     replaced with this. Read as 'subst this for that in list'. "
>    [this that lst]
>    (postwalk-replace {that this} lst))
>
> (subst 0 1 '(0 1 2 3 (0 1 2 3 (0 1 2 3) 0 1 2 3) 0 1 2 3))
>
> ; (0 0 2 3 (0 0 2 3 (0 0 2 3) 0 0 2 3) 0 0 2 3)
>
>
> --
> Lee Spector, Professor of Computer Science
> Cognitive Science, Hampshire College
> 893 West Street, Amherst, MA 01002-3359
> lspector at hampshire.edu, http://hampshire.edu/lspector/
> Phone: 413-559-5352, Fax: 413-559-5438
>
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11

-- 
Wm. Josiah Erikson
Network Engineer
Hampshire College
Amherst, MA 01002
(413) 559-6091



More information about the Cs254f11 mailing list