#clojure logs

2009-07-11

00:00texodus,(into {} (map (fn [[k v]] [k (+ 1 v)]) {:a 1 :b 2 :c 3}))
00:00clojurebot{:c 4, :b 3, :a 2}
00:00texodusthere we go
00:01texodususeful with a multimethod that can dispatch on key
02:34grrrt,(contains? [:a :b :c] :b)
02:34clojurebotfalse
02:34grrrthm. I was expecting that to be true
02:35grrrtwhy did that return false?
02:37grrrt,(some #(= % :b) [:a :b :c])
02:37clojurebottrue
02:37grrrtI'm missing something here.
02:43hiredmangrrrt: have you read the contains? docstring?
02:43hiredman,(doc contains?)
02:43clojurebot"([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'."
02:43hiredmanthe keyword there is *key*
02:43grrrtand "it will not perform a linear search"
02:44grrrtso contains? only works for maps and sets and similar things?
02:44hiredmanimport to keep in mind that clojure datastructures are also java Collections
02:44hiredman~jdoc java.util.Collections
02:45grrrtI forget the java-side of things sometimes :)
02:45hiredmanerm
02:45hiredman~jdoc java.util.Collection
02:45hiredmanCollections is useful, but in this case you want Collection
02:46grrrt,(.contains [:a :b :c] :b)
02:46clojurebottrue
02:46grrrtthanks hiredman
02:47grrrtsigh, I should go outside and get some fresh air :)
03:08hiredman~ping
03:08clojurebotPONG!
06:40guille_in order to use an interface generated dinamically how do you compile it then? (compile '<classname>) doesn't work in this case
08:06rhickeyChouser: nice! - http://blog.n01se.net/?p=41
08:19RaynesSo Clojure is going to be written in Clojure?
08:19RaynesO.O
08:20rhickeyRaynes: eventually, why not?
08:20Raynesrhickey: Awesome. :)
08:21rhickeyI'm just fighting with the spec-growth of newnew
08:21rhickeyI don't want to see all of genclas piled on
08:21rhickeygenclass
08:25chubotHas 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:47eevarhmm.. 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:48eevarmight have changed something, or do something slighly different now, tho. can't really compare w/o a java version
14:32eevarhttp://pastebin.com/m29a300b5
14:34eevarany major differences between those, or are the Java and clojure versions ~equivalent? The Java code is 2x faster
14:38slashus2eevar: There are some doseq loops that may not be as fast as pure java. 2x isn't bad.
14:39eevarnot really a concern for me. what i'll eventually be doing inside that doseq loop is gonna be slow
14:39eevarand i'd rather write it in clojure than java
14:40eevarbut doseq is the culprit then?
14:40slashus2eevar: Profile the code.
14:41eevarok, nice experiment i guess
14:44hiredman,(macroexpand-1 '(doseq [a (range 10) b (range 10)])
14:44clojurebotEOF while reading
14:44hiredman,(macroexpand-1 '(doseq [a (range 10) b (range 10)]))
14:44clojurebot(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:45eevarhmm... how do i run a clojure app from the command line? :p
14:46hiredmanthat is a lot of code for (loop [a 0
14:46hiredmaner
14:47eevarclojure.main i guess
14:47hiredman(loop [a 0] (when (< a 10) (loop [b 0] (when (< b 10) (recur (inc b)))) (recur (inc a))))
14:48hiredmandoseq is not particularly fast
14:48hiredmanjust convient for loop over a seq for side effects
14:48hiredmanbut you are not even really looping over a seq
14:49hiredmanyou are, but your seqs are just ranges constructed for the purpose
14:49eevaryup
14:49hiredmanso I imagine if you replace the whole thing with nested loops and no ranges, there will be some speed up
14:57eevarharder to read, tho. not sure it's worth it
14:57eevarfor me, anyway
14:58eevaraww, wth, i'll try
15:02Chousukeor you could try using dotimes
15:02Chousuke(doc dotimes)
15:02clojurebot"([bindings & body]); bindings => name n Repeatedly executes body (presumably for side-effects) with name bound to integers from 0 through n-1."
15:04eevaryay, dotimes version is just as fast as the Java one
15:05eevarand as readable
15:08eevarseems to cap out at ~240 fps, but that's not far from the Java version
15:54texodusSo, I'm trying to call load on a java Keystore
15:54texodus(doto (. Keystore (getInstance "JKS")) (.load (FileInputStream. path) (.toCharArray key-pass)))
15:55texodusthrows a classcastexception : java.lang.Character[] cannot be cast to char[]
15:57texodusno java autoboxing for arrays
15:57texodusanyway to force a char[]?
15:59ataggart,(doc make-array)
15:59clojurebot"([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:00texodustried that, still makes a java.lang.character array
16:00ataggartwhat value are you using for the type?
16:02ataggartthough (.toCharArray "foo") should be sufficient
16:02texodustried a couple of things
16:02ataggartassuming key-pass is a string
16:02texodusno, that gets instantly converted back to a string for some reason
16:03ataggart,(.toCharArray "foo")
16:03clojurebot#<char[] [C@187d27e>
16:03texoduswhen I try toCharArray, I get (string cannot be cast to char[]"
16:04texodusyeah, not sure why that happens - #^chars for char array type hint?
16:04ataggart.toCharArray will return you an array of chars
16:04ataggartas the output above shows
16:04texodusyes, that's true - but clojure seems to convert it back to a string before it passes it to keystore
16:04texodusbecause using this
16:05texoduser, the example above
16:05texodusthrows a classcastexception
16:05texodus,(make-array (type (char \a)) 0)
16:05clojurebot#<Character[] [Ljava.lang.Character;@14c7cd>
16:05texodusdoesnt work either
16:06ataggartof course not
16:06ataggartrtead the last sentence of the docs for make-array
16:06ataggartbut, again, you shouldn't need to make a char array since .toCharArray does it for you
16:06ataggartsomehting else is the problem
16:06texodus,(doc make-array)
16:06clojurebot"([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:07texodusyes, I realize that
16:07texodusagain, it doesnt actualyl work
16:07texoduswhen I use .toCharArray, it claims I am passing KeyStore.load a string
16:07ataggart,(.isArray (class (.toCharArray "foo")))
16:07clojurebottrue
16:07texoduseven though .toCharArray clearly creates an char[]
16:08ataggartweird
16:08texodusyes, I know - but now try passing that array to a java method that takes char[]
16:08hiredmancan you paste the exception and the code somewhere?
16:09texodussure
16:09ataggartI'm looking for one
16:09hiredmanlisppaste8: url?
16:09lisppaste8To use the lisppaste bot, visit http://paste.lisp.org/new/clojure and enter your paste.
16:09ataggart,(Arrays/sort (.toCharArray "foo"))
16:09clojurebotjava.lang.Exception: No such namespace: Arrays
16:09ataggart,(java.util.Arrays/sort (.toCharArray "foo"))
16:09clojurebotnil
16:10hiredmanwait, what am I doing here? I was doing laundry
16:10ataggartlol
16:10hiredmanataggart: Arrays/sort mutates the array and returns nil
16:10ataggartya
16:10ataggartgetting to used to cljure ;)
16:11ataggart,(java.util.Arrays/copyOf (.toCharArray "foo") 1)
16:11clojurebot#<char[] [C@1473a2e>
16:11ataggartya, clojure isn't secretly turnign char arrays to strings
16:11ataggartsomething else is going on in your code
16:12texodusyeah, this is odd
16:13ataggarthiredman: since you're here and all, do you know of anything available for doing logging from inside clojure code?
16:13texoduswhen I paste the same code in the repl, it works
16:13ataggarttexodus: 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:14hiredmanataggart: not really, I've seen people wrapping log4j or whatever, but I don't really know anything about that
16:14ataggartk, guess I'll roll one up and push it into contrib
16:19lisppaste8texodus pasted "chararray casting weirdness" at http://paste.lisp.org/display/83422
16:19texodus(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:19texodusbut the pasted code throws a classcastexception
16:21texoduswow, I'm retarded
16:21texodusnevermind, thanks for the help
16:22texodusjust 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:22Chousukeheh.
16:22Chousukecert-pass?
16:22texodusindeed
16:23texodusthat'll teach me to use doto
16:24Chousukeis KeyStore.getInstance a static method?
16:24Chousukeif so, you should use the form KeyStore/getInstance :)
16:25texoduscan do - just good form, or is there a particular reason?
16:25Chousukegood style
16:25Chousukeat least, in my opinion.
16:25ChousukeI prefer the sugared forms over using . directly.
16:28Chousukesaves a pair of parentheses too.
16:29hiredmanthey are a valuable resource
16:29Chousukeheh
16:30ChousukeI prefer parens used as efficiently as possible
17:21ceninanwhat should I use to flatten a list in a macro?
17:21ceninanlike this: (() ()) -> () ()
17:27Chousukeceninan: (let [[a b] [[1 2] [3 4]]] ..) ?
17:28ceninanChousuke: yes, thanks!
17:28ceninanthat's what's called destructuring bind?
17:32ceninannevermind, looked it up like I should
19:00ataggart_how can I force a stack trace or message from the repl? All I'm getting is: => java.lang.ClassCastException (repl-1:38)
19:07cmvkkyou can use (.printStackTrace *e)
19:14ataggartthx
19:32jhawk28chouser: congrats on #1 on http://news.ycombinator.com/
20:20Chouserjhawk28: hm! thanks.
20:50mrpikahello everyone
20:51mrpikawould any of you happen to know if there is a function in the clojure api to parse numbers
20:52mrpikaI've started working my way through PCL and i can't find an analog to the CL parse-integer function
20:52cmvkklike, from string to int?
20:52cmvkkor from int to string?
20:52mrpikastring to int
20:52arbscht_,(Integer/parseInt "99")
20:52mrpikaI considered using Integer.parseInt but it will throw an exception if nothing in the string is parsible
20:52clojurebot99
20:53mrpika,(Integer/parseInt "X")
20:53clojurebotjava.lang.NumberFormatException: For input string: "X"
20:53arbscht_what behaviour would you prefer in that case?
20:54mrpikain CL the parse-integer function returns nil in that case
20:54mrpikai could always write a funciton to catch the exception
20:54cmvkkthat's probably the easiest way.
20:54mrpikabut it seems like that would be kind of expensive
20:55mrpikai was just wondering if the problem had been solved in a better way
20:55grrrt,(try (Integer/parseInt "X") (catch Exception e nil))
20:55clojurebotgrrrt: Gabh mo leithscéal?
20:55grrrtheh
20:55grrrtok well something like that
20:56grrrt,(try (Integer/parseInt "42") (catch Exception e nil))
20:57clojurebotgrrrt: Titim gan éirí ort.
20:57grrrtthat actually works on my local repl
20:58mrpika,(clojure-version)
20:58clojurebot"1.1.0-alpha-SNAPSHOT"
20:59mrpika,(try (Integer/parseInt "42") (catch NumberFormatException e nil))
20:59clojurebotmrpika: Huh?
20:59cmvkkheh, maybe it doesn't like try/catch?
21:00grrrthm.
21:01Chousukeyeah. try is a forbidden form.
21:01mrpika,(try 3)
21:01clojurebot3
21:02mrpika,(try 3 (catch Exception 4))
21:02clojurebotmrpika: Titim gan éirí ort.
21:02grrrtthat's not a valid form though
21:02cmvkki like how that always appears as garbled chinese characters for me.
21:02mrpika,(try 3 (catch Exception e 4))
21:02clojurebotmrpika: It's greek to me.
21:03mrpika,(try 3 (catch e 4))
21:03clojurebotmrpika: Gabh mo leithscéal?
21:03mrpikadoesn't like catch clauses
21:03Chousukehmm
21:04Chousukecmvkk: configure your client to use UTF-8? :)
21:05cmvkkyeah, it should be, who knows. i prefer to write off encoding issues as 'complicated' and subsequently ignore them.
21:07Chousukeheh
21:07ChousukeI haven't had encoding issues with irssi in a long time
21:07Chousukethanks to recode :P
21:12mrpikaarbscht_, grrrt, cmvkk: thanks for the help
21:25arbscht_mrpika: fwiw, http://paste.lisp.org/display/83438
21:28mrpikawhoa!
21:28mrpikaThanks. I was just gonna be lazy. My solution is like the last 2 lines of that
21:34arbscht_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:35arbscht_the substring handling thing seems unnecessary to me. I'd rather let the calling code handle that
21:54arbscht_this might be clearer http://paste.lisp.org/display/83438#1
22:01lisppaste8ataggart pasted "simple int parse" at http://paste.lisp.org/display/83440
22:01ataggartwhat I've been using
22:02arbscht_that's much better :)
22:03Chouser,(read-string "45")
22:03clojurebot45
22:03ataggart,(doc read-string)
22:03Chouserthat's another option
22:03clojurebot"([s]); Reads one object from the string s"
22:04grrrtwow
22:04Chouser,(read-string "x")
22:04ataggartyep
22:04ataggartI like the default thing though
22:04clojurebotx
22:04mrpikai didn't know read-string did the conversion
22:04ataggartmakes reading in optional http params a lot easier
22:04arbscht_could be too general for parsing integers
22:04Chouser,(class (read-string "foo"))
22:04clojurebotclojure.lang.Symbol
22:05mrpika(class (read-string "3"))
22:05mrpika,(class (read-string "3"))
22:05clojurebotjava.lang.Integer
22:05ataggartya, not what one would want when sanitzing inputs
22:05mrpikathat helps if you're just learning clojure
22:05mrpikaataggart: i see your point
22:08lisppaste8ataggart pasted "get req param" at http://paste.lisp.org/display/83441
22:09ataggarttypo, but you get the point
22:10ataggartthe version I have around here is just one multimethod that dispatches on (type default)
23:13grrrtI 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:13grrrtwhat's the easiest way of doing that?
23:15ataggartis this a regulat java object ?
23:15grrrtyes
23:17ataggartyou'l have to jump their the reflection stuff
23:17ataggartin javaland
23:17grrrtsigh
23:17grrrtI was afraid you'd say that :)
23:17ataggart(.getMethods (class "foo"))
23:17ataggart,(.getMethods (class "foo"))
23:17clojurebot#<Method[] [Ljava.lang.reflect.Method;@1ab9dac>
23:18grrrtI know... I had to use that already
23:18grrrtoh well
23:18ataggartya
23:18grrrtcheer
23:18grrrts
23:18ataggartfeel free to write up a nice clj lib ;)
23:18grrrtI have to finish my other nice lib first :)
23:18ataggartlol