#clojure log - Jul 12 2009

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

1:16 Ali_: I am learning clojure, can I ask a question here?

1:24 cmvkk: yes.

1:34 Ali_: I am receiving IllegalArgumentException when I define this function

1:34 (defn pascal-triangle

1:34 "Returns a lazy sequence of all the rows in pascal triangel."

1:34 (iterate

1:34 (fn [row]

1:34 (concat '(1)

1:34 (map #(reduce + %) (partition 2 1 row))

1:34 '(1)))

1:34 '(1 1)))

1:35 Don't know how to create ISeq from Symbol.

1:35 Where in this definition is it creating ISeq from symbol

1:35 cmvkk: probably partition.

1:35 Ali_: I tried this separately

1:36 (defn p-t-row [r]

1:36 (concat '(1)

1:36 (map #(reduce + %) (partition 2 1 r))

1:36 '(1)))

1:36 this works and every time I call it I get a new row from pascal triangle

1:42 cmvkk: oh, duh. the simplist things are the easiest to overlook.

1:42 for pascal-triangle, you want def not defn.

1:43 it's expecting a function definition with an arglist, and it's choking because there isn't one...

1:43 Ali_: I will look into that. But I was looking at the code for fibs in lazy-seqs which is similar and it uses defn

1:44 isn't it?

1:44 cmvkk: well you can use this as a function, but it at least needs an empty arglist.

1:44 just put [] after the docstring.

1:44 Ali_: oh, yes yes

1:44 thanks

1:44 I forgot about that, thanks a lot

1:53 still not working

1:53 I am getting wrong number of arguments to iterate now

1:53 after I added the empty arglist or using def

1:53 (defn pascal-triangle

1:53 "Returns a lazy sequence of all the rows in pascal triangle."

1:53 []

1:53 (iterate

1:53 (fn [row]

1:53 (concat '(1)

1:53 (map #(reduce + %) (partition 2 1 row))

1:53 '(1))

1:54 [1])))

1:55 sorry, it was misplaced parens

2:05 Jomyoot: Is there an example on how to structure project folders/name spaces and etc?

2:05 is it similar to how I would do packages in Java?

2:05 kotarak: Jomyoot: I structure my projects like this:

2:06 Jomyoot: toplevel build.xml, ivy stuff, README, etc.

2:06 Jomyoot: since' there is no objects in clojure.

2:06 do I still need to keep that package/folder relationship?

2:06 like in java?

2:06 com.what.here

2:06 kotarak: Jomyoot: 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

2:06 Jomyoot: goes into src/com/wht/here

2:07 kotarak: Jomyoot: compiled stuff goes into classes, jars etc. in build

2:07 Jomyoot: kotarak: I use intelliJ

2:07 with La Clojure

2:07 Hope it's good enough

2:07 kotarak: Jomyoot: Don't know about IntelliJ. Do everything manually and work with Vim.

2:08 But the layout proved it's usefulness.

2:08 To your questions: namespaces follow Java conventions.

2:08 Jomyoot: Do I need to use Maven?

2:08 kotarak: No. I use eg. Ivy. But even that is optional.

2:08 Jomyoot: for many of the plugins?

2:08 kotarak: You can use make if you like.

2:09 (ns foo.bar-baz.frobincator) must go to (considering the structure above) src/foo/bar_baz/frobnicator.clj

2:10 Jomyoot: ok

2:11 I tried Scala

2:11 kotarak: Jomyoot: don't forget the - vs. _ translation.

2:12 Jomyoot: Didn't see enough Magick

2:12 it's like a terse version of Java

2:12 but that's it

2:12 Was trying to find magick in it

2:14 clj file names aren't capitalized?

2:15 kotarak: It depends.

2:15 (ns foo.bar.Baz) must be capitalised.

2:16 (ns foo.bar.baz) not.

2:18 Jomyoot: hmm

2:37 does clojure get compiled to .class?

2:37 clj --> .class?

2:37 Should I expect it to?

2:37 cmvkk: if you use genclass. otherwise, when clojure is compiled, every function becomes its own class.

2:38 Jomyoot: so when i hit compile, i should be getting bunches of .class files right?

2:38 so my IDE is not set up right

2:38 nothing comes out

2:39 cmvkk: yeah if nothing comes out that's probably wrong.

3:46 Ali_: How does this form run inside the jvm?

3:46 (reduce + (take 1000000000 whole-numbers)

3:46 Does it take the first number add it, throw it, take the next, take the next and add it, ....

3:46 Or does it realize the first billion numbers and then add all the numbers in the seq?

3:47 kotarak: Ali_: No. It realises as needed.

3:47 Ali_: the first one?

3:47 kotarak: But whole-numbers retains the head.

3:48 The the intermediate results will stay in memory.

3:48 Ali_: how can one make whole-numbers not retain the head?

3:48 kotarak: Rewrite it like this: (reduce + (take 10000000000 (whole-numbers))), whole-numbers is a function returning a fresh sequence of all numbers.

3:49 So instead of (def whole-numbers (iterate inc 0)) do (defn whole-numbers [] (iterate inc 0))

3:49 Ali_: thanks. let me try it.

3:50 in this case, it takes the next number, add it, throw it and so on without keeping it in the heap?

3:50 kotarak: Yes.

3:50 Ali_: cool

3:51 kotarak: But 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.

3:51 But the function returns always a new sequence, so the intermediate results can get garbage collected.

4:21 lenst: '(1 2 3)

6:41 sayyestolife: hm

6:41 I'm thinking of doing a small 2d game in Clojure. What graphic library would you guys suggest?

6:55 I don't mind it being quite low level (like SDL)

6:58 I guess I might aswell go with sdljava

7:40 yakov2: hm

8:08 is there a "instanceof" in Clojure?

8:10 hiredman: ,(doc instance)

8:10 clojurebot: "/;nil; "

8:10 hiredman: ,(doc instance/)

8:10 clojurebot: Invalid token: instance/

8:10 hiredman: ,(doc instance?)

8:10 clojurebot: "([c x]); Evaluates x and tests if it is an instance of the class c. Returns true or false"

8:10 hiredman: it is really early

8:11 late, whichever

8:13 yakov2: thanks

8:52 Jomyoot_: how do I run .clj from command line?

8:55 how would I run .clj from java

9:00 Raynes: Jomyoot_: 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>

9:01 Jomyoot_: does clojure.Lang.Script work anymore?

9:01 I keep finding references to clojure.Lang.Script

9:03 slaney: Jomyoot_: yes, it works

9:04 I am using it right now

9:04 hiredman: slaney: :(

9:04 slaney: heh

9:04 I am just getting started

9:04 hiredman: clojure.main

9:05 slaney: I see

9:05 hiredman: Script is old, and less liked

9:06 slaney: well, this is a 4 line shell script I wrote to either fire up the repl, or exec a file...will be trivial to change

9:10 Jomyoot_: where is the contrib place

9:11 clojure.contrib

9:11 is it at google or sourceorge?

9:11 Chousuke: it's at assembla too

9:11 Jomyoot_: most recent one where?

9:11 Chousuke: and github

9:11 http://www.assembla.com/spaces/clojure-contrib

9:38 Jomyoot_: Caused by: java.sql.SQLException: Value '[B@4de35d1a' can not be represented as java.sql.Timestamp

9:39 I get this using the sql contrib

9:39 Is there something i should know?

9:39 hiredman: what are you trying to do?

9:40 anyway, it looks something is trying to turn a byte array into a sql timestamp

9:40 (and cannot)

9:40 Jomyoot_: hmm

9:41 (with-connection db

9:41 (with-query-results rs ["select * from entries where is_hidden = 0 AND type = 'Story'"]

9:41 (dorun (map #(println (:id %)) rs))))

9:41 Seems simple enough

9:41 I do not have a byte array in that table

9:42 i do have quite a lot of timestamps in there

9:51 am I supposed to use Java's Hashtable?

9:51 can't find something provided by clojrue

9:51 hiredman: uh

9:51 really?

9:52 http://clojure.org/data_structures

9:53 Raynes: He desperately needs a copy of Programming Clojure. :)

10:05 Jomyoot_: waiting for mine to arrive

10:05 hhave the PDF version

10:05 waiting for paper one

10:15 Raynes: Since when does Clojure have a while loop? O_o

10:17 Chousuke: since: Wed Nov 26 23:13:57 2008

10:18 Raynes: Chousuke: I didn't get the memo ;)

10:18 And, way to be accurate!

10:18 Chousuke: that's from git log

10:19 Raynes: I figured that. :)

10:19 Chousuke: while has considerably less use in Clojure than in other languages, though

10:19 but you could have a (while @running ...) :)

10:27 Jomyoot_: (with-connection db

10:27 (with-query-results rs ["SELECT id, name FROM tags WHERE SITE_ID = 1"]

10:27 (dorun (map #(dosync (assoc tags (:id %) (:name %))) rs))))

10:27 What's wrong with my code

10:27 I do (def tags (hash-map)) earlier

10:28 I wish there are more examples

10:30 Raynes: Chousuke: Good for infinite loops I imagine.

10:45 Jomyoot_: omg i got it

10:45 (ref) and (deref) and (dosync)

10:51 maacl: Lau_of_DK: hejsa

10:52 Lau_of_DK: maacl: Hejsa :)

11:01 maacl: Lau_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:02 Lau_of_DK: maacl 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:37 StartsWithK: hi

11:38 is there a standard practice for using :arglists metadata to fix args for documentation on fns and macros?

11:39 how do i write :arglists '([name #{deps*}]), it that ok for sets?

11:41 Jomyoot_: simple question: how do apply (str) to ("a" "b" "c")

11:41 to combine the result

11:41 StartsWithK: how do i indicate that #{} is optional to, but can be empty if specified?

11:41 Jomyoot_: (apply str (list "a" "b" "c"))

11:42 Jomyoot_: thanks

11:45 StartsWithK: so is #^{:arglists '([name #{task-deps*}? [property-deps*]? [property-bindings*]? & body])} ok?

11:46 or & in front of body is not needed, i see that defn dosn't have it

11:47 Jomyoot_: last question: how to (apply str (list "a" "b" "c")) but seperate each "a" "b" "c" by a " " space

11:49 StartsWithK: Jomyoot_: (apply str (interpose " " ["a" "b"]))

11:51 Jomyoot_: well Clojure is awesome

11:51 Lisp is awesome actually

11:51 my code is so short now

11:51 StartsWithK: :)

11:52 Jomyoot_: wonder why Lisp was not that popular

11:52 recently

11:52 it wasn't popular recently

11:52 probably was like long itme ago

11:54 justin`: anyone know how I might reset a "deck" to its original state? It's a var pointing to a sequence

11:55 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:56 Chouser: if you plan to change a reference

11:56 (like a var)

11:56 justin`: yup

11:57 Chouser: in the normal course of a program, then a var is probably not the right choice

11:57 unless each change is local only to one thread

11:57 StartsWithK: justin`: maybe (binding [deck (.getRoot deck)] ...)

11:57 justin`: hmm

11:59 Chouser: that binding is right if you want the change to be in effect only within the ... part

11:59 StartsWithK: (def deck 3)

11:59 (binding [deck 4] (println deck) (binding [deck (.getRoot #'deck)] (println deck)))

11:59 prints 4\n3

11:59 Chouser: otherwise a ref or atom may work better for you.

11:59 StartsWithK: but that may not be what you realy want

12:00 justin`: yeah I'll try with a ref

12:10 ceninan: how do I stop a macro expansion from namespace-qualifying?

12:12 StartsWithK: ceninan: (defmacro x [] (list 'println "hi")) no namespace-qualifing, (defmacro x [] `(println "x")) namespace qualified for println

12:13 ceninan: StartsWithK: thanks!

12:48 StartsWithK: http://paste.pocoo.org/show/128073/ i tried to fix arglists, but new declaration is not shown with doc

12:48 am i doing something wrong?

12:51 cmvkk: seems like arglists is probably auto generated by defmacro, and it's overwriting your declaration.

12:52 Chousuke: StartsWithK: try just putting the metadata map as a regular map after the name of the macro

12:52 (doc defmacro)

12:52 clojurebot: "([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:54 StartsWithK: Chousuke: that worked, thanks

12:56 cmvkk: yes, strange as it uses defn to declare a macro with straight (defn name args) call

13:27 larryjoe: I 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:53 bradford: ,(let [foo 5] (str (:name (meta #'foo))))

13:53 clojurebot: java.lang.Exception: Unable to resolve var: foo in this context

13:54 bradford: k, that doesnt work...in my repl i already bound foo beforehand :-)

13:54 i'm trying to get the string name corresponding to a vars symbol

13:55 the other problem is that i need to do it for a collection.

13:56 obviously i can not do this (for [x something] (str (:name (meta #'x))))

14:31 IKelvin: hi there,

14:31 i'm trying to assign a public class variable.

14:31 public class Foo {

14:31 public int a;

14:31 }

14:31 void function(){

14:31 Foo foo = new Foo();

14:31 foo.a = 5; // how to write this in clojure?

14:31 }

14:31 Clojure:

14:31 (foo (Foo.))

14:31 (.a foo 5) ; obviously doesn't work

14:31 Chouser: (set! (.a foo) 5) ; I think

14:31 hiredman: ,(doc set!)

14:31 clojurebot: "/;nil; "

14:31 hiredman: #@$@#

14:34 IKelvin: it worked! Thanks!

14:38 Chouser: that's an instance member, by the way

14:39 I guess "class" may be right too, but I thought you meant static at first

14:40 IKelvin: i see

15:39 schaefer_: 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:39 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:40 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:41 maacl_: Is there a function that works like empty but also works for strings?

15:42 hiredman: ~javadoc java.lang.String

15:42 maacl_: i.e works both for collections and strings at the same time ?

15:44 hiredman: why would want that?

15:44 presumably you want an empty of something so you can build onto it, and none of the datastructure building functions work on string either

15:45 maacl_: because I need to return a collection or string depending on what was passed into the function

15:46 hiredman: sounds like you want defmulti

15:47 d

15:49 maacl_: yeah, was trying to find a clever way around it

15:49 hiredman: why?

15:49 maacl_: that is clever as in "clever"

15:49 hiredman: defmulti is nice

15:50 maacl_: sure, but thought this might be more elegant

15:50 hiredman: the set of operations on String and on Collections are different, so you need two code paths anyway

15:51 maacl_: well, conj got me part of the way :-)

15:51 hiredman: erm

15:51 conj does not work on strings

15:52 maacl_: no but on characters which I build the string from

15:53 hiredman: so why do you need an empty string then? if you are operating on a list?

15:54 maacl_: well it turns out I don't because it was a dead end

15:56 looks like defmulti is the way to go - thanks t´for the tip

16:01 Chouser: schaefer_: I think you could do what you want for simple fn calls just using macros

16:02 schaefer__: oh? how do you mean?

16:02 Chouser: schaefer_: that is, write (defmacro my-defnk ...) such that (my-defnk foo ...) expands to (defmacro foo ...)

16:04 then (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:04 schaefer__: ah... i think i follow you

16:05 Chouser: I guess (my-defnk foo ...) might actually expand to both (defn foo* ...) and (defmacro foo ...)

16:05 schaefer__: yes, that makes sense

16:05 Chouser: so that a call (foo :a 1 :c 3) could expand to (foo* 1 nil 3) or whatever.

16:06 the problem would be you couldn't pass foo to high-order fns

16:06 (map foo [1 2 3]) wouldn't work.

16:07 schaefer__: why not? would that just be seen as one 'token' to the macro?

16:08 Chouser: map isn't a macro -- it's first args needs to be an actual fn, not a macro

16:08 schaefer__: oh, sorry, i misunderstood your example. i see the problem now

16:09 Chouser: not even sure how you'd want that to look (even if there were no restrictions imposed by Clojure)

16:09 (map foo :a [1 2 3] :b [4 5 6]) ?

16:09 (map foo [{:a 1 :b 4} {:a 2 :b 5}]) ?

16:09 schaefer__: well, you could have an arbitrary rule that says use positional args when named args aren't available

16:10 Chouser: hm... actually, in that case you might be able to abuse the :inline metadata

16:10 schaefer__: 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:11 Chouser: ok. that would be fine too using the multiple macro idea, I think.

16:11 schaefer__: yes, i think so

16:11 Chouser: I'm very intruiged by named args, so let me know if you pursue this.

16:11 clojurebot: this is not a bug

16:12 schaefer__: 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:12 (or something like that)

16:13 Chouser: right, no adjusting the reader without actually patching the clojure code itself

16:13 schaefer__: 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:13 Chouser: which is an option, of course, but reduces your target market a bit.

16:14 schaefer__: exactly. whatever i come up with, i want to be within the mainstream

16:20 Chouser: what 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:21 schaefer__: interesting, but i can imagine it being very confusing to someone reading the code

16:21 in java, i've wished for something similar, particularly for constructors where there is a lot of:

16:22 public Constructor(Object a, Object b) {this.a=a; this.b=b;)

16:23 hiredman: Chouser: sounds like binding

16:25 Chouser: I 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:25 not sure what it would look like. Maybe (foo ~a ~b ~c) or something :-/

16:26 because of course you'd want to still be able to do (foo ~a ~b :c (+ 4 5)) or whatever as needed

16:26 I have no idea if this would end up being useful or not, but it seems to me like it *might* be.

16:26 schaefer__: yes. i was afraid you were talking about (let [a 1 b 2 c3] (foo))

16:26 that would be a little too weird, imo

16:27 Chouser: oh, no. That's too open-ended for me.

16:27 kotarak: too much context to remember...

16:27 schaefer__: exactly my thinking

16:27 Chouser: yes

16:27 too much

16:28 in that case an arg could be "given" at a top level, not mentioned in several calls deeper, and used at a low level.

16:28 well, like 'binding', but even less explicit. Too scary.

16:28 schaefer__: 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:29 kotarak: naa.. I found clear names just as good..

16:29 Chouser: schaefer__: well, if you get the compile-time named arg expansion working, we can try tacking ~a on top.

16:29 * kotarak is not a typing foe. likes OCaml a lot...

16:30 hiredman: :/

16:30 schaefer__: kotorak: agreed. i think chouser's idea helps to encourage common names across code code and, assuming you start with good names...

16:30 sgtarr_: anyone here using Clojure in enterprise settings?

16:30 * hiredman likes map destructuring

16:30 kotarak: sgtarr_: me, as some kind of secret weapon

16:31 schaefer__: chouser: i'm going to play around with it this week. i'll post whatever i come up with in the forum.

16:31 sgtarr_: kotarak: sounds good

16:31 Chouser: schaefer__: cool

16:43 sgtarr_: kotarak: are you making them believe it's actual java code?

16:44 it all compiles into java bytecode anyway :)

16:44 kotarak: sgtarr_: 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:47 sgtarr_: I would like to take Clojure to an enterprise setting

16:47 I work for a medical tech company and we use various technologies; .NET/C#, Java, C, Python, depending on the product

16:50 kotarak: sgtarr_: sorry, can't help you on that, I'm not a programmer, more an "engineer"...

17:22 pokey19: Hello

17:22 how do I write Clojure code in an external file and then have Clojure interpret it?

17:26 technomancy: pokey19: spit and load-file

17:53 lisppaste8: ceninan pasted "Next time I will learn-_before_-doing..." at http://paste.lisp.org/display/83479

17:54 ceninan: *shudder*

17:54 learning-by-learning is a better idea

18:44 syamajala: is there anything like html-template for clojure?

18:45 i looked at enlive, but its not quite what i want

18:50 Chousuke: clj-html or compojure.html perhaps?

19:33 mattrepl: added tests for clojure-cassandra, but when running 'run-tests nothing is printed out

19:33 anything obviously wrong? http://github.com/mattrepl/clojure-cassandra/blob/812fb1339ce170a468c8578c274f1aec10c9c6a8/src/cassandra/test.clj

19:34 I just use the test namespace and clojure.test, then call run-tests with the test namespace as the only arg

19:47 love it... changed my init and had forgot to add output redirection back in

22:59 technomancy: wow, this is pretty slick: http://ideolalia.com/rendering-textures-in-clojure-0

22:59 (using the penumbra openGL library)

Logging service provided by n01se.net