#clojure log - Aug 02 2008

The Joy of Clojure
Main Clojure site
Google Group
IRC
List of all logged dates

8:04 rhickey: anyone want to take a crack at multimethodizing RT.print? I've made (class nil) -> nil

12:52 mathias: evening!

12:53 mada: does anyone know if there is some equivalent of CL's ASSOC in clojure?

12:53 I have a list of two-element lists and want to find one who's first element is X

12:54 right now I am looping through the list, which seems a bit ugly

12:55 kotarak: mada: maybe you can something like: (filter (fn [[x _]] (= x X)) two-element-list)

12:56 mada: yes, I thought abt using filter but that seemed almost as ugly :) thanks for the hint though

12:56 kotarak: mada: why not using a map in the first place?

12:56 mada: I don't understand

12:57 let me show u the data I got

12:57 {:columns (("TITLE_ID" "java.lang.Integer") ("ENTRY_DATE" "java.sql.Timestamp") ("NAME" "java.lang.String"))}

12:57 that is meta data from a db query

12:57 kotarak: oh ok.

12:57 mada: and I want to want to find out...

12:57 kotarak: Never mind. So the form of the data is not for discussion

12:57 mada: hmm... no I don't :)

12:58 correct

12:58 I cannot control it (actualy I am lying but I am too lazy to change that piece of code that returns the meta data :)

12:59 kotarak: what's that strange syntax with the underscore?

13:00 (I haven't read the full docs)

13:00 kotarak: mada: no syntax at all, I use it for things like function arguments which can be ignored. (Relic from my ML experiments)

13:01 mada: ah

13:01 now I grokked it :)

13:01 kotarak: mada: (fn [x _] x) <- a function taking two arguments, but I don't use the second one

13:01 mada: yes

13:01 clever

13:02 kotarak: That is not enforced by Clojure. Just a matter of taste.

13:02 mada: ic

13:04 kotarak: mada: for your original question: you could transform the list into a map: (apply hash-map (apply concat two-element-list))

13:05 then you access: (map-variable "TITLE_ID")

13:05 mada: cool!

13:05 works too :)

13:06 so hash-map works on pairs of element

13:06 s

13:06 kotarak: Then also the assoc, dissoc, get etc. stuff of Clojure works for your data.

13:07 mada: ok. I don't need those in this case though.

13:07 kotarak: hash-map creates the equivalent of associated-lists. But probably faster and easier to use, etc.

13:07 mada: yes, I would guess so.

13:08 I wonder, would it be a good idea to convert each row I get from a db query into a map instead of using a list with no key names?

13:08 the rows I get currently are just plain lists with data

13:08 untagged, so to speak

13:08 I get the column names as meta data on the result set

13:09 making each row into a map would make the rows easy to use when I want to pick out values from them

13:10 right now I have a function that does that for me, looking at the result set metadata to know what each value in the list is

13:11 I will experiment with it and see.

13:11 kotarak: I don't know. I have no clue about DBs. As long as it is fast enough... Who cares? I would probably go for the map, since you get some support in assoc, dissoc etc. and check for something else of this is too slow.

13:13 mada: will look into it and do some experiments. thanks for the ideas!

14:10 haha!

14:11 kotarak: hihi?

14:11 mada: kotarak: I succeeded in doing what I wanted only to discover it was already in clojure as `resultset-seq' :)

14:11 kotarak: mada: nice :)

17:31 ozzilee: Hey all, I've come across what I think might be common code pattern, but I'm not sure what it would be called, I was wondering if someone knows.

17:31 I've got a function, foo, that relies on an object (Java, in this case), bar.

17:32 bar isn't available all of the time. When I call (foo x y z) and bar is available, I'd like to to go ahead and run, but if I call it when bar is not available, I'd like it to just remember what I called it with for when bar becomes available again.

17:33 Does anyone know what that would be called? Perhaps common lisp has a macro for it?

17:46 drewr: What do you mean by availability?

17:47 Whether it's defined or not?

17:50 ozzilee: drewr: Well, the case I'm working on now is a Bayeux object, that's not around until after Jetty is initialized.

17:50 Bayeux is an Ajax/Comet thing.

17:51 I want to be able to subscribe to channels before Jetty is initialized, which means I need to remember what channels I tried to subscribe to and then subscribe after initialization.

17:53 It could work on whether or not a variable is defined, I suppose. I could just define it after Jetty initializes.

Logging service provided by n01se.net