#clojure logs

2008-05-22

11:07leafwHi. I'm having trouble iterating lists of elements
11:07leafwif I use a loop and call (.get sequence i), it's ugly.
11:08leafwalso, the loop requires an (if (< ....) this that) where 'this' can't be more than one statemnt (no progn?)
11:08leafwI'd appreciate suggestions on how to loop properly a sequence
11:08asbjxrn(doseq item list ....) ?
11:08rhickeyis there some reason why map/reduce/doseq etc are unsuitable?
11:09leafwnever tried
11:09leafwyes: because I don't know how to use them. Will soon.
11:09ozzilee(doseq x [1 2 3 4 5] (println x))
11:09rhickeyok :)
11:09leafwthanks for the keywords.
11:13leafwand, is there a way to do a multiline comment in a text file?
11:14asbjxrn(comment ...) is the one I use
11:14leafwthanks.
11:15rhickeycomment is pretty much it right now, its main limitation being that the contents must be read-able
11:15leafwread-able meaning, parsable
11:15rhickeykind-of
11:15leafwok
11:15leafwa triple ;;; .... ;;; could imitate python's """ .... """
11:16leafwbut I know that other lisps don't do it.
11:21leafwin a map call, how can one give arguments to the mapped function?
11:21leafwcan't get the syntax right.
11:22asbjxrn(map (lambda (x) (println x)) '(1 2 3)) ?
11:22asbjxrnNot sure I understand the question, map by definition calls the mapped function with an argument.
11:23asbjxrn(eh, lambda should be (fn [x] (println x)) .... )
11:24asbjxrnIs there a function? function? (How to test if a symbol is a function?)
11:24rhickeyleafw: the first thing you must determine is, are you iterating for side-effects or to calculate a result?
11:24leafwsuppose I have a function named 'find-all', that takes one arg, and a list of 'children': (map (list-all [sb]) children) ... fails
11:24leafwI am iterating for side effects.
11:24leafwI accumulate thousands of strings in a StringBuffer.
11:24rhickeythen you should be using something starting with do
11:24leafwdoseq works
11:25leafwI am just trying to see how I could use map as well.
11:25rhickeyaccumulating is not really side effects
11:25leafwkind of. I could also return the string and just append it, recursively.
11:26rhickeyyou could just reduce - see the implementation of str in boot.clj
11:26leafwthanks.
11:27leafwby the way, is there any effort to make error reporting better?
11:27leafwlike: 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:27rhickeythat's kind of general, any specific case?
11:29rhickeywhat was the code?
11:29leafwalso, 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:30leafwthe 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:32rhickeyyeah, the operator position just gets a direct IFn call, so will just give IncompatibleClassChangeError, or ClassCastException
11:33rhickeyto do otherwise would seriously compromise the speed of Clojure
11:33leafwwhich doesn't help much ... until one gets it.
11:34rhickeyI've thought about a debug mode, which would be slower but more verbose - no time for that right now
11:34leafwI'm looking forward to it. Exceptions have been quite puzzling so far.
11:34ozzileeCould you could have the REPL catch exceptions and examine the call stack to provide more helpful errors in certain cases?
11:35ozzileeNot sure if that would be easier to implement than a debug mode.
11:36rhickeyOne of the problems is that ClassCastException says only what you had, not what it was trying to cast to - if it said:
11:37rhickeyjava.lang.ClassCastException: java.lang.Integer is not java.lang.IFn
11:37rhickeythat would be a lot better
11:37leafwindeed.
11:39leafwguys, I still don't see it: can a non-lambda function that takes arguments be mapped to a sequence?
11:39leafwI'd appreciate help
11:39rhickeyuser=> (map inc [1 2 3])
11:39rhickey(2 3 4)
11:39ozzileeleafw: You need to create a lambda so that the fn only takes one argument
11:40ozzilee(map (fn [x] (find-all sb x)) [...])
11:40rhickeyor, if it takes multiple args, pass multiple seqs
11:40leafwI see
11:40leafwthen it's definitely not the proper construct, I'd be misusing it.
11:40leafwthank you.
11:40ozzileeMore concretely: (map (fn [x] (+ x 1)) [1 2 3 4])
11:41rhickey(map (partial find-all sb) ...)
11:41leafwpartial -now that's a new one
11:42ozzileeI'm not sure what you're trying to do, but reduce will most likely do it for you.
11:42ozzileeIf you'd like to read up: http://www.cs.nott.ac.uk/~gmh/fold.pdf
11:42ozzilee(reduce is pretty much fold with a different name)
11:42ozzilee(warning: pdf)
11:43asbjxrnIn case everyone missed it earlier: Is there a way to test if something is a function?
11:43leafwI understand. Thanks ozzilee
11:43rhickeyasbjxrn: (instance? clojure.lang.IFn x)
11:44asbjxrnThanks.
11:45asbjxrnI thought about instance?, but with clojure.fns
11:45ozzileeSo, 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:45leafwthat's an ugly one ... ready for a (defn is-fn [x] ...)
11:47rhickeyozzilee: it's a slippery slope. If you ar egoing to do type testing you need to understand the type system
11:48rhickeyfor instance (fn? :fred) would be true
11:48rhickeyalso (fn? {}) and (fn? [])
11:48ozzilee(shrug) so call it (callable?). That would be accurate.
11:49rhickeyThere are an indefinite number of types, I don't want a corresponding set of ?s
11:50ozzileeI just find it ugly to need to think about Java to do things that don't otherwise call out to Java code.
11:50rhickeyIf you import some clojure.lang it would just be (instance? IFn x)
11:51ozzileeYeah, idk. instance == Java for me.
11:51ozzileenit-picking :-)
11:51rhickeyoh, well, even if there were no Java I'd still prefer (instance? type) to (this-type?, (that-type? etc
11:52leafwon 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:53ozzileeWell, 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:53rhickeyleafw: there's been a lot of discussion on the google group re: hosting Clojure
11:53leafwthanks
11:53rhickeyozzilee: with genclass there is
11:54ozzileeHmm. Ok, I'll have to look into that, never heard of it.
11:54asbjxrnTo 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:55ozzileeYeah, but if I wanted to think about Java in every line of code... I'd write in Java :-)
11:56ozzileeNo biggie, I'll write my own callable?, list?, etc, as I suspect many others will.
12:00leafwrhickey : can't find "Re: hosting Clojure" on google groups ... ?
12:01asbjxrnMaybe this thread is what you're looking for?
12:01asbjxrnhttp://groups.google.com/group/clojure/browse_thread/thread/d98cedb860f16a34/136ca458a73dc643?q=hosting&amp;lnk=ol&amp;
12:02leafwapparently I signed up too late. Thanks.
12:06leafwsentry out.
12:33dudleyfWow.
12:33dudleyfThat was an incredibly enlightening thread.
12:38rhickeyozzilee: 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:46rhickeyhttp://sourceforge.net/community/cca08
12:49drewrUgh, requires login.
12:50dudleyfrhickey: What do you think about nested namespaces? Are they possible? feasible? desirable?
12:51rhickeyI think they are complex and not needed
12:52rhickeynamespace aliases would be useful
13:03dudleyfAre namespaces the mechanism for grouping together related functions?
13:04dudleyfLike modules in Haskell?
13:04dudleyfSo that a given project would probably have multiple namespaces?
13:48leafwhi all. I have another very basic question that keeps scaping me
13:48leafwhow to set a valuble to a clojure-defined variable, for instance from a (let [sum 0] ...)
13:49leafwI 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:49leafwso I (doseq pixel pix ...) to sum, but I can't figure out how to set the value of sum once it's declared!
13:49leafwany help appreciated.
13:50ozzileeleafw: Try with-local-vars and var-set/var-get perhaps.
13:51ozzileeleafw: You can supply reduce with a starting value, as well.
13:51leafwnice!
13:51ozzilee(reduce + 10 [1 2 3 4 5]) ;; gives 25
13:51leafwmeaning, then it will start at starting-value + first-element ?
13:52leafwthanks ozzilee, will try al.
13:54ozzileeYou 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:54ozzilee(reduce (fn [a x] (str a x)) "" [1 2 3 4 5])
13:54ozzileeGives you "12345". A bit contrived, maybe.
13:54leafwxD
13:54leafwindeed
13:54leafwbut makes sense
13:54leafwthank you
13:55ozzilee(reduce (fn [a x] (+ a (count x))) 0 ["alice" "bob" "carol"]) ;; Sum the number of chars.
13:56ozzileeIt'll do pretty much anything, might get hard to read if you do too much though :-)
14:03leafw:)
14:03leafwwill be careful.
14:09ozzileepastie question: "manipulating vars in loaded file" http://paste.lisp.org/display/61148
14:09ozzileeAny help with the above would be much appreciated.
14:10ozzileeI've got to head out for a bit, back in a couple hours.
14:18rhickeymore like packages in Java
14:19dudleyfrhickey: But flat
14:20rhickeyJava packages are also flat, dots in names are a convention for hierarchy
14:21ozzileerhickey: Naming quesiton: would it be more canonical to have http/get, or http/http-get?
14:22dudleyfI'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:23dudleyfozzilee: I hope the answer is http/get
14:25dudleyfotherwise the namespace is fairly irrelevant
14:30ozzileedudleyf: 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:31dudleyfIs there away to rename imports?
14:32rhickeydudleyf: imagine clojure.lang.*, java.lang.* - lang.Thing means which?
14:33dudleyfA compile error?
14:33dudleyfBut point taken
14:35rhickeyIt's a compile error in any case
14:36rhickeyozzilee: http/get
14:36ozzileerhickey: Thanks.
22:06ozzileepmap
22:07ozzileeHey go figure, that doesn't work in this buffer (grumble)
22:10Chouserheh
23:08abrooksHeh. http://www.gadgetell.com/tech/comment/firefox-usage-spreads-its-now-found-even-in-outer-space/
23:10abrooksOoops.
23:11abrooksWrong window.
23:17ChouserI wondered.