On Sep 11, 4:29 pm, James Reeves <[EMAIL PROTECTED]> wrote:
> On Sep 10, 7:40 pm, Rich Hickey <[EMAIL PROTECTED]> wrote:
>
> > What are you doing with Clojure?
>
> Working on Compojure, a library/framework for developing web
> applications.
>
> > What 3 features would you most like to see added next?
>
> 1. Perhaps some standard multimethods could be defined? I often find
> myself writing constructor functions for structs:
>
> (defstruct revision
>   :content
>   :timestamp)
>
> (defn create-revision
>   [content]
>   (struct revision content (new java.util.Date)))
>
> But there isn't a standard naming convention for what to call the
> constructor, so users wouldn't know whether it was new-revision,
> construct-revision, create-revision or whatever. It would be nice if
> there was a multimethod with a standard name that could be overridden.
> Maybe something like:
>
> (defmulti create #(first %&))
>
> (defmethod create revision
>   [type content]
>   (struct type content (new java.util.Date)))
>
> 2. Clojure's inbuilt data structure syntax for vectors, sets and hash-
> maps is very useful, but the disadvantage to this extra syntax is that
> it is currently very difficult to manipulate blocks of Clojure code
> without altering the containing data type. Traditional lisps that only
> use S-exprs do not have this problem.
>
> For instance, I can easily create a small function to transform a tree
> of sequences so that the contents of each sequence are reversed:
>
> (defn postfix
>   [code]
>   (if (seq? code)
>     (map postfix (reverse code))
>     code))
>
> But once you start trying to parse sequences within maps and vectors,
> you run into problems. When you apply "map" to a vector, your vector
> gets turned into a sequence, so your code is no longer valid. It would
> be nice if there were a function that would enable you to iterate over
> the items in a tree of Clojure code, without affecting the data type
> of lists and hash-maps and so forth.
>

This can be done generically using empty, like this:

(defn map-same [f coll]
  (into (empty coll) (map f coll)))

(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 :d 4})
-> {:c 4, :d 5, :a 2, :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 clojure@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to