#clojure logs

2009-07-12

01:16Ali_I am learning clojure, can I ask a question here?
01:24cmvkkyes.
01:34Ali_I am receiving IllegalArgumentException when I define this function
01:34Ali_(defn pascal-triangle
01:34Ali_ "Returns a lazy sequence of all the rows in pascal triangel."
01:34Ali_ (iterate
01:34Ali_ (fn [row]
01:34Ali_ (concat '(1)
01:34Ali_ (map #(reduce + %) (partition 2 1 row))
01:34Ali_ '(1)))
01:34Ali_ '(1 1)))
01:35Ali_Don't know how to create ISeq from Symbol.
01:35Ali_Where in this definition is it creating ISeq from symbol
01:35cmvkkprobably partition.
01:35Ali_I tried this separately
01:36Ali_(defn p-t-row [r]
01:36Ali_ (concat '(1)
01:36Ali_ (map #(reduce + %) (partition 2 1 r))
01:36Ali_ '(1)))
01:36Ali_this works and every time I call it I get a new row from pascal triangle
01:42cmvkkoh, duh. the simplist things are the easiest to overlook.
01:42cmvkkfor pascal-triangle, you want def not defn.
01:43cmvkkit's expecting a function definition with an arglist, and it's choking because there isn't one...
01:43Ali_I will look into that. But I was looking at the code for fibs in lazy-seqs which is similar and it uses defn
01:44Ali_isn't it?
01:44cmvkkwell you can use this as a function, but it at least needs an empty arglist.
01:44cmvkkjust put [] after the docstring.
01:44Ali_oh, yes yes
01:44Ali_thanks
01:44Ali_I forgot about that, thanks a lot
01:53Ali_still not working
01:53Ali_I am getting wrong number of arguments to iterate now
01:53Ali_after I added the empty arglist or using def
01:53Ali_(defn pascal-triangle
01:53Ali_ "Returns a lazy sequence of all the rows in pascal triangle."
01:53Ali_ []
01:53Ali_ (iterate
01:53Ali_ (fn [row]
01:53Ali_ (concat '(1)
01:53Ali_ (map #(reduce + %) (partition 2 1 row))
01:53Ali_ '(1))
01:54Ali_ [1])))
01:55Ali_sorry, it was misplaced parens
02:05JomyootIs there an example on how to structure project folders/name spaces and etc?
02:05Jomyootis it similar to how I would do packages in Java?
02:05kotarakJomyoot: I structure my projects like this:
02:06kotarakJomyoot: toplevel build.xml, ivy stuff, README, etc.
02:06Jomyootsince' there is no objects in clojure.
02:06Jomyootdo I still need to keep that package/folder relationship?
02:06Jomyootlike in java?
02:06Jomyootcom.what.here
02:06kotarakJomyoot: src subdir contains all sources (I mix .java and .clj if there is any .java...), layout below src is dictated by the namespace dir convention
02:06Jomyootgoes into src/com/wht/here
02:07kotarakJomyoot: compiled stuff goes into classes, jars etc. in build
02:07Jomyootkotarak: I use intelliJ
02:07Jomyootwith La Clojure
02:07JomyootHope it's good enough
02:07kotarakJomyoot: Don't know about IntelliJ. Do everything manually and work with Vim.
02:08kotarakBut the layout proved it's usefulness.
02:08kotarakTo your questions: namespaces follow Java conventions.
02:08JomyootDo I need to use Maven?
02:08kotarakNo. I use eg. Ivy. But even that is optional.
02:08Jomyootfor many of the plugins?
02:08kotarakYou can use make if you like.
02:09kotarak(ns foo.bar-baz.frobincator) must go to (considering the structure above) src/foo/bar_baz/frobnicator.clj
02:10Jomyootok
02:11JomyootI tried Scala
02:11kotarakJomyoot: don't forget the - vs. _ translation.
02:12JomyootDidn't see enough Magick
02:12Jomyootit's like a terse version of Java
02:12Jomyootbut that's it
02:12JomyootWas trying to find magick in it
02:14Jomyootclj file names aren't capitalized?
02:15kotarakIt depends.
02:15kotarak(ns foo.bar.Baz) must be capitalised.
02:16kotarak(ns foo.bar.baz) not.
02:18Jomyoothmm
02:37Jomyootdoes clojure get compiled to .class?
02:37Jomyootclj --> .class?
02:37JomyootShould I expect it to?
02:37cmvkkif you use genclass. otherwise, when clojure is compiled, every function becomes its own class.
02:38Jomyootso when i hit compile, i should be getting bunches of .class files right?
02:38Jomyootso my IDE is not set up right
02:38Jomyootnothing comes out
02:39cmvkkyeah if nothing comes out that's probably wrong.
03:46Ali_How does this form run inside the jvm?
03:46Ali_(reduce + (take 1000000000 whole-numbers)
03:46Ali_Does it take the first number add it, throw it, take the next, take the next and add it, ....
03:46Ali_Or does it realize the first billion numbers and then add all the numbers in the seq?
03:47kotarakAli_: No. It realises as needed.
03:47Ali_the first one?
03:47kotarakBut whole-numbers retains the head.
03:48kotarakThe the intermediate results will stay in memory.
03:48Ali_how can one make whole-numbers not retain the head?
03:48kotarakRewrite it like this: (reduce + (take 10000000000 (whole-numbers))), whole-numbers is a function returning a fresh sequence of all numbers.
03:49kotarakSo instead of (def whole-numbers (iterate inc 0)) do (defn whole-numbers [] (iterate inc 0))
03:49Ali_thanks. let me try it.
03:50Ali_in this case, it takes the next number, add it, throw it and so on without keeping it in the heap?
03:50kotarakYes.
03:50Ali_cool
03:51kotarakBut when you make whole-numbers a constant not a function, it will hold the head of the sequence. Then the results get cached and stay in heap.
03:51kotarakBut the function returns always a new sequence, so the intermediate results can get garbage collected.
04:21lenst'(1 2 3)
06:41sayyestolifehm
06:41sayyestolifeI'm thinking of doing a small 2d game in Clojure. What graphic library would you guys suggest?
06:55sayyestolifeI don't mind it being quite low level (like SDL)
06:58sayyestolifeI guess I might aswell go with sdljava
07:40yakov2hm
08:08yakov2is there a "instanceof" in Clojure?
08:10hiredman,(doc instance)
08:10clojurebot"/;nil; "
08:10hiredman,(doc instance/)
08:10clojurebotInvalid token: instance/
08:10hiredman,(doc instance?)
08:10clojurebot"([c x]); Evaluates x and tests if it is an instance of the class c. Returns true or false"
08:10hiredmanit is really early
08:11hiredmanlate, whichever
08:13yakov2thanks
08:52Jomyoot_how do I run .clj from command line?
08:55Jomyoot_how would I run .clj from java
09:00RaynesJomyoot_: java -cp <path to clojure.jar>;<path to source files needed by file you want to run> clojure.main <path to source file you want to run>
09:01Jomyoot_does clojure.Lang.Script work anymore?
09:01Jomyoot_I keep finding references to clojure.Lang.Script
09:03slaneyJomyoot_: yes, it works
09:04slaneyI am using it right now
09:04hiredmanslaney: :(
09:04slaneyheh
09:04slaneyI am just getting started
09:04hiredmanclojure.main
09:05slaneyI see
09:05hiredmanScript is old, and less liked
09:06slaneywell, this is a 4 line shell script I wrote to either fire up the repl, or exec a file...will be trivial to change
09:10Jomyoot_where is the contrib place
09:11Jomyoot_clojure.contrib
09:11Jomyoot_is it at google or sourceorge?
09:11Chousukeit's at assembla too
09:11Jomyoot_most recent one where?
09:11Chousukeand github
09:11Chousukehttp://www.assembla.com/spaces/clojure-contrib
09:38Jomyoot_Caused by: java.sql.SQLException: Value '[B@4de35d1a' can not be represented as java.sql.Timestamp
09:39Jomyoot_I get this using the sql contrib
09:39Jomyoot_Is there something i should know?
09:39hiredmanwhat are you trying to do?
09:40hiredmananyway, it looks something is trying to turn a byte array into a sql timestamp
09:40hiredman(and cannot)
09:40Jomyoot_hmm
09:41Jomyoot_(with-connection db
09:41Jomyoot_ (with-query-results rs ["select * from entries where is_hidden = 0 AND type = 'Story'"]
09:41Jomyoot_ (dorun (map #(println (:id %)) rs))))
09:41Jomyoot_Seems simple enough
09:41Jomyoot_I do not have a byte array in that table
09:42Jomyoot_i do have quite a lot of timestamps in there
09:51Jomyoot_am I supposed to use Java's Hashtable?
09:51Jomyoot_can't find something provided by clojrue
09:51hiredmanuh
09:51hiredmanreally?
09:52hiredmanhttp://clojure.org/data_structures
09:53RaynesHe desperately needs a copy of Programming Clojure. :)
10:05Jomyoot_waiting for mine to arrive
10:05Jomyoot_hhave the PDF version
10:05Jomyoot_waiting for paper one
10:15RaynesSince when does Clojure have a while loop? O_o
10:17Chousukesince: Wed Nov 26 23:13:57 2008
10:18RaynesChousuke: I didn't get the memo ;)
10:18RaynesAnd, way to be accurate!
10:18Chousukethat's from git log
10:19RaynesI figured that. :)
10:19Chousukewhile has considerably less use in Clojure than in other languages, though
10:19Chousukebut you could have a (while @running ...) :)
10:27Jomyoot_(with-connection db
10:27Jomyoot_ (with-query-results rs ["SELECT id, name FROM tags WHERE SITE_ID = 1"]
10:27Jomyoot_ (dorun (map #(dosync (assoc tags (:id %) (:name %))) rs))))
10:27Jomyoot_What's wrong with my code
10:27Jomyoot_I do (def tags (hash-map)) earlier
10:28Jomyoot_I wish there are more examples
10:30RaynesChousuke: Good for infinite loops I imagine.
10:45Jomyoot_omg i got it
10:45Jomyoot_(ref) and (deref) and (dosync)
10:51maaclLau_of_DK: hejsa
10:52Lau_of_DKmaacl: Hejsa :)
11:01maaclLau_of_DK: I cannot figure out how to generalize this recursive call to work for s1 being anything else than a string: (str (traceback-lcs m s1 s2 (- i 1) (- j 1)) (nth s1 (- i 1))) - any ideas? I would like for it to return a collection of the same type as s1.
11:02Lau_of_DKmaacl I'd love to, but I gotta run, if you havent worked it out by tomorrow when I get back - we can have a look at it
11:37StartsWithKhi
11:38StartsWithKis there a standard practice for using :arglists metadata to fix args for documentation on fns and macros?
11:39StartsWithKhow do i write :arglists '([name #{deps*}]), it that ok for sets?
11:41Jomyoot_simple question: how do apply (str) to ("a" "b" "c")
11:41Jomyoot_to combine the result
11:41StartsWithKhow do i indicate that #{} is optional to, but can be empty if specified?
11:41StartsWithKJomyoot_: (apply str (list "a" "b" "c"))
11:42Jomyoot_thanks
11:45StartsWithKso is #^{:arglists '([name #{task-deps*}? [property-deps*]? [property-bindings*]? & body])} ok?
11:46StartsWithKor & in front of body is not needed, i see that defn dosn't have it
11:47Jomyoot_last question: how to (apply str (list "a" "b" "c")) but seperate each "a" "b" "c" by a " " space
11:49StartsWithKJomyoot_: (apply str (interpose " " ["a" "b"]))
11:51Jomyoot_well Clojure is awesome
11:51Jomyoot_Lisp is awesome actually
11:51Jomyoot_my code is so short now
11:51StartsWithK:)
11:52Jomyoot_wonder why Lisp was not that popular
11:52Jomyoot_recently
11:52Jomyoot_it wasn't popular recently
11:52Jomyoot_probably was like long itme ago
11:54justin`anyone know how I might reset a "deck" to its original state? It's a var pointing to a sequence
11:55justin`in other words I'm trying to set "deck" to (shuffle (make-deck)) but I'm having trouble figuring out how to do this in a transaction
11:56Chouserif you plan to change a reference
11:56Chouser(like a var)
11:56justin`yup
11:57Chouserin the normal course of a program, then a var is probably not the right choice
11:57Chouserunless each change is local only to one thread
11:57StartsWithKjustin`: maybe (binding [deck (.getRoot deck)] ...)
11:57justin`hmm
11:59Chouserthat binding is right if you want the change to be in effect only within the ... part
11:59StartsWithK(def deck 3)
11:59StartsWithK(binding [deck 4] (println deck) (binding [deck (.getRoot #'deck)] (println deck)))
11:59StartsWithKprints 4\n3
11:59Chouserotherwise a ref or atom may work better for you.
11:59StartsWithKbut that may not be what you realy want
12:00justin`yeah I'll try with a ref
12:10ceninanhow do I stop a macro expansion from namespace-qualifying?
12:12StartsWithKceninan: (defmacro x [] (list 'println "hi")) no namespace-qualifing, (defmacro x [] `(println "x")) namespace qualified for println
12:13ceninanStartsWithK: thanks!
12:48StartsWithKhttp://paste.pocoo.org/show/128073/ i tried to fix arglists, but new declaration is not shown with doc
12:48StartsWithKam i doing something wrong?
12:51cmvkkseems like arglists is probably auto generated by defmacro, and it's overwriting your declaration.
12:52ChousukeStartsWithK: try just putting the metadata map as a regular map after the name of the macro
12:52Chousuke(doc defmacro)
12:52clojurebot"([name doc-string? attr-map? [params*] body] [name doc-string? attr-map? ([params*] body) + attr-map?]); Like defn, but the resulting function name is declared as a macro and will be used as a macro by the compiler when it is called."
12:54StartsWithKChousuke: that worked, thanks
12:56StartsWithKcmvkk: yes, strange as it uses defn to declare a macro with straight (defn name args) call
13:27larryjoeI have a question regarding eval and local bindings... using clojure.contrib.macros/const, (let [x 1] (const x)) gives an UnsupportedOperationException... how does eval know about the external lexical bindings, and why does it not use the dynamic bindings (BTW I like that it exceptions)
13:53bradford,(let [foo 5] (str (:name (meta #'foo))))
13:53clojurebotjava.lang.Exception: Unable to resolve var: foo in this context
13:54bradfordk, that doesnt work...in my repl i already bound foo beforehand :-)
13:54bradfordi'm trying to get the string name corresponding to a vars symbol
13:55bradfordthe other problem is that i need to do it for a collection.
13:56bradfordobviously i can not do this (for [x something] (str (:name (meta #'x))))
14:31IKelvinhi there,
14:31IKelvini'm trying to assign a public class variable.
14:31IKelvinpublic class Foo {
14:31IKelvin public int a;
14:31IKelvin}
14:31IKelvinvoid function(){
14:31IKelvin Foo foo = new Foo();
14:31IKelvin foo.a = 5; // how to write this in clojure?
14:31IKelvin}
14:31IKelvinClojure:
14:31IKelvin(foo (Foo.))
14:31IKelvin(.a foo 5) ; obviously doesn't work
14:31Chouser(set! (.a foo) 5) ; I think
14:31hiredman,(doc set!)
14:31clojurebot"/;nil; "
14:31hiredman#@$@#
14:34IKelvinit worked! Thanks!
14:38Chouserthat's an instance member, by the way
14:39ChouserI guess "class" may be right too, but I thought you meant static at first
14:40IKelvini see
15:39schaefer_i have a smalltalk background and i'm a big fan of named arguments. i like the defnk macro available in contrib and i've been thinking about its performance: it is at least one object allocation followed by several "puts" to a map for making the call and then several "gets" as the function begins executing
15:39schaefer_if the compiler (or reader in clojure's case) is smart enough, all that overhead could be eliminated and turned into a direct stack based call
15:40schaefer_my question is: is there a way (in user space) to smarten up clojure's reader to avoid all that overhead? conceptually, i can see how to write a macro to handle the function side, but i don't know how to do it on the caller side
15:41maacl_Is there a function that works like empty but also works for strings?
15:42hiredman~javadoc java.lang.String
15:42maacl_i.e works both for collections and strings at the same time ?
15:44hiredmanwhy would want that?
15:44hiredmanpresumably you want an empty of something so you can build onto it, and none of the datastructure building functions work on string either
15:45maacl_because I need to return a collection or string depending on what was passed into the function
15:46hiredmansounds like you want defmulti
15:47hiredmand
15:49maacl_yeah, was trying to find a clever way around it
15:49hiredmanwhy?
15:49maacl_that is clever as in "clever"
15:49hiredmandefmulti is nice
15:50maacl_sure, but thought this might be more elegant
15:50hiredmanthe set of operations on String and on Collections are different, so you need two code paths anyway
15:51maacl_well, conj got me part of the way :-)
15:51hiredmanerm
15:51hiredmanconj does not work on strings
15:52maacl_no but on characters which I build the string from
15:53hiredmanso why do you need an empty string then? if you are operating on a list?
15:54maacl_well it turns out I don't because it was a dead end
15:56maacl_looks like defmulti is the way to go - thanks t´for the tip
16:01Chouserschaefer_: I think you could do what you want for simple fn calls just using macros
16:02schaefer__oh? how do you mean?
16:02Chouserschaefer_: that is, write (defmacro my-defnk ...) such that (my-defnk foo ...) expands to (defmacro foo ...)
16:04Chouserthen (foo :a 1 :b 2) would call that generated macro and would figure out at compile time the right way to call the positional args
16:04schaefer__ah... i think i follow you
16:05ChouserI guess (my-defnk foo ...) might actually expand to both (defn foo* ...) and (defmacro foo ...)
16:05schaefer__yes, that makes sense
16:05Chouserso that a call (foo :a 1 :c 3) could expand to (foo* 1 nil 3) or whatever.
16:06Chouserthe problem would be you couldn't pass foo to high-order fns
16:06Chouser(map foo [1 2 3]) wouldn't work.
16:07schaefer__why not? would that just be seen as one 'token' to the macro?
16:08Chousermap isn't a macro -- it's first args needs to be an actual fn, not a macro
16:08schaefer__oh, sorry, i misunderstood your example. i see the problem now
16:09Chousernot even sure how you'd want that to look (even if there were no restrictions imposed by Clojure)
16:09Chouser(map foo :a [1 2 3] :b [4 5 6]) ?
16:09Chouser(map foo [{:a 1 :b 4} {:a 2 :b 5}]) ?
16:09schaefer__well, you could have an arbitrary rule that says use positional args when named args aren't available
16:10Chouserhm... actually, in that case you might be able to abuse the :inline metadata
16:10schaefer__or, you could disallow (map foo [1 2 3]) in favor of declaring a new function: (map (fn [x] (foo :a x)) [1 2 3])
16:11Chouserok. that would be fine too using the multiple macro idea, I think.
16:11schaefer__yes, i think so
16:11ChouserI'm very intruiged by named args, so let me know if you pursue this.
16:11clojurebotthis is not a bug
16:12schaefer__instead of the macro approach, is there a way to influence the reader to teach it new tricks? or is that what the docs mean by "the symbole table isn't available in user space" ?
16:12schaefer__(or something like that)
16:13Chouserright, no adjusting the reader without actually patching the clojure code itself
16:13schaefer__i'm going to play around with this. i see it mainly as a way to dig into macros and understand the innards of clojure
16:13Chouserwhich is an option, of course, but reduces your target market a bit.
16:14schaefer__exactly. whatever i come up with, i want to be within the mainstream
16:20Chouserwhat I'd like to experiment with at some point is named args that somehow default to picking up same-named locals from the calling scope
16:21schaefer__interesting, but i can imagine it being very confusing to someone reading the code
16:21schaefer__in java, i've wished for something similar, particularly for constructors where there is a lot of:
16:22schaefer__public Constructor(Object a, Object b) {this.a=a; this.b=b;)
16:23hiredmanChouser: sounds like binding
16:25ChouserI was thinking something more like (defnk foo [a b c] ...) defining a fn that takes named args a, b and c. You could call this using (foo :a a :b b :c c), but if the names of your locals actually match the arg names like that, there'd be some kind of shortcut
16:25Chousernot sure what it would look like. Maybe (foo ~a ~b ~c) or something :-/
16:26Chouserbecause of course you'd want to still be able to do (foo ~a ~b :c (+ 4 5)) or whatever as needed
16:26ChouserI have no idea if this would end up being useful or not, but it seems to me like it *might* be.
16:26schaefer__yes. i was afraid you were talking about (let [a 1 b 2 c3] (foo))
16:26schaefer__that would be a little too weird, imo
16:27Chouseroh, no. That's too open-ended for me.
16:27kotaraktoo much context to remember...
16:27schaefer__exactly my thinking
16:27Chouseryes
16:27Chousertoo much
16:28Chouserin that case an arg could be "given" at a top level, not mentioned in several calls deeper, and used at a low level.
16:28Chouserwell, like 'binding', but even less explicit. Too scary.
16:28schaefer__i do like the notion of (foo ~a ~b :c (+ 4 5)) primarily because it encourages common variable names across code. one of the things i miss in clojure (god help me for saying so) is the self-documenting that goes along with strong typing
16:29kotaraknaa.. I found clear names just as good..
16:29Chouserschaefer__: well, if you get the compile-time named arg expansion working, we can try tacking ~a on top.
16:30hiredman:/
16:30schaefer__kotorak: agreed. i think chouser's idea helps to encourage common names across code code and, assuming you start with good names...
16:30sgtarr_anyone here using Clojure in enterprise settings?
16:30kotaraksgtarr_: me, as some kind of secret weapon
16:31schaefer__chouser: i'm going to play around with it this week. i'll post whatever i come up with in the forum.
16:31sgtarr_kotarak: sounds good
16:31Chouserschaefer__: cool
16:43sgtarr_kotarak: are you making them believe it's actual java code?
16:44sgtarr_it all compiles into java bytecode anyway :)
16:44kotaraksgtarr_: no, but it's not really professional. :] Some local, home-grown utility. My boss just knows, that it will solve our problems and run where ever Java 1.5 is installed...
16:47sgtarr_I would like to take Clojure to an enterprise setting
16:47sgtarr_I work for a medical tech company and we use various technologies; .NET/C#, Java, C, Python, depending on the product
16:50kotaraksgtarr_: sorry, can't help you on that, I'm not a programmer, more an "engineer"...
17:22pokey19Hello
17:22pokey19how do I write Clojure code in an external file and then have Clojure interpret it?
17:26technomancypokey19: spit and load-file
17:53lisppaste8ceninan pasted "Next time I will learn-_before_-doing..." at http://paste.lisp.org/display/83479
17:54ceninan*shudder*
17:54ceninanlearning-by-learning is a better idea
18:44syamajalais there anything like html-template for clojure?
18:45syamajalai looked at enlive, but its not quite what i want
18:50Chousukeclj-html or compojure.html perhaps?
19:33mattrepladded tests for clojure-cassandra, but when running 'run-tests nothing is printed out
19:33mattreplanything obviously wrong? http://github.com/mattrepl/clojure-cassandra/blob/812fb1339ce170a468c8578c274f1aec10c9c6a8/src/cassandra/test.clj
19:34mattreplI just use the test namespace and clojure.test, then call run-tests with the test namespace as the only arg
19:47mattrepllove it... changed my init and had forgot to add output redirection back in
22:59technomancywow, this is pretty slick: http://ideolalia.com/rendering-textures-in-clojure-0
22:59technomancy(using the penumbra openGL library)