#clojure log - Apr 17 2011

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

0:05 christophercodri: fairly new to Clojure and a little stuck...

0:06 (defn process-file [file-name]

0:06 (with-open [rdr (BufferedReader. (FileReader. file-name))]

0:06 (doseq [line (line-seq rdr)]

0:06 (println

0:06 (let [c line] (conj @calldb (grab-first c))))))))

0:06 oops..

0:08 trying to push things onto a vector from a file.

0:33 ataggart: christophercodri: what's the question?

0:34 christophercodri: I am trying to update an empty vector with a bunch of values that I am pushing from a file

0:35 I don't think I am retaining the change with doseq

0:35 tomoj: &(let [a (atom [])] [(conj @a 3) (conj @a 4) @a])

0:35 sexpbot: ⟹ [[3] [4] []]

0:36 ataggart: (vec (line-seq (reader file-name)))

0:36 ^ that will do it

0:36 reader is in clojure.java.io

0:37 and you need to put the reader in a with-out, but that' the general idea

0:37 erm with-open

0:37 I need more or less beer, can't tell which

0:37 christophercodri: haha

0:38 ataggart: and right now, if you think you need a reference type (e.g., atom) you're probably wrong

0:39 christophercodri: probably

0:41 ataggart: and just to prove to myself I'm right...

0:41 ,(first (with-open [rdr (reader "http://www.google.com")] (vec (line-seq rdr))))

0:41 clojurebot: java.lang.Exception: Unable to resolve symbol: reader in this context

0:41 ataggart: bah

0:42 ,(use 'clojure.java.io)

0:42 clojurebot: nil

0:42 ataggart: ,(first (with-open [rdr (reader "http://www.google.com/")] (vec (line-seq rdr))))

0:42 clojurebot: java.security.AccessControlException: access denied (java.net.SocketPermission www.google.com:80 connect,resolve)

0:42 ataggart: well, if not for the sandbox, that'd work

0:43 christophercodri: he

0:44 good stuff, I got what I wanted... now I just need to clean up some of the mess

0:44 my vector is full of stuff I don't need at the moment

0:44 other* stuff

3:28 mreynolds: I'm running into a problem with executors where my call can be both a Callable and a Runnable. I tried type hinting like so (.submit executor ^Runnable #(print-protocol "s" (server-sequence server-stream))) but that doesn't seem to resolve the issue

3:28 Is type hinting the right thing to do here?

3:33 amalloy: mreynolds: link to the java method you're calling?

3:33 mreynolds: http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html .submit

3:33 Type hints are not the answer, but I'm not sure what is

3:34 amalloy: if type-hinting is not doing it, try (cast Runnable #(...))

3:34 mreynolds: I'll do that

3:35 amalloy: &(doc cast)

3:35 sexpbot: ⟹ "([c x]); Throws a ClassCastException if x is not a c, else returns x."

3:36 amalloy: not sure that's so helpful, but...

3:37 mreynolds: well, that got rid of the error.... lemme see if it works :)

3:37 Nice, that worked, thanks!

3:39 * amalloy doesn't really understand why this works, but has seen it successfully done before

3:41 mreynolds: I think I get that the internal impl, somehow, can figure out that it's one or the other, but flails if it's both. Raek's blog has a post, but it doesn't end up using an ambiguous method, so it "Just Works". I guess I don't really understand it either, but I'm ok with that :)

3:46 amalloy: $source cast

3:46 sexpbot: cast is http://is.gd/xGG74t

3:46 mreynolds: Interesting that it just punts to the java option

3:46 amalloy: mreynolds: there isn't really another way

3:47 mreynolds: Clojure's type inference is still semi-magic to me :)

3:50 amalloy: it doesn't really have very much type inference, as far as i'm aware. i mean, yes, if you hint something then it sorta bubbles out to the results of method calls on that object

3:51 but in most clojure code you don't bother hinting, and all interop gets done with reflection

3:52 mreynolds: amalloy: Yeah, I don't understand how that works just yet. I do in Java, but that's very straightforward in the bytecode, so that's different.

3:53 amalloy: if you're interested you can have a look at clojure.lang.Reflector

3:53 which is where most of the magic happens

3:53 mreynolds: amalloy: Will do. I plan to do a "tour" after I get this project off the ground.

3:55 Honestly, I'm just really happy at how fast I'm getting my projects done. It takes some change of mindset, but I have been able to write things quickly that were hard in Java. I'm planning to invest more heavily in Clojure as time permits.

3:56 markoman: this is not proper keyword :0 ?

3:56 or :1

3:56 mreynolds: Should be?

3:56 I use them that way

3:57 markoman: hmh, intelliJ marks as error on REPL

3:57 amalloy: &:1

3:57 sexpbot: ⟹ :1

3:58 amalloy: markoman: throws an actual clojure exception, or just highlights it as an error?

3:58 (presuming the latter, this is just an indication that intellij is not the best clojure ide)

3:58 markoman: highlights, and wont let me eval it

3:59 amalloy: haha ouch

3:59 time to roll out :zero :P

3:59 :smallest-nonnegative-number

3:59 markoman: i like intelliJ the most so far, but yes it seems a bug there

4:00 mreynolds: yeah, clojure support is still somewhat weak outside of emacs

4:00 amalloy: mreynolds: ccw is coming along nicely from what i hear

4:00 mreynolds: eclipse's clojure support is actually pretty good, but still has weird parsing bugs with keywords and such

4:00 amalloy: Yeah, I'm liking it so far short of a few gripes. Thankfully I can switch to default mode and fix them then swap back quickly.

4:02 markoman: {:zero 0} evals well, all the other but that starts with :{number}

4:03 same problem is on code editor

4:03 but it looks like it works runtime

4:03 as it should I guess :)

4:04 amalloy: &(keyword "i can make a crazy keyword")

4:04 sexpbot: ⟹ :i can make a crazy keyword

4:05 amalloy: &(keyword "maybe even one with : in it")

4:05 sexpbot: ⟹ :maybe even one with : in it

4:05 amalloy: &(keyword "but probably / isn't allowed?")

4:05 sexpbot: ⟹ :but probably / isn't allowed?

4:07 mreynolds: I have to imagine the text is just treated as a string, right? So you could put anything in there?

4:07 Be hard to type in a repl, of course

4:11 amalloy: mreynolds: i thought / might be forbidden because it's the namespace separator

4:41 raek: mreynolds: in my blog, the function argument was type hinted as a Callable

4:42 mreynolds: raek: Ahhhh, right, in the parameters

4:42 raek: Sorry, missed that

4:43 raek: hrm, I wonder why ^Runnable #(...) shouldn't work...

4:43 mreynolds: from the docs, it looked like type hints only work on parameters

4:43 At least, that's all I've seen from use

4:44 raek: mreynolds: but do you get some kind of error, or just a reflection warning?

4:45 opqdonut_: can you type hint expressions too? I thought it was only symbols

4:45 mreynolds: raek: It just said "multiple types...."

4:45 raek: Don't have the error handy, sorry

4:45 I can dig it up if you're curious

4:45 opqdonut_: also, ISTR the various interfaces that clojure fns implement overlapping with eachother

4:46 something like (FutureTask. function) not knowing whether to treat function as Runnable or Callable

4:46 raek: java.lang.IllegalArgumentException: More than one matching method found: submit

4:46 mreynolds: opqdonut_: I was using submit, which has a similar issue

4:46 There you go

4:46 opqdonut_: so you'll have to reify the relevant instance yourself

4:46 raek: ok, I get this when I typehint the executor service, but not the function

4:48 and when I hint both, I get the same error

4:49 letting the function and typehinting the symbol worked...

4:49 (let [^Runnable f #(inc 1)] (.submit ^ExecutorService pool f))

4:49 (let [f #(inc 1)] (.submit ^ExecutorService pool ^Runnable f))

4:50 ,(meta '^{:foo "bar"}#(inc 1))

4:50 clojurebot: {:foo "bar"}

4:50 amalloy: raek: perhaps the compiler isn't interested on the tag of a literal, because it already knows what its type is?

4:51 raek: ,'#(inc 1)

4:51 clojurebot: (fn* [] (inc 1))

4:51 raek: hrm, maybe so

4:51 amalloy: whereas tagging a symbol has meaning

7:34 markoman: how do I do this: (assoc {:x 1 :y 1} {:x 2 :z 1}) -> {:x 2 :y 1 :z 1} is it possible to kind of explode {:x 2 :z 1} map?

7:42 mids: ,(merge {:x 1 :y 1} {:x 2 :z 1})

7:43 clojurebot: {:z 1, :x 2, :y 1}

7:51 markoman: thanks

8:24 Vinzent: Is there midje users here? What's the right way to say {:a {:b #(...)}} => {:a {:b fn?}} ?

9:34 fliebel: When I have a type implementing one method of a protocol, can I use extend to implement the other, or will that conflict?

10:20 jaley: hi guys! can anyone point me toward some instructions for compile java code with cake, if they exist? I've been looking around the github project but not found anything.

10:23 fliebel: jaley: I think you just have to put them in the right dir.

10:23 src/jvm that is

10:24 jaley: fliebel: ah really? I have to have my source in src/jvm? interesting... I'm migrating a lein project to cake, I previously had a :source-java in my project.clj

10:25 Vinzent: Still no midje users here? Why I got "Midje caught an exception when translating this form" when writing (provided *var* => a-value) ?

10:25 fliebel: jaley: You might be able to configure it like that, but src/jvm works for me.

10:25 jaley: fliebel: hmmm.. looking at the code you're right. it's just passing in "src" and "jvm" as string constants.

10:26 fliebel: Why didn't anyone warn me :( IllegalArgumentException class examples.pong.bat already directly implements interface begame.object.visible for protocol:#'begame.object/visible

10:27 I'm trying to implement on half of the protocol first, and then extend the other half later.

10:30 jaley: fliebel: awesome. that got it. thanks

10:32 I have a code-gen custom task. is there an easy way to make cake run it before the default compile task? Only option I see is to undeftask compile, then redefine it with the dependency and call the original task?

10:36 fliebel: jaley: Sounds okay to me.

10:44 Could anyone who knows about these things explain me how databases store their indexes? For example, CouchDB uses a B+ tree, and can produce views sorted by any key. Does this mean it stores a tree for every view, or is there something smart to sort/index data by multiple keys? *has the weird feeling TimMc or cemerick might know*

10:54 TimMc: fliebel: Unfortunately, I have forgotten pretty much everything from my databases class.

10:55 Try asking me last semester. :-)

10:59 Raynes: fliebel: IIRC, cemerick's brain is just a CouchDB database.

11:01 TimMc: That doesn't mean he knows how it works. :-)

11:26 fliebel: Raynes, TimMc: I'd love to know how *my* brain works ;)

11:39 TimMc, Raynes: Asking in #CouchDB reveals that every view has its own B-tree indeed. I got to have a B-graph or something...

12:44 ihodes: heyo--anyone have a suggestion as to where i should start if i want to do secure login and sessions etc?

12:48 anonymouse89: is there a real clean way to do a partial sum over a list of values?

12:48 like (1 2 3 4) -> (1 3 6 10)

12:52 I vaguely remember a core fn like reduce, but leaving the intermediate values

12:53 jarpiain: ,(reductions + [1 2 3 4])

12:53 clojurebot: (1 3 6 10)

12:53 ihodes: reductions

12:53 there you go ;)

12:53 anonymouse89: ihodes: that's it! thanks

12:54 ihodes: anonymous89: no problem, though i think jarpiain and i basically raced the latency on that one

12:55 anonymouse89: jarpiain: thanks too!

12:55 ihodes: also, i suppose it's anonymouse89--that's a tricky nick.

12:56 anonymouse89: ihodes: do you not have a tab-comlete for usernames?

12:59 ihodes: anonymouse89: wow--no i've always just typed them. now i use tab-complete. sigh. i'm using irssi in tmux, and i didn't know about tab-complete

13:01 anonymouse89: another quick question, doseq is never lazy, correct?

13:02 ihodes: correct; the do's are the antilazy.

14:04 mec: is there a builtin for [(filter pred coll) (remove pred coll)] ?

14:06 raek: mec: ((juxt filter remove) pred coll)

14:06 or clojure.contrib.seq/separate

14:07 mec: raek: thanks

14:18 devn: mmm, juxt

14:20 anyone know if there's a way to extract an archive of google groups posts?

14:20 like a collection of html files, or something?

14:21 fliebel: devn: downthemall can crawl it for you.

14:34 __name__: Can I match the value in defmethod with a function?

14:39 raek: __name__: to use something like (defmethod foo string? ...) instead of (defmethod foo String ...)? if so, then no.

14:40 __name__: raek: yeah, that's what i meant.

14:40 too bad.

14:40 raek: don't forget that the value is matched with isa? and not =

14:42 so you can still group values together using 'derive'

14:43 devn: fliebel: got a link?

14:43 raek: (defn has-foo-nature? [x] (isa? x ::foo)) (derive String :foo) (defmethod bar String ...)

14:44 s/ :foo/ ::foo/

14:44 sexpbot: <raek> (defn has-foo-nature? [x] (isa? x ::foo)) (derive String ::foo) (defmethod bar String ...)

14:44 fliebel: $google downthemall

14:44 sexpbot: First out of 168 results is: DownThemAll!

14:44 http://www.downthemall.net/

14:45 raek: __name__: ^ that's a workaround which in some way could be considered equivalent to defining a method for the predicate has-foo-nature?

14:46 (of course, this might not always be possible to do)

14:46 __name__: okay

14:46 where's the reason for not allowing a user-defined match fun?

14:47 raek: sometihng user-supplied instead of isa?

14:48 Quiark: hi, what's the equivalent of User.class in Clojure (User.class in Java returns an instance of Class describing the class User)

14:48 raek: Quiark: just User

14:49 ,(class java.util.ArrayList)

14:49 clojurebot: java.lang.Class

14:49 __name__: raek: yeah

14:49 Quiark: then we've got bigger problems

14:50 java.lang.IllegalArgumentException: No matching method found: fetchConnection for class com.restfb.DefaultFacebookClient

14:51 ok, maybe because this method has a variable number of arguments

14:51 raek: __name__: that sounds like an even more general approach to dispatch. what method should be chosen when multiple predicates return true?

14:51 __name__: raek: isa? can return true for multiple predicates too.

14:51 hence the prioritization mechanism

14:52 raek: Quiark: varargs are a bit different in Java. (int a, int... b) actually looks like (int a, int[] b) on the JVM level (after the java compiler is done)

14:53 ,(String/format "%d %d %d" (into-array [1 2 3]))

14:53 clojurebot: "1 2 3"

14:54 raek: __name__: fair enough. but I think you have to ask rhickey himself for the rationale... :)

14:54 __name__: Does your Bot have a message thing?

14:55 raek: note that it is entirely possible to roll your own multimethod alternative...

14:56 Quiark: raek, aha, so I must pass an empty array even when I don't use any of the variable arguments

14:57 raek: Quiark: yes, unless the method happens to have an overloaded version without the varargs

14:58 ,(String/format "foo" (make-array Object 0))

14:58 clojurebot: "foo"

14:59 Quiark: kthxbai

15:00 TimMc: $findfn (make-array Object 0)

15:00 sexpbot: []

15:00 TimMc: $findfn (make-array Integer 0)

15:00 sexpbot: []

15:00 TimMc: ,(int-array)

15:00 clojurebot: java.lang.IllegalArgumentException: Wrong number of args (0) passed to: core$int-array

15:01 TimMc: Ah, it wants a coll.

15:25 amalloy: __name__: dnolen has been working on some deep magic to allow predicate dispatch. dunno what state it's in

15:25 * __name__ nods, thanks

18:43 * raek discovers http://existentialtype.wordpress.com/2011/04/09/persistence-of-memory/

20:07 seancorfield: https://github.com/clojure/java.jdbc yay!

20:56 TimMc: seancorfield: Confused. Will the namespace actually be java.jdbc?

20:57 seancorfield: clojure.java.jdbc

20:57 like the new clojure.tools.logging etc

23:42 brehaut: anyone know if its possible to have both positional and named varargs in a function?

23:43 amalloy: brehaut: er what?

23:44 brehaut: so (fn [& r] …) captures positional varargs, (fn [& {:keys [a b c] :or {a 1 b 2 c 3}}] …) captures named args

23:45 im failing at destructuring both

23:45 amalloy: oh

23:46 brehaut: i have a function does RPC (thus doesnt know how many positional arguments it might get) but i want to add a flag to it (rather than doing a dynamic scope binding if possible)

23:46 amalloy: brehaut: there isn't a way to do this with just destructuring

23:47 brehaut: thats what i was afraid of

23:47 mec: (fn [& [a b c :as {:keys [d e] :or {d 1 e 3}}]) would that work?

23:47 amalloy: but you could do something like scan the arglist for keywords, since presumably keywords are illegal for rpc calls?

23:47 brehaut: amalloy: i think that will probably do for my needs yeah

23:47 amalloy: and yes they are

23:48 mec: those positional args are not variadic

23:48 amalloy: plus, they require the whole arglist to be a well-formed map: specifically, an even number of arguments

23:48 brehaut: bbs

23:48 mec: ah right

23:49 brehaut: amalloy: im beginning to wonder if maybe just a dynamic binding would be better

Logging service provided by n01se.net