#clojure log - Feb 20 2010

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

3:21 LauJensen: Morning team

3:41 wooby: hi LauJensen

4:08 Hali_303: why is this false? (contains? [:p] :p)

4:12 never mind, I should read the docs instead :D

6:56 raek: ,(contains? [:p] :p)

6:56 clojurebot: false

6:56 Chousuke: clojurebot: contains?

6:56 clojurebot: contains? is for checking whether a collection has a value for a given key. If you want to find out whether a value exists in a Collection (in linear time!), use the java method .contains

6:57 raek: Hali_303: contains? only works for maps and sets...

6:57 Chousuke: no, it works for vectors too

6:57 (doc contains?)

6:57 clojurebot: "([coll key]); Returns true if key is present in the given collection, otherwise returns false. Note that for numerically indexed collections like vectors and Java arrays, this tests if the numeric key is within the range of indexes. 'contains?' operates constant or logarithmic time; it will not perform a linear search for a value. See also 'some'."

6:57 Chousuke: ,(contains? '[a b c] 2)

6:57 clojurebot: true

6:57 kotarak: raek: no linear search, for vectors it tests whether the index is contained

6:57 raek: ah, yes...

6:57 Hali_303: thanks guys! i'm still learning clojure :)

6:58 raek: of course, the indices are the keys of the vector

6:58 Hali_303: what are you using for web development? does anyone use compojure for some real production apps?

7:07 raek: I'm using compojure, but not for a "production" project... :)

7:08 at most, about 20 persons will use it

7:11 Hali_303: raek: are there some example projects on the web using compojure? I'd be interested how to do authentication and validation

7:27 raek: Hali_303: no, I'm sorry. I've only used compojure for two weeks

7:27 Hali_303: raek: no problem, I'll keep searching

7:28 raek: authentication sounds like something that should be fairly easy to be done in middleware

8:20 avarus: hi

11:49 rrc7cz-hm: how might you go about converting "a__b__c" = 1, "d" = 2 into {:a {:b {:c 1}} :d 2}?

11:49 where some delimiter signals a nesting level

11:54 kotarak: ,(apply merge (for [[k v] (partition 2 ["a_b_c" 1 "d" 2])] (let [ks (.split k "_")] (assoc-in {} ks v))))

11:54 clojurebot: {"d" 2, "a" {"b" {"c" 1}}}

11:54 kotarak: ,(apply merge (for [[k v] (partition 2 ["a_b_c" 1 "d" 2])] (let [ks (map keyword (.split k "_"))] (assoc-in {} ks v))))

11:54 clojurebot: {:d 2, :a {:b {:c 1}}}

11:56 rrc7cz-hm: wow

11:56 kotarak: rrc7cz-hm: has some gotchas though.

11:57 ,(apply merge (for [[k v] (partition 2 ["a_b_c" 1 "d" 2 "a" :boooo])] (let [ks (map keyword (.split k "_"))] (assoc-in {} ks v))))

11:57 clojurebot: {:d 2, :a :boooo}

11:58 rrc7cz-hm: kotarak: unfortunately I'll be iterating over a seq of key-val pairs which could appear in any order. I guess the last one would override all previous then

11:59 kotarak: rrc7cz-hm: yes, you have to merge the different levels.

11:59 rrc7cz-hm: that makes things more complicated.

12:00 rrc7cz-hm: kotarak: thanks for the start though; this is a good base to build on

12:02 kotarak: ,(reduce (fn [m [k v]] (let [ks (map keyword (.split k "_"))] (assoc-in m ks v))) {} (partition 2 ["a_b_c" 1 "d" 2 "a_f" 3]))

12:02 clojurebot: {:d 2, :a {:f 3, :b {:c 1}}}

12:03 kotarak: rrc7cz-hm: maybe this (^^^^) is more what you need?

12:04 rrc7cz-hm: kotarak: that's exactly it

12:04 basically I have nested maps that I convert to flat maps with __ deliminating levels. this is so I can put the map in an html form

12:05 kotarak: rrc7cz-hm: I hope you don't trust this data when you get it back...

12:05 rrc7cz-hm: so there are inputs like <input type="text" name="a_f_s" value="1" />

12:05 kotarak: what do you mean?

12:06 kotarak: rrc7cz-hm: Naa. It's ok. My fear was you want to carry state with that.

12:06 rrc7cz-hm: in a hidden field or so.

12:06 rrc7cz-hm: kotarak: it's for selecting product options, which can get quite complex. Right now I have the nested maps converted to the flat one just fine

12:07 kotarak: I'm trying to now convert it back for comparison

12:07 kotarak: rrc7cz-hm: that's a nice application actually. :)

12:07 rrc7cz-hm: kotarak: I will then walk the various product configurations (nested maps) and try to find the best match from what I got from the UI

12:08 kotarak: yeah, it's the only way I've been able to think of for handling arbitrarily complex product customizations

13:14 hamza: does future objects use a thread pool or each one fires up a new thread?

13:16 {newbie}: future object have to me "executed" by an external source

13:16 like and Executor

13:16 s/and/an

13:18 hamza: kk thanks.

14:02 jlilly: is there a shortcut for (def myvar (inc myvar)) ?

14:03 AWizzArd: No, because this should not be done within programs. Clojure is dynamic - this is only allowed for administration purposes.

14:03 jlilly: trying to preserve state for the length of a loop

14:04 ie: determining an outcome based on a nested if statement.

14:04 AWizzArd: In a loop you can have your own flags/counters

14:04 You can modify them with recur

14:04 Chousuke: defs anywhere but on the top level usually means you have non-idiomatic code

14:04 and that you need to restructure it.

14:04 but how, is difficult to tell until I can see the code in question :)

14:05 kotarak: ,(loop [x 1] (if (= x 5) :done (recur (inc x)))

14:05 clojurebot: EOF while reading

14:05 kotarak: ,(loop [x 1] (if (= x 5) :done (recur (inc x))))

14:05 clojurebot: :done

14:05 jlilly: http://pastie.org/834439

14:07 Chousuke: hmm

14:08 jlilly: so my first inclination is to make it (def robot1-wins (inc robot1-wins))

14:08 but I understand that's a bad idea.

14:09 perhaps I really want to pull out the "who won" if block into a function

14:13 Chousuke: hmm

14:17 mattrepl: are protocols and datatypes expected to change much now?

14:18 Chousuke: jlilly: http://gist.github.com/309849

14:20 jlilly: rather than explicit loops, generating a sequence of the stuff you want to process is often easier :)

14:20 jlilly: in the gist, I generate an infinite sequence of battle results and then take 50 of them

14:21 jlilly: next, it's reduced to a triple containing r1-wins, r2-wins and ties

14:23 jlilly: the (or winner :neither) thing in condp is just there in case winner is nil (which won't be the case here, but I put it there regardless)

14:26 mattrepl: I think they're pretty close to their final form, but since they're not in a release yet, they might change.

14:26 mattrepl: Chousuke: thanks

14:28 maravillas: how might i destructure a list of arguments to pass to a java constructor? that is, something along the lines of (apply new JCheckBox args), if "new" were a function, but...

14:28 Chousuke: maravillas: you can't do that

14:29 maravillas: but you can do (let [[a b c] args] (JCheckBox. a b c)) :)

14:29 maravillas: ah, darn. ii wanted to be able to handle multiple constructor overloads with one statement

14:30 Chousuke: That's unfortunately not possible. :)

14:30 maravillas: oh well. thanks :)

16:16 {newbie}: has anyone here used clojure for numerical algorithms?

16:47 ugglan: gnight

17:09 jlilly: Chousuke: thanks for that gist. Makes things much nicer. :)

17:54 {newbie}: hi! was the 1.1.x brach of clojure contrib already release?

17:54 ed*

18:02 Chousuke: {newbie}: yes. though I'm not sure where to download it :P

18:03 {newbie}: from github?

18:03 AWizzArd: You can download Clojure from http://build.clojure.org/ too.

18:07 {newbie}: AWizzArd: are these versions compiled against each other?

18:07 AWizzArd: Yes.

18:08 {newbie}: nice! thanks

18:16 jcromartie: o hai

18:16 Is there a more idiomatic way to do this: (assoc response :body (json-str (:body response))) ?

18:16 basically to alter a hash

19:01 dermatthias: .

19:04 tomoj: hmm, it seems like amap for dot products on float arrays is slower than map on lazy seqs

19:12 chouser: whoa

19:14 AWizzArd: That would be surprising.

19:22 tomoj: I probably screwed up, then

20:58 hamza: gents, can someone tell me why this does not work, http://paste.lisp.org/display/95334 ?

21:01 somnium: hamza: I think the namespace created by 'create-ns is completely empty (ie lacking clojure.core)

21:03 hamza: any way to import definitions from clojure.core?

21:03 somnium: ,(doc refer)

21:03 clojurebot: "([ns-sym & filters]); refers to all public vars of ns, subject to filters. filters can include at most one each of: :exclude list-of-symbols :only list-of-symbols :rename map-of-fromsymbol-tosymbol For each public interned var in the namespace named by the symbol, adds a mapping from the name of the var to the var to the current namespace. Throws an exception if name is already mapped to something else in the current name

21:04 scgilardi: but you'll have to fully qualify it: (clojure.core/refer 'clojure.core)

21:05 hamza: thanks alot that solved it..

21:20 zab: Hi. I'm trying to upload a very simple Clojure app to App Engine. Once uploaded, I keep getting this stack trace: http://pastebin.com/dabf3cd9

21:20 points to clojure.lang.DynamicClassLoader?

21:20 Seems like GAE's sandbox doesn't like it? java.security.AccessControlException

21:26 somnium: zab: I think compojure.http.multipart disagrees with AE's security policy

21:26 zab: somnium: Yeah I've already commented it out of Compojure sources, and recompiled

21:29 somnium: or you can do (:use [compojure.http :exclude [multipart]]), still getting the error after excluding?

21:32 zab: hmm I deleted src/compojure/http/multipart.clj, commented it out, recompiled and deployed. Same exception raised.

21:32 stack trace also shows it rising from clojure and not compojure though?

21:33 somnium: there might be others, are you doing (:use compojure) in the ns?

21:33 zab: yeah

21:33 (:use compojure.http compojure.html)

21:35 oh, no longer getting that exception. Now getting java.lang.NoClassDefFoundError for my servlet class.

21:41 somnium: zab: dunno, last time I used it with sdk 1.3 this stub was working: http://paste.pocoo.org/show/180670/

22:25 slyphon: is it possible to deref inside a destructuring ?

22:25 oh, hrm, that probably doesn't make sense

22:31 tomoj: you can deref to get the thing you're destructuring..

22:32 slyphon: i'm destructuring a nested hash-map like structure

22:32 tomoj: the leaves are refs?

22:32 slyphon: yeah

22:32 well, one of the branches is a ref

22:32 tomoj: don't think you can do that, then

22:33 slyphon: i may be overdoing it with the refs

22:33 yeah

22:33 it probably is more sensible to do it inside a let in a dosync

22:40 can you mix the :keys form with the symbol/keyword forms? like (let [{:keys [a b c] foo :foo} thing] ...)

22:41 hrm

22:42 oh, duh

22:44 zab: oh man, I tried everything to get a Compojure app running on GAE. This was with the latest 1.3.1 GAE SDK. Nothing worked.

22:44 Then I downgraded the SDK to 1.2.1 (the last one that I could find), and now everything is working.

22:49 Okay I retract everything I said. 1.3.1 is now working. Problem was I was not copying across lib/appengine-tools-api.jar

22:49 Seems it's necessary for it to work in deployment, but not for the dev server.

23:13 Crowbar7: Evening

23:47 sdm: Hi, I'm just starting to learn clojure ... is it possible to do a loop or dotimes inside of a doto?

23:48 (doto (String.) (dotimes [i 10] (.concat i))) => #<CompilerException java.lang.IllegalArgumentException: dotimes requires a vector for its binding (REPL:1)>

23:48 am I doing something crazy?

23:49 somnium: ,(macroexpand-1 '(doto (String.) (dotimes [i 10] ...)))

23:49 clojurebot: java.lang.RuntimeException: java.lang.IllegalArgumentException: dotimes requires a vector for its binding

23:50 somnium: sdm, hmm, not sure why he choked

23:50 hamza: you can't, it will mess dotimes

23:51 (clojure.core/let [G__27 (String.)] (dotimes G__27 [i 10] ...) G__27)

23:52 sdm: Okay... so doto and dotimes (or loop) collide the binds?

23:54 hamza: when in a doto block every function used will be supplied x in its first argument, so not just dotimes it may mess a bunch of things.

23:56 sdm: ahh I see, I thought doto did something only functions starting with . (like (.concat ...) that makes sense

23:56 thank you!

23:56 hamza: np

23:57 Crowbar7: So, I'm quite new to Clojure and was wondering a couple of things. I'm building a really fairly crappy IRC bot that makes a thread whenever a trigger is caught. writing an re-find for everyone one in a cond seems a little lengthy. Would it be possible to just have the keywords checked in a map then using the functions names tied to the keyword to spawn the thread? Or am I doing this whole thing absolutly horrible?

23:58 The reason for threading is proof of concept / fun and because I hate it when http scraping bots hang because they are still collecting data ro something and I can't query them.

Logging service provided by n01se.net