2008-09-14
| 02:24 | Guest55704 | I have a basic clojure question regarding loops... |
| 02:25 | tgreen | um...anybody awake atm? |
| 02:26 | arbscht | ask your question anyway |
| 02:27 | tgreen | ok. so I'm attempting to create a 2D array like this ((1111)(2222)(3333)(4444)). |
| 02:27 | tgreen | code: ( let [x 1] (replicate 4 (replicate 4 x ) )) |
| 02:28 | tgreen | this get's me to ((1111)(1111)...), but I'm a bit unclear on how to increment x |
| 02:29 | arbscht | that's a list of lists, btw, not an array |
| 02:30 | arbscht | perhaps you could do something like (map (partial replicate 4) (list 1 2 3 4)) |
| 02:31 | tgreen | cool. so the clojure/lisp way is instead of passing in a var and incrementing in a loop, you reference a list and iterate over that list. |
| 02:32 | arbscht | if you wanted to use loop, you could do that too |
| 02:37 | arbscht | here's a quick first pass at doing it with loop http://paste.lisp.org/display/66794 |
| 02:37 | tgreen | thanks. looking at it now. |
| 02:41 | tgreen | ok. so you can mutate local variables? docs "(inc x) returns a number one greater than num", so within the loop, there is an implicit let, and that means we can mutate x? |
| 02:42 | arbscht | you can rebind variables in a loop using recur |
| 02:42 | arbscht | it's not mutation |
| 02:45 | tgreen | ok. thanks again. |
| 07:53 | achim_p | hi! |
| 07:54 | achim_p | i'm trying to evaluate forms to be loaded from a file with a given binding. i tried using "load-...", with no success: |
| 07:54 | achim_p | (let [x 2] (load-string "x")) ;=> Unable to resolve symbol: x in this context |
| 07:54 | achim_p | any ideas? |
| 09:50 | Chouser | achim_p_: not quite sure what you're trying to do, but perhaps try "binding" instead of "let"? |
| 10:17 | achim_p_ | Chouser: it's for a small html templating lib i'm writing. i'd like to have template files with something like (html (body (h1 -page-title-))) and then use (template [-page-title- "Foo"] "templatefile.tmpl") to get the html. so rebinding globals wouldn't help in this case, but thanks for your suggestion! |
| 10:18 | achim_p_ | got it working now (i think), using read and macros |
| 10:19 | Chouser | ok |
| 11:00 | achim_p_ | erm, probably a rather strange question: is there a way to get to the 'contents' of a LispReader$Unquote? |
| 11:00 | lisppaste8 | achim_p pasted "unquote" at http://paste.lisp.org/display/66807 |
| 11:15 | rhickey | achim_p_: the use of unquote outside of syntax-quote is undefined and unsupported |
| 11:25 | achim_p_ | rhickey: okay, thanks! i was afraid it would be ... |
| 12:53 | meredydd | Hey all. What's happened to load-resource, now contrib.lib-type functionality has been pulled into mainline? |
| 13:07 | Chouser | meredydd: renamed to just load |
| 13:07 | meredydd | Okay... |
| 13:07 | meredydd | ...ah. |
| 13:08 | meredydd | Much nicer. And I see it's in the (ns) macro now...excellent. |
| 15:39 | Chouser | I thought I'd seen obfuscated JS before, but nothing as perverse as this. |
| 15:45 | rhickey | oh, yeah |
| 15:46 | rhickey | Linj was an attempt to generate readable Java from a Lisp, but the Lisp was sort of like Java-with-parens |
| 15:48 | Chouser | Having real closures is nice on the emit side, but what you end up with is deeply nested impenetrableness. |
| 15:50 | rhickey | still, there are only a handful of primitive expressions, so once you get them right, arbitrary nesting, while scary looking, is still reliable |
| 15:53 | Chouser | sure. But they're not right yet. ;-) |
| 15:54 | Chouser | Right now I've got a local (generated by a macro) that is getting called but never initialized. |
| 15:54 | Chouser | Maybe I shouldn't be trying "for" quite yet. :-) |
| 15:55 | rhickey | probably not :) |
| 16:11 | Chouser | ah, I've got a localbinding for the enclosing fn, but I'm not initializing it. |
| 16:15 | Chouser | Hm, how can I know which of FnMethod's locals refers to the FnExpr? |
| 16:19 | rhickey | Chouser: why would you need to do that? |
| 16:21 | Chouser | The body of a named fn is apparently using a localbinding to talk about itself. I see this localbinding in the fnmethod's locals list, but I don't see how I'm supposed to know it's talking about this fn. |
| 16:22 | Chouser | (fn forever [] (forever)) |
| 16:22 | Chouser | oh, I guess it's going to have the same name. |
| 16:23 | Chouser | of course that's rather a recurring feeling with this project. |
| 16:29 | achim_p_ | hi! final question for today :) - how to locate (by absolute path or url) a resource on the classpath as it would be found by "load"? i tried (.findResource (RT/baseLoader) file), which yielded nil, which confuses me, since both load and (.getResourceAsStream (RT/baseLoader) file) work. |
| 16:31 | rhickey | achim_p_: use getResource |
| 16:34 | rhickey | Chouser: yeah, I don't need to deal with that because I get the fn as 'this' |
| 17:03 | achim_p_ | rhickey: okay, so no paths, i reckon ... it's probably better not to rely on cp entries mapping to fs paths anyway - thanks! |
| 17:04 | rhickey | achim_p_: right, if you use the API you don't care where they are |
| 17:35 | tgreen | <newbie question> how do i make a list of increasing numbers? i've given this a shot : http://paste.lisp.org/display/66830 |
| 17:36 | rhickey | tgreen: (range 10) |
| 17:36 | tgreen | thanks! |
| 17:38 | tgreen | btw - i've read the website and i'm attempting to understand this language. But it's pretty different from what I'm used to (java/python/c#). Any pointers to references/tutorials to jumpstart seasoned developers that know nothing about lisp? |
| 17:38 | tgreen | for instance, this was a great help : http://www.moxleystratton.com/article/clojure/for-non-lisp-programmers |
| 17:41 | tgreen | for instance, I'm attempting to bootstrap my way into clojure by writing simple code samples in python, and then attempting the same thing in clojure. like, rotate an NxN matrix 90 degrees. that sort of thing. but i'm constantly running into issues like, how do i dereference a 2 d array? ...etc. |
| 17:41 | rhickey | tgreen: there's also the wiki: http://en.wikibooks.org/wiki/Clojure_Programming, and this series: http://writingcoding.blogspot.com/2008/06/clojure-series-table-of-contents.html |
| 17:45 | tgreen | two great links. thanks again. i can't wait to have that 'aha' moment w/ clojure. |
| 18:14 | Chouser | tgreen: I frequently recommend projecteuler.net for set of tasks that gently increase in complexity. |
| 18:15 | Chouser | I'm afraid that's more on the side of giving you questions than it is answering them. |
| 18:18 | tgreen | Chouser: that's great! good for interview practice and general skills. |
| 18:30 | Chouser | it's what convinced me that Rhino's not ready for general-purpose programming. :-) |
| 18:41 | arohner | is it bad form to do: (dosync #( foo))? |
| 18:45 | Chouser | I wouldn't think so. |
| 18:46 | Chouser | But this is not a strong area for me. ;-) |
| 18:46 | arohner | I find myself commonly writing functions that are "do the thing I care about", and then a separate function to "do stuff to the ref" |
| 18:46 | arohner | i.e. |
| 18:47 | arohner | (def ref_struct (ref my_struct)) |
| 18:47 | Chouser | I think that general pattern has actually been recommended by rhickey. |
| 18:47 | arohner | (def do_stuff [my_struct]) |
| 18:47 | arohner | (def ref_do_stuff [ref] (dosync #(do_stuff %))) |
| 18:47 | achim_p_ | rhickey: can't boot.clj's root-directory fn be public? otherwise it's hard to implement classpath loading that is consistens with load's path handling |
| 18:48 | achim_p_ | (see paste) |
| 18:48 | lisppaste8 | achim_p pasted "resource-stream" at http://paste.lisp.org/display/66837 |
| 18:53 | Chouser | arohner: oh, maybe it's better to do have (def compute_stuff [my_data]) (def do_stuff [ref] (dosync (alter compute_stuff ref))) |
| 18:53 | Chouser | that is, one func that knows how to do the work on that kind of data, regardless of whether it's in a ref or not. |
| 18:54 | arohner | right |
| 18:54 | Chouser | ...and then another func that knows how to apply that work to a ref. |
| 18:54 | arohner | yeah, that's what I've been doing |
| 18:54 | rhickey | arohner: you should end up with more pure fns than transactional ones. |
| 21:38 | Chouser | http://n01se.net/paste/23H -- t01.cljs: ClojureScript, works in FF3 and Chrome |
| 21:39 | rhickey | cool! |
| 21:39 | rhickey | showing off with for, eh? :) |
| 21:42 | rhickey | what does that generate? |
| 21:52 | Chouser | yeah, showing off. what's the point in using clojure if you don't do something JS can't. :-) |
| 21:53 | rhickey | I think it's great - congrats! |
| 21:53 | Chouser | heh, thanks. |
| 21:53 | Chouser | still lots to do. Probably a little bit in the emit code, but a whole lot of RT |
| 21:54 | rhickey | yeah, the runtime is not nothing... |
| 21:57 | Chouser | emits: http://n01se.net/paste/4m8 |
| 21:57 | Chouser | it's all checked into clojure-contrib/clojurescript |
| 21:59 | Chouser | that includes the clojure patch, which makes a bunch of Expr classes public and adds *allow-unresolved-var* |
| 22:06 | rhickey | I was thinking about the vars thing, it seems like a big loss to just ignore var resolution. One option is to interpret defs during compilation just establishing the vars (not evaluating the init expr). top-level do can unroll its contents into the top level. Then the only thing you wouldn't resolve would be truly nested defs |
| 22:08 | Chouser | makes sense. |
| 23:05 | Chouser | :interpose might be nice to have in "for" |