Newbie: Creating a MapEntry

131 views
Skip to first unread message

samppi

unread,
Nov 21, 2008, 12:03:25 AM11/21/08
to Clojure
Is it possible to create a MapEntry from scratch? The reason why I'm
asking is because I want to mess with sequences of two-sized vectors,
and it would be really cool if I could use the key and val functions
on them rather than (get % 0) and (get % 1):

(map #(str "Key:" (key %) "Value:" (val %)) [(map-entry :a 3) (map-
entry :b 2) (map-entry :a 1) (map-entry :c 0)])

Just wondering. (And if not, is there any good reason why they should
only be available from sequences of maps? :))

Stephen C. Gilardi

unread,
Nov 21, 2008, 12:07:38 AM11/21/08
to clo...@googlegroups.com

On Nov 21, 2008, at 12:03 AM, samppi wrote:

Is it possible to create a MapEntry from scratch? 

user=> (def a (clojure.lang.MapEntry. 3 4))
#'user/a
user=> (key a)
3
user=> (val a)
4
user=>

You can also make it a little shorter by importing clojure.lang.MapEntry .

--Steve

samppi

unread,
Nov 21, 2008, 12:25:38 AM11/21/08
to Clojure
Thank you very much.

wlr

unread,
Nov 21, 2008, 12:56:43 AM11/21/08
to Clojure
On Nov 21, 12:03 am, samppi <rbysam...@gmail.com> wrote:
> I want to mess with sequences of two-sized vectors,

Maybe destructuring will do:
user> (dorun (map (fn [[k v]] (println "Key " k "Value " v)) [ [:a 3]
[:b 2] [:a 1] [:c 0]]))
=>
Key :a Value 3
Key :b Value 2
Key :a Value 1
Key :c Value 0
nil

Rich Hickey

unread,
Nov 21, 2008, 7:35:18 AM11/21/08
to Clojure
I make no promises about the continued existence of MapEntry. Please
don't use it.

Rich

J. McConnell

unread,
Nov 21, 2008, 10:23:36 AM11/21/08
to clo...@googlegroups.com
On Fri, Nov 21, 2008 at 12:03 AM, samppi <rbys...@gmail.com> wrote:
>
> Is it possible to create a MapEntry from scratch? The reason why I'm
> asking is because I want to mess with sequences of two-sized vectors,
> and it would be really cool if I could use the key and val functions
> on them rather than (get % 0) and (get % 1):
>
> (map #(str "Key:" (key %) "Value:" (val %)) [(map-entry :a 3) (map-
> entry :b 2) (map-entry :a 1) (map-entry :c 0)])

Since Rich discouraged use of the MapEntry class, here are a couple of options.

First, one thing worth remembering is that vectors are functions of
their indexes, so you can do this and get rid of the "get"'s:

(map #(str "Key: " (% 0) "Value: " (% 1)) [[:a 3] [:b 2] [:a 1] [:c 0]])

Second, if it was the use of (key) and (val) that you were most
interested in, you could create your own (map-entry) function:

user=> (defn map-entry [k v] (first {k v}))
#'user/map-entry
user=> (map #(str "Key:" (key %) "Value:" (val %))
[(map-entry :a 3) (map-entry :b 2) (map-entry :a 1) (map-entry :c 0)])
("Key::aValue:3" "Key::bValue:2" "Key::aValue:1" "Key::cValue:0")

HTH,

- J.

samppi

unread,
Nov 21, 2008, 11:40:32 AM11/21/08
to Clojure
Yes, thank you—(key) and (val) were what I was interested in, so I'll
use the latter function you gave. What I'm wondering though is, if
MapEntries aren't guaranteed for the future, what is being planned for
(key) and (val) too. Oh, well. :)

On Nov 21, 8:23 am, "J. McConnell" <jdo...@gmail.com> wrote:

Rich Hickey

unread,
Nov 21, 2008, 12:00:16 PM11/21/08
to Clojure


On Nov 21, 11:40 am, samppi <rbysam...@gmail.com> wrote:
> Yes, thank you—(key) and (val) were what I was interested in, so I'll
> use the latter function you gave. What I'm wondering though is, if
> MapEntries aren't guaranteed for the future, what is being planned for
> (key) and (val) too. Oh, well. :)
>

There's a difference between supporting key and val and allowing you
to create MapEntries directly. key and val will stick around.

Rich

Stephen C. Gilardi

unread,
Nov 21, 2008, 12:11:55 PM11/21/08
to clo...@googlegroups.com

On Nov 21, 2008, at 10:23 AM, J. McConnell wrote:

user=> (defn map-entry [k v] (first {k v}))


Very nice!

--Steve

Reply all
Reply to author
Forward
0 new messages