#clojure log - May 22 2008

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

11:07 leafw: Hi. I'm having trouble iterating lists of elements

11:07 if I use a loop and call (.get sequence i), it's ugly.

11:08 also, the loop requires an (if (< ....) this that) where 'this' can't be more than one statemnt (no progn?)

11:08 I'd appreciate suggestions on how to loop properly a sequence

11:08 asbjxrn: (doseq item list ....) ?

11:08 rhickey: is there some reason why map/reduce/doseq etc are unsuitable?

11:09 leafw: never tried

11:09 yes: because I don't know how to use them. Will soon.

11:09 ozzilee: (doseq x [1 2 3 4 5] (println x))

11:09 rhickey: ok :)

11:09 leafw: thanks for the keywords.

11:13 and, is there a way to do a multiline comment in a text file?

11:14 asbjxrn: (comment ...) is the one I use

11:14 leafw: thanks.

11:15 rhickey: comment is pretty much it right now, its main limitation being that the contents must be read-able

11:15 leafw: read-able meaning, parsable

11:15 rhickey: kind-of

11:15 leafw: ok

11:15 a triple ;;; .... ;;; could imitate python's """ .... """

11:16 but I know that other lisps don't do it.

11:21 in a map call, how can one give arguments to the mapped function?

11:21 can't get the syntax right.

11:22 asbjxrn: (map (lambda (x) (println x)) '(1 2 3)) ?

11:22 Not sure I understand the question, map by definition calls the mapped function with an argument.

11:23 (eh, lambda should be (fn [x] (println x)) .... )

11:24 Is there a function? function? (How to test if a symbol is a function?)

11:24 rhickey: leafw: the first thing you must determine is, are you iterating for side-effects or to calculate a result?

11:24 leafw: suppose I have a function named 'find-all', that takes one arg, and a list of 'children': (map (list-all [sb]) children) ... fails

11:24 I am iterating for side effects.

11:24 I accumulate thousands of strings in a StringBuffer.

11:24 rhickey: then you should be using something starting with do

11:24 leafw: doseq works

11:25 I am just trying to see how I could use map as well.

11:25 rhickey: accumulating is not really side effects

11:25 leafw: kind of. I could also return the string and just append it, recursively.

11:26 rhickey: you could just reduce - see the implementation of str in boot.clj

11:26 leafw: thanks.

11:27 by the way, is there any effort to make error reporting better?

11:27 like: Class java.lang.StringBuffer does not implement the requested interface clojure.lang.IFn ---> means: don't try to evaluate an instance of a java object, you can't.

11:27 rhickey: that's kind of general, any specific case?

11:29 what was the code?

11:29 leafw: also, the jdbc for postgresql uses a system of double exception: there is an SQLException that carries a second error within it, which is clear and specific to the case. For clojure, the second one would be the one I'd like to see all the time, not the first (the java one).

11:30 the code was my silly effort to make map accept a function with an argument: (map (find-all (sb)) children) .... where 'sb' is a StringBuffer instance.

11:32 rhickey: yeah, the operator position just gets a direct IFn call, so will just give IncompatibleClassChangeError, or ClassCastException

11:33 to do otherwise would seriously compromise the speed of Clojure

11:33 leafw: which doesn't help much ... until one gets it.

11:34 rhickey: I've thought about a debug mode, which would be slower but more verbose - no time for that right now

11:34 leafw: I'm looking forward to it. Exceptions have been quite puzzling so far.

11:34 ozzilee: Could you could have the REPL catch exceptions and examine the call stack to provide more helpful errors in certain cases?

11:35 Not sure if that would be easier to implement than a debug mode.

11:36 rhickey: One of the problems is that ClassCastException says only what you had, not what it was trying to cast to - if it said:

11:37 java.lang.ClassCastException: java.lang.Integer is not java.lang.IFn

11:37 that would be a lot better

11:37 leafw: indeed.

11:39 guys, I still don't see it: can a non-lambda function that takes arguments be mapped to a sequence?

11:39 I'd appreciate help

11:39 rhickey: user=> (map inc [1 2 3])

11:39 (2 3 4)

11:39 ozzilee: leafw: You need to create a lambda so that the fn only takes one argument

11:40 (map (fn [x] (find-all sb x)) [...])

11:40 rhickey: or, if it takes multiple args, pass multiple seqs

11:40 leafw: I see

11:40 then it's definitely not the proper construct, I'd be misusing it.

11:40 thank you.

11:40 ozzilee: More concretely: (map (fn [x] (+ x 1)) [1 2 3 4])

11:41 rhickey: (map (partial find-all sb) ...)

11:41 leafw: partial -now that's a new one

11:42 ozzilee: I'm not sure what you're trying to do, but reduce will most likely do it for you.

11:42 If you'd like to read up: http://www.cs.nott.ac.uk/~gmh/fold.pdf

11:42 (reduce is pretty much fold with a different name)

11:42 (warning: pdf)

11:43 asbjxrn: In case everyone missed it earlier: Is there a way to test if something is a function?

11:43 leafw: I understand. Thanks ozzilee

11:43 rhickey: asbjxrn: (instance? clojure.lang.IFn x)

11:44 asbjxrn: Thanks.

11:45 I thought about instance?, but with clojure.fns

11:45 ozzilee: So, on the subject of predicates... doesn't make sense to me to call out to java to test things internal to clojure. Shouldn't there be a (fn?) predicate?

11:45 leafw: that's an ugly one ... ready for a (defn is-fn [x] ...)

11:47 rhickey: ozzilee: it's a slippery slope. If you ar egoing to do type testing you need to understand the type system

11:48 for instance (fn? :fred) would be true

11:48 also (fn? {}) and (fn? [])

11:48 ozzilee: (shrug) so call it (callable?). That would be accurate.

11:49 rhickey: There are an indefinite number of types, I don't want a corresponding set of ?s

11:50 ozzilee: I just find it ugly to need to think about Java to do things that don't otherwise call out to Java code.

11:50 rhickey: If you import some clojure.lang it would just be (instance? IFn x)

11:51 ozzilee: Yeah, idk. instance == Java for me.

11:51 nit-picking :-)

11:51 rhickey: oh, well, even if there were no Java I'd still prefer (instance? type) to (this-type?, (that-type? etc

11:52 leafw: on anothe topic: is it possible to have more than one clojure repl running in the same JVM? The init part looks static to me

11:53 ozzilee: Well, there's only so many types... it's not like Clojure itself gives the user power to create arbitrary new types, unless I'm mistaken.

11:53 rhickey: leafw: there's been a lot of discussion on the google group re: hosting Clojure

11:53 leafw: thanks

11:53 rhickey: ozzilee: with genclass there is

11:54 ozzilee: Hmm. Ok, I'll have to look into that, never heard of it.

11:54 asbjxrn: To me one of the main features of clojure is that it's so closely tied to Java. So embracing java comes with the territory in a way.

11:55 ozzilee: Yeah, but if I wanted to think about Java in every line of code... I'd write in Java :-)

11:56 No biggie, I'll write my own callable?, list?, etc, as I suspect many others will.

12:00 leafw: rhickey : can't find "Re: hosting Clojure" on google groups ... ?

12:01 asbjxrn: Maybe this thread is what you're looking for?

12:01 http://groups.google.com/group/clojure/browse_thread/thread/d98cedb860f16a34/136ca458a73dc643?q=hosting&lnk=ol&

12:02 leafw: apparently I signed up too late. Thanks.

12:06 sentry out.

12:33 dudleyf: Wow.

12:33 That was an incredibly enlightening thread.

12:38 rhickey: ozzilee: encapsulating Java is a non-goal of Clojure. But even heavy use of Java is still 1/3-1/10th the size in Clojure

12:46 http://sourceforge.net/community/cca08

12:49 drewr: Ugh, requires login.

12:50 * drewr never created an sf account.

12:50 dudleyf: rhickey: What do you think about nested namespaces? Are they possible? feasible? desirable?

12:51 rhickey: I think they are complex and not needed

12:52 namespace aliases would be useful

13:03 dudleyf: Are namespaces the mechanism for grouping together related functions?

13:04 Like modules in Haskell?

13:04 So that a given project would probably have multiple namespaces?

13:48 leafw: hi all. I have another very basic question that keeps scaping me

13:48 how to set a valuble to a clojure-defined variable, for instance from a (let [sum 0] ...)

13:49 I can't use reduce in this case because it's an array of byte[]; with reduce, the second element is bit-and 255 ok, but can't do so for the very first element.

13:49 so I (doseq pixel pix ...) to sum, but I can't figure out how to set the value of sum once it's declared!

13:49 any help appreciated.

13:50 ozzilee: leafw: Try with-local-vars and var-set/var-get perhaps.

13:51 leafw: You can supply reduce with a starting value, as well.

13:51 leafw: nice!

13:51 ozzilee: (reduce + 10 [1 2 3 4 5]) ;; gives 25

13:51 leafw: meaning, then it will start at starting-value + first-element ?

13:52 thanks ozzilee, will try al.

13:54 ozzilee: You can do some neat things with reduce. Your starting value doesn't need to be the same type as the rest of the values.

13:54 (reduce (fn [a x] (str a x)) "" [1 2 3 4 5])

13:54 Gives you "12345". A bit contrived, maybe.

13:54 leafw: xD

13:54 indeed

13:54 but makes sense

13:54 thank you

13:55 ozzilee: (reduce (fn [a x] (+ a (count x))) 0 ["alice" "bob" "carol"]) ;; Sum the number of chars.

13:56 It'll do pretty much anything, might get hard to read if you do too much though :-)

14:03 leafw: :)

14:03 will be careful.

14:09 ozzilee: pastie question: "manipulating vars in loaded file" http://paste.lisp.org/display/61148

14:09 Any help with the above would be much appreciated.

14:10 I've got to head out for a bit, back in a couple hours.

14:18 rhickey: more like packages in Java

14:19 dudleyf: rhickey: But flat

14:20 rhickey: Java packages are also flat, dots in names are a convention for hierarchy

14:21 ozzilee: rhickey: Naming quesiton: would it be more canonical to have http/get, or http/http-get?

14:22 dudleyf: I'm not a Java programmer, so forgive me, but if you have some.package.Thing, can't you import some.* and then reference package.Thing?

14:23 ozzilee: I hope the answer is http/get

14:25 otherwise the namespace is fairly irrelevant

14:30 ozzilee: dudleyf: Right, but in that case it would shadow the clojure/get on (refer 'http). Is that something to be avoided? I do not know.

14:31 dudleyf: Is there away to rename imports?

14:32 rhickey: dudleyf: imagine clojure.lang.*, java.lang.* - lang.Thing means which?

14:33 dudleyf: A compile error?

14:33 But point taken

14:35 rhickey: It's a compile error in any case

14:36 ozzilee: http/get

14:36 ozzilee: rhickey: Thanks.

14:44 * ozzilee is walking out the door

22:06 ozzilee: pmap

22:07 Hey go figure, that doesn't work in this buffer (grumble)

22:10 Chouser: heh

23:08 abrooks: Heh. http://www.gadgetell.com/tech/comment/firefox-usage-spreads-its-now-found-even-in-outer-space/

23:10 Ooops.

23:11 Wrong window.

23:11 * abrooks tracks down the #noise window

23:17 Chouser: I wondered.

Logging service provided by n01se.net