Here's another, perhaps very controversial, solution:

(in m.2.2.1)

;; in
(defn flexible-get
  ([map key]
     (or (let [k (read-string key)]
           (and (number? k)
                (get map k)))
         (get map (keyword key))
         (get map (name key))))
  ([map key & more]
     (apply flexible-get (flexible-get map key) more)))

(defn lookup-symbol? [x]
  (and (symbol? x)
       (.contains (name x) ".")
       (not (.endsWith (name x) "."))
       (not (.startsWith (name x) "."))))

(defmacro in
  "allows dot map key access. ex: (in (let [a {:b 1 \"c\" 2}] (+ a.b
a.c))) => 3 "
  [& forms]
  `(do ~@(postwalk (fn [x]
                     (if (lookup-symbol? x)
                       (let [[map & keys] (.split (str x) "\\.")]
                         `(flexible-get ~(read-string map) ~@keys))
                       x))
                   forms)))


Scott



On Wed, Jun 29, 2011 at 9:41 PM, Scott Jaderholm <jaderh...@gmail.com> wrote:
> (defmacro ->f
>  "like -> but f is threaded at pos 0 instead of 1"
>  ([f] f)
>  ([f x] `(~f ~x))
>  ([f x & more]
>     `(->f (~f ~x) ~@more)))
>
>
> (->f m 2 2 1)
>
> Scott
>
>
>
> On Wed, Jun 29, 2011 at 7:00 PM, Antonio Recio <amdx6...@gmail.com> wrote:
>> Is there other way to express (((m2) 2) 1)?
>> (def m [1 2 [21 22 [221 222 223] 23] 3])
>> (((m 2) 2) 1)
>> ;-> 222
>>
>> --
>> 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
>

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