#clojure log - Jul 11 2009

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

0:00 texodus: ,(into {} (map (fn [[k v]] [k (+ 1 v)]) {:a 1 :b 2 :c 3}))

0:00 clojurebot: {:c 4, :b 3, :a 2}

0:00 texodus: there we go

0:01 useful with a multimethod that can dispatch on key

2:34 grrrt: ,(contains? [:a :b :c] :b)

2:34 clojurebot: false

2:34 grrrt: hm. I was expecting that to be true

2:35 why did that return false?

2:37 ,(some #(= % :b) [:a :b :c])

2:37 clojurebot: true

2:37 grrrt: I'm missing something here.

2:43 hiredman: grrrt: have you read the contains? docstring?

2:43 ,(doc contains?)

2:43 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'."

2:43 hiredman: the keyword there is *key*

2:43 grrrt: and "it will not perform a linear search"

2:44 so contains? only works for maps and sets and similar things?

2:44 hiredman: import to keep in mind that clojure datastructures are also java Collections

2:44 ~jdoc java.util.Collections

2:45 grrrt: I forget the java-side of things sometimes :)

2:45 hiredman: erm

2:45 ~jdoc java.util.Collection

2:45 Collections is useful, but in this case you want Collection

2:46 grrrt: ,(.contains [:a :b :c] :b)

2:46 clojurebot: true

2:46 grrrt: thanks hiredman

2:47 sigh, I should go outside and get some fresh air :)

3:08 hiredman: ~ping

3:08 clojurebot: PONG!

6:40 guille_: in order to use an interface generated dinamically how do you compile it then? (compile '<classname>) doesn't work in this case

8:06 rhickey: Chouser: nice! - http://blog.n01se.net/?p=41

8:19 Raynes: So Clojure is going to be written in Clojure?

8:19 O.O

8:20 rhickey: Raynes: eventually, why not?

8:20 Raynes: rhickey: Awesome. :)

8:21 rhickey: I'm just fighting with the spec-growth of newnew

8:21 I don't want to see all of genclas piled on

8:21 genclass

8:25 chubot: Has anyone encountered a problem with emacs on windows and java input ? (read-line) in the REPL does not recognise the delimiter (well thats my guess). Same issue in clojure-in-a-box.

13:47 eevar: hmm.. as for clojure performance, my (horribly inefficient; setting one pixel at a time) triple buffered active rendering loop is actually just half the speed of my java version (which I was stupid enough to delete)

13:48 might have changed something, or do something slighly different now, tho. can't really compare w/o a java version

14:32 http://pastebin.com/m29a300b5

14:34 any major differences between those, or are the Java and clojure versions ~equivalent? The Java code is 2x faster

14:38 slashus2: eevar: There are some doseq loops that may not be as fast as pure java. 2x isn't bad.

14:39 eevar: not really a concern for me. what i'll eventually be doing inside that doseq loop is gonna be slow

14:39 and i'd rather write it in clojure than java

14:40 but doseq is the culprit then?

14:40 slashus2: eevar: Profile the code.

14:41 eevar: ok, nice experiment i guess

14:44 hiredman: ,(macroexpand-1 '(doseq [a (range 10) b (range 10)])

14:44 clojurebot: EOF while reading

14:44 hiredman: ,(macroexpand-1 '(doseq [a (range 10) b (range 10)]))

14:44 clojurebot: (clojure.core/loop [G__2053 (clojure.core/seq (range 10))] (clojure.core/when G__2053 (clojure.core/let [a (clojure.core/first G__2053)] (clojure.core/loop [G__2054 (clojure.core/seq (range 10))] (clojure.core/when G__2054 (clojure.core/let [b (clojure.core/first G__2054)] (do) (recur (clojure.core/next G__2054))))) (recur (clojure.core/next G__2053)))))

14:45 eevar: hmm... how do i run a clojure app from the command line? :p

14:46 hiredman: that is a lot of code for (loop [a 0

14:46 er

14:47 eevar: clojure.main i guess

14:47 hiredman: (loop [a 0] (when (< a 10) (loop [b 0] (when (< b 10) (recur (inc b)))) (recur (inc a))))

14:48 doseq is not particularly fast

14:48 just convient for loop over a seq for side effects

14:48 but you are not even really looping over a seq

14:49 you are, but your seqs are just ranges constructed for the purpose

14:49 eevar: yup

14:49 hiredman: so I imagine if you replace the whole thing with nested loops and no ranges, there will be some speed up

14:57 eevar: harder to read, tho. not sure it's worth it

14:57 for me, anyway

14:58 aww, wth, i'll try

15:02 Chousuke: or you could try using dotimes

15:02 (doc dotimes)

15:02 clojurebot: "([bindings & body]); bindings => name n Repeatedly executes body (presumably for side-effects) with name bound to integers from 0 through n-1."

15:04 eevar: yay, dotimes version is just as fast as the Java one

15:05 and as readable

15:08 seems to cap out at ~240 fps, but that's not far from the Java version

15:54 texodus: So, I'm trying to call load on a java Keystore

15:54 (doto (. Keystore (getInstance "JKS")) (.load (FileInputStream. path) (.toCharArray key-pass)))

15:55 throws a classcastexception : java.lang.Character[] cannot be cast to char[]

15:57 no java autoboxing for arrays

15:57 anyway to force a char[]?

15:59 ataggart: ,(doc make-array)

15:59 clojurebot: "([type len] [type dim & more-dims]); Creates and returns an array of instances of the specified class of the specified dimension(s). Note that a class object is required. Class objects can be obtained by using their imported or fully-qualified name. Class objects for the primitive types can be obtained using, e.g., Integer/TYPE."

16:00 texodus: tried that, still makes a java.lang.character array

16:00 ataggart: what value are you using for the type?

16:02 though (.toCharArray "foo") should be sufficient

16:02 texodus: tried a couple of things

16:02 ataggart: assuming key-pass is a string

16:02 texodus: no, that gets instantly converted back to a string for some reason

16:03 ataggart: ,(.toCharArray "foo")

16:03 clojurebot: #<char[] [C@187d27e>

16:03 texodus: when I try toCharArray, I get (string cannot be cast to char[]"

16:04 yeah, not sure why that happens - #^chars for char array type hint?

16:04 ataggart: .toCharArray will return you an array of chars

16:04 as the output above shows

16:04 texodus: yes, that's true - but clojure seems to convert it back to a string before it passes it to keystore

16:04 because using this

16:05 er, the example above

16:05 throws a classcastexception

16:05 ,(make-array (type (char \a)) 0)

16:05 clojurebot: #<Character[] [Ljava.lang.Character;@14c7cd>

16:05 texodus: doesnt work either

16:06 ataggart: of course not

16:06 rtead the last sentence of the docs for make-array

16:06 but, again, you shouldn't need to make a char array since .toCharArray does it for you

16:06 somehting else is the problem

16:06 texodus: ,(doc make-array)

16:06 clojurebot: "([type len] [type dim & more-dims]); Creates and returns an array of instances of the specified class of the specified dimension(s). Note that a class object is required. Class objects can be obtained by using their imported or fully-qualified name. Class objects for the primitive types can be obtained using, e.g., Integer/TYPE."

16:07 texodus: yes, I realize that

16:07 again, it doesnt actualyl work

16:07 when I use .toCharArray, it claims I am passing KeyStore.load a string

16:07 ataggart: ,(.isArray (class (.toCharArray "foo")))

16:07 clojurebot: true

16:07 texodus: even though .toCharArray clearly creates an char[]

16:08 ataggart: weird

16:08 texodus: yes, I know - but now try passing that array to a java method that takes char[]

16:08 hiredman: can you paste the exception and the code somewhere?

16:09 texodus: sure

16:09 ataggart: I'm looking for one

16:09 hiredman: lisppaste8: url?

16:09 lisppaste8: To use the lisppaste bot, visit http://paste.lisp.org/new/clojure and enter your paste.

16:09 ataggart: ,(Arrays/sort (.toCharArray "foo"))

16:09 clojurebot: java.lang.Exception: No such namespace: Arrays

16:09 ataggart: ,(java.util.Arrays/sort (.toCharArray "foo"))

16:09 clojurebot: nil

16:10 hiredman: wait, what am I doing here? I was doing laundry

16:10 ataggart: lol

16:10 hiredman: ataggart: Arrays/sort mutates the array and returns nil

16:10 ataggart: ya

16:10 getting to used to cljure ;)

16:11 ,(java.util.Arrays/copyOf (.toCharArray "foo") 1)

16:11 clojurebot: #<char[] [C@1473a2e>

16:11 ataggart: ya, clojure isn't secretly turnign char arrays to strings

16:11 something else is going on in your code

16:12 texodus: yeah, this is odd

16:13 ataggart: hiredman: since you're here and all, do you know of anything available for doing logging from inside clojure code?

16:13 texodus: when I paste the same code in the repl, it works

16:13 ataggart: texodus: I've found when things just don't make any sense, it's usually some stupidly trivial bug, like a copy/paste error or typo.

16:14 hiredman: ataggart: not really, I've seen people wrapping log4j or whatever, but I don't really know anything about that

16:14 ataggart: k, guess I'll roll one up and push it into contrib

16:19 lisppaste8: texodus pasted "chararray casting weirdness" at http://paste.lisp.org/display/83422

16:19 texodus: (doto (. java.security.KeyStore (getInstance "JKS")) (.load (java.io.FileInputStream. "path/to/keystore") (.toCharArray "password"))) works in a repl for me for some reason ....

16:19 but the pasted code throws a classcastexception

16:21 wow, I'm retarded

16:21 nevermind, thanks for the help

16:22 just realized there is another method immediately after that one that also takes a char[] and was getting a string - error pointed to the doto line so I didnt realize it was complaining about a different method invocation

16:22 Chousuke: heh.

16:22 cert-pass?

16:22 texodus: indeed

16:23 that'll teach me to use doto

16:24 Chousuke: is KeyStore.getInstance a static method?

16:24 if so, you should use the form KeyStore/getInstance :)

16:25 texodus: can do - just good form, or is there a particular reason?

16:25 Chousuke: good style

16:25 at least, in my opinion.

16:25 I prefer the sugared forms over using . directly.

16:28 saves a pair of parentheses too.

16:29 hiredman: they are a valuable resource

16:29 Chousuke: heh

16:30 I prefer parens used as efficiently as possible

17:21 ceninan: what should I use to flatten a list in a macro?

17:21 like this: (() ()) -> () ()

17:23 * ceninan thinks flatten wasn't the right word to use, as that would imply ((1 2) (3 4)) -> (1 2 3 4)

17:27 Chousuke: ceninan: (let [[a b] [[1 2] [3 4]]] ..) ?

17:28 ceninan: Chousuke: yes, thanks!

17:28 that's what's called destructuring bind?

17:32 nevermind, looked it up like I should

17:32 * ceninan should have remembered destructuring bind before asking

19:00 ataggart_: how can I force a stack trace or message from the repl? All I'm getting is: => java.lang.ClassCastException (repl-1:38)

19:07 cmvkk: you can use (.printStackTrace *e)

19:14 ataggart: thx

19:32 jhawk28: chouser: congrats on #1 on http://news.ycombinator.com/

20:20 Chouser: jhawk28: hm! thanks.

20:50 mrpika: hello everyone

20:51 would any of you happen to know if there is a function in the clojure api to parse numbers

20:52 I've started working my way through PCL and i can't find an analog to the CL parse-integer function

20:52 cmvkk: like, from string to int?

20:52 or from int to string?

20:52 mrpika: string to int

20:52 arbscht_: ,(Integer/parseInt "99")

20:52 mrpika: I considered using Integer.parseInt but it will throw an exception if nothing in the string is parsible

20:52 clojurebot: 99

20:53 mrpika: ,(Integer/parseInt "X")

20:53 clojurebot: java.lang.NumberFormatException: For input string: "X"

20:53 arbscht_: what behaviour would you prefer in that case?

20:54 mrpika: in CL the parse-integer function returns nil in that case

20:54 i could always write a funciton to catch the exception

20:54 cmvkk: that's probably the easiest way.

20:54 mrpika: but it seems like that would be kind of expensive

20:55 i was just wondering if the problem had been solved in a better way

20:55 grrrt: ,(try (Integer/parseInt "X") (catch Exception e nil))

20:55 clojurebot: grrrt: Gabh mo leithscéal?

20:55 grrrt: heh

20:55 ok well something like that

20:56 ,(try (Integer/parseInt "42") (catch Exception e nil))

20:57 clojurebot: grrrt: Titim gan éirí ort.

20:57 grrrt: that actually works on my local repl

20:58 mrpika: ,(clojure-version)

20:58 clojurebot: "1.1.0-alpha-SNAPSHOT"

20:59 mrpika: ,(try (Integer/parseInt "42") (catch NumberFormatException e nil))

20:59 clojurebot: mrpika: Huh?

20:59 cmvkk: heh, maybe it doesn't like try/catch?

21:00 grrrt: hm.

21:01 Chousuke: yeah. try is a forbidden form.

21:01 mrpika: ,(try 3)

21:01 clojurebot: 3

21:02 mrpika: ,(try 3 (catch Exception 4))

21:02 clojurebot: mrpika: Titim gan éirí ort.

21:02 grrrt: that's not a valid form though

21:02 cmvkk: i like how that always appears as garbled chinese characters for me.

21:02 mrpika: ,(try 3 (catch Exception e 4))

21:02 clojurebot: mrpika: It's greek to me.

21:03 mrpika: ,(try 3 (catch e 4))

21:03 clojurebot: mrpika: Gabh mo leithscéal?

21:03 mrpika: doesn't like catch clauses

21:03 Chousuke: hmm

21:04 cmvkk: configure your client to use UTF-8? :)

21:05 cmvkk: yeah, it should be, who knows. i prefer to write off encoding issues as 'complicated' and subsequently ignore them.

21:07 Chousuke: heh

21:07 I haven't had encoding issues with irssi in a long time

21:07 thanks to recode :P

21:12 mrpika: arbscht_, grrrt, cmvkk: thanks for the help

21:25 arbscht_: mrpika: fwiw, http://paste.lisp.org/display/83438

21:28 mrpika: whoa!

21:28 Thanks. I was just gonna be lazy. My solution is like the last 2 lines of that

21:34 arbscht_: I'm not sure it's a great solution. but it seemed like a good case to try hash destructuring, which ought to make hiredman's day :-)

21:35 the substring handling thing seems unnecessary to me. I'd rather let the calling code handle that

21:54 this might be clearer http://paste.lisp.org/display/83438#1

22:01 lisppaste8: ataggart pasted "simple int parse" at http://paste.lisp.org/display/83440

22:01 ataggart: what I've been using

22:02 arbscht_: that's much better :)

22:03 Chouser: ,(read-string "45")

22:03 clojurebot: 45

22:03 ataggart: ,(doc read-string)

22:03 Chouser: that's another option

22:03 clojurebot: "([s]); Reads one object from the string s"

22:04 grrrt: wow

22:04 Chouser: ,(read-string "x")

22:04 ataggart: yep

22:04 I like the default thing though

22:04 clojurebot: x

22:04 mrpika: i didn't know read-string did the conversion

22:04 ataggart: makes reading in optional http params a lot easier

22:04 arbscht_: could be too general for parsing integers

22:04 Chouser: ,(class (read-string "foo"))

22:04 clojurebot: clojure.lang.Symbol

22:05 mrpika: (class (read-string "3"))

22:05 ,(class (read-string "3"))

22:05 clojurebot: java.lang.Integer

22:05 ataggart: ya, not what one would want when sanitzing inputs

22:05 mrpika: that helps if you're just learning clojure

22:05 ataggart: i see your point

22:08 lisppaste8: ataggart pasted "get req param" at http://paste.lisp.org/display/83441

22:09 ataggart: typo, but you get the point

22:10 the version I have around here is just one multimethod that dispatches on (type default)

23:13 grrrt: I am trying to accomplish the following: given a string with a method name (e.g. "toLowerCase"), I would like to call the corresponding method on an object s

23:13 what's the easiest way of doing that?

23:15 ataggart: is this a regulat java object ?

23:15 grrrt: yes

23:17 ataggart: you'l have to jump their the reflection stuff

23:17 in javaland

23:17 grrrt: sigh

23:17 I was afraid you'd say that :)

23:17 ataggart: (.getMethods (class "foo"))

23:17 ,(.getMethods (class "foo"))

23:17 clojurebot: #<Method[] [Ljava.lang.reflect.Method;@1ab9dac>

23:18 grrrt: I know... I had to use that already

23:18 oh well

23:18 ataggart: ya

23:18 grrrt: cheer

23:18 s

23:18 ataggart: feel free to write up a nice clj lib ;)

23:18 grrrt: I have to finish my other nice lib first :)

23:18 ataggart: lol

Logging service provided by n01se.net