2009-07-11
| 00:00 | texodus | ,(into {} (map (fn [[k v]] [k (+ 1 v)]) {:a 1 :b 2 :c 3})) |
| 00:00 | clojurebot | {:c 4, :b 3, :a 2} |
| 00:00 | texodus | there we go |
| 00:01 | texodus | useful with a multimethod that can dispatch on key |
| 02:34 | grrrt | ,(contains? [:a :b :c] :b) |
| 02:34 | clojurebot | false |
| 02:34 | grrrt | hm. I was expecting that to be true |
| 02:35 | grrrt | why did that return false? |
| 02:37 | grrrt | ,(some #(= % :b) [:a :b :c]) |
| 02:37 | clojurebot | true |
| 02:37 | grrrt | I'm missing something here. |
| 02:43 | hiredman | grrrt: have you read the contains? docstring? |
| 02:43 | hiredman | ,(doc contains?) |
| 02: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'." |
| 02:43 | hiredman | the keyword there is *key* |
| 02:43 | grrrt | and "it will not perform a linear search" |
| 02:44 | grrrt | so contains? only works for maps and sets and similar things? |
| 02:44 | hiredman | import to keep in mind that clojure datastructures are also java Collections |
| 02:44 | hiredman | ~jdoc java.util.Collections |
| 02:45 | grrrt | I forget the java-side of things sometimes :) |
| 02:45 | hiredman | erm |
| 02:45 | hiredman | ~jdoc java.util.Collection |
| 02:45 | hiredman | Collections is useful, but in this case you want Collection |
| 02:46 | grrrt | ,(.contains [:a :b :c] :b) |
| 02:46 | clojurebot | true |
| 02:46 | grrrt | thanks hiredman |
| 02:47 | grrrt | sigh, I should go outside and get some fresh air :) |
| 03:08 | hiredman | ~ping |
| 03:08 | clojurebot | PONG! |
| 06:40 | guille_ | in order to use an interface generated dinamically how do you compile it then? (compile '<classname>) doesn't work in this case |
| 08:06 | rhickey | Chouser: nice! - http://blog.n01se.net/?p=41 |
| 08:19 | Raynes | So Clojure is going to be written in Clojure? |
| 08:19 | Raynes | O.O |
| 08:20 | rhickey | Raynes: eventually, why not? |
| 08:20 | Raynes | rhickey: Awesome. :) |
| 08:21 | rhickey | I'm just fighting with the spec-growth of newnew |
| 08:21 | rhickey | I don't want to see all of genclas piled on |
| 08:21 | rhickey | genclass |
| 08: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 | eevar | might have changed something, or do something slighly different now, tho. can't really compare w/o a java version |
| 14:32 | eevar | http://pastebin.com/m29a300b5 |
| 14:34 | eevar | 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 | eevar | and i'd rather write it in clojure than java |
| 14:40 | eevar | 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 | hiredman | 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 | hiredman | doseq is not particularly fast |
| 14:48 | hiredman | just convient for loop over a seq for side effects |
| 14:48 | hiredman | but you are not even really looping over a seq |
| 14:49 | hiredman | 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 | eevar | for me, anyway |
| 14:58 | eevar | aww, wth, i'll try |
| 15:02 | Chousuke | or you could try using dotimes |
| 15:02 | Chousuke | (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 | eevar | and as readable |
| 15:08 | eevar | 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 | texodus | (doto (. Keystore (getInstance "JKS")) (.load (FileInputStream. path) (.toCharArray key-pass))) |
| 15:55 | texodus | throws a classcastexception : java.lang.Character[] cannot be cast to char[] |
| 15:57 | texodus | no java autoboxing for arrays |
| 15:57 | texodus | 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 | ataggart | 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 | texodus | 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 | ataggart | 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 | texodus | because using this |
| 16:05 | texodus | er, the example above |
| 16:05 | texodus | throws a classcastexception |
| 16:05 | texodus | ,(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 | ataggart | rtead the last sentence of the docs for make-array |
| 16:06 | ataggart | but, again, you shouldn't need to make a char array since .toCharArray does it for you |
| 16:06 | ataggart | 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 | texodus | again, it doesnt actualyl work |
| 16:07 | texodus | 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 | ataggart | getting to used to cljure ;) |
| 16:11 | ataggart | ,(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 | ataggart | 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 | texodus | but the pasted code throws a classcastexception |
| 16:21 | texodus | wow, I'm retarded |
| 16:21 | texodus | nevermind, thanks for the help |
| 16:22 | texodus | 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 | Chousuke | cert-pass? |
| 16:22 | texodus | indeed |
| 16:23 | texodus | that'll teach me to use doto |
| 16:24 | Chousuke | is KeyStore.getInstance a static method? |
| 16:24 | Chousuke | 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 | Chousuke | at least, in my opinion. |
| 16:25 | Chousuke | I prefer the sugared forms over using . directly. |
| 16:28 | Chousuke | saves a pair of parentheses too. |
| 16:29 | hiredman | they are a valuable resource |
| 16:29 | Chousuke | heh |
| 16:30 | Chousuke | I prefer parens used as efficiently as possible |
| 17:21 | ceninan | what should I use to flatten a list in a macro? |
| 17:21 | ceninan | like this: (() ()) -> () () |
| 17:27 | Chousuke | ceninan: (let [[a b] [[1 2] [3 4]]] ..) ? |
| 17:28 | ceninan | Chousuke: yes, thanks! |
| 17:28 | ceninan | that's what's called destructuring bind? |
| 17:32 | ceninan | nevermind, looked it up like I should |
| 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 | mrpika | would any of you happen to know if there is a function in the clojure api to parse numbers |
| 20:52 | mrpika | 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 | cmvkk | 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 | mrpika | 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 | mrpika | 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 | grrrt | ok well something like that |
| 20:56 | grrrt | ,(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 | Chousuke | 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 | Chousuke | I haven't had encoding issues with irssi in a long time |
| 21:07 | Chousuke | 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 | mrpika | 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 | arbscht_ | the substring handling thing seems unnecessary to me. I'd rather let the calling code handle that |
| 21:54 | arbscht_ | 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 | ataggart | 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 | mrpika | ,(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 | mrpika | 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 | ataggart | 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 | grrrt | 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 | ataggart | in javaland |
| 23:17 | grrrt | sigh |
| 23:17 | grrrt | I was afraid you'd say that :) |
| 23:17 | ataggart | (.getMethods (class "foo")) |
| 23:17 | ataggart | ,(.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 | grrrt | oh well |
| 23:18 | ataggart | ya |
| 23:18 | grrrt | cheer |
| 23:18 | grrrt | 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 |