Re: Eval not working on unexpanded macro quote
Hi, On Aug 21, 8:34 am, gutzofter wrote: > (defmacro macro-hello [] > `"hello") > > (defn -main [] > (println (eval '(= 0 0))) ;works > (println (eval '(com.yourcompany.defpackage/macro-hello))) ;works > (println (eval `(macro-hello))) ;works > (println (eval '(macro-hello))) ;does not work > ) > > seems that backquote resolves namespace while quote doesn't. > > Can you point me in the right direction concerning backquote and > quote? http://clojure.org/reader My suspicion is, that the macro call is done by `eval` in the clojure.core namespace, not the namespace of your file. The ` resolves the symbol to `com.yourcompany.defpackage/macro-hello` which will also work in the clojure.core namespace, since it is fully qualified. ' does not resolve the symbol and since clojure.core doesn't have a `macro-hello`, you get the error. For real-life use, you should only use `. ' is only useful for well- defined DSLs, but not for macros. (read: hygiene) Avoid `eval`. It will come back to haunt you. Sincerely Meikel --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: clojure success story ... hopefully :-)
bradford cross writes: > Hi Chad, yep, that was me. We do hope to open source some stuff soon. > > First will probably be our wrappers for cascading/hadoop and s3. Those would be of great interest to many of us. Please do. --J. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: replace-subtree for zippers?
Meikel Brandmeyer writes: > A transcript: > > ; Create trees... > user=> (def t1 [1 [[2 3] 4]]) > #'user/t1 > user=> (def t2 [[[:a :b] :c] :d]) > #'user/t2 > > ; Create zippers and navigate to subtrees... > user=> (def zt1 (-> (zip/vector-zip t1) zip/down zip/right)) > #'user/zt1 > user=> (zip/node zt1) > [[2 3] 4] > user=> (def zt2 (-> (zip/vector-zip t2) zip/down)) > #'user/zt2 > user=> (zip/node zt2) > [[:a :b] :c] > > ; Replace the subtree from first zipper with subtree from second > zipper... > user=> (def zt1 (zip/replace zt1 (zip/node zt2))) > #'user/zt1 > user=> (zip/root zt1) > [1 [[:a :b] :c]] > > Isn't that what you want? It isn't what I want. But that's because I misspecified what I actually wanted. I didn't think about the problem enough. I need something more akin to a splice function: (splice tree1 index tree2) (splice '(1 2 (3 4 5) 6) 4 '(7 8 (9 10))) should produce => '(1 2 (3 7 8 (9 10) 6)) think crossover in genetic programming (this is actually what I need this for). --J. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Transcript of Rich's "Clojure for Lispers" talk uploaded
Great job doing the transcript! On Aug 13, 10:40 pm, Andy Fingerhut wrote: > This is the same Clojure for Lispers talk with audio, and video of > slides, available on clojure.blip.tv, among others, from the September > 2008 Boston Lisp meeting. > > It has been uploaded to the files section of the group with this name: > > clojure-for-lispers-transcript.txt > > I've added a very few references to books and articles that Rich > mentions during the talk, but other than that, it is just the text of > what he says, and a fraction of what the audience asks (some of them > are much more difficult to understand in the audio). > > People are welcome to put this in some more easily accessible place, > if you know how and have the access to it. For example, if a link to > it could be put on clojure.blip.tv, that might be cool. > > Andy --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: replace-subtree for zippers?
Hi, On Aug 21, 12:05 pm, Jan Rychter wrote: > It isn't what I want. But that's because I misspecified what I actually > wanted. I didn't think about the problem enough. I need something more > akin to a splice function: > > (splice tree1 index tree2) > > (splice '(1 2 (3 4 5) 6) 4 '(7 8 (9 10))) > should produce => '(1 2 (3 7 8 (9 10) 6)) (defn zip-splice [loc node new-node] (loop [loc loc] (cond (zip/end? loc) (throw (Exception. "Node not found")) (= (zip/node loc) node) (let [childs (zip/children new-node) loc(reduce #(zip/insert-left %1 %2) loc childs)] (zip/remove loc)) :else (recur (zip/next loc) user=> (def z (zip/vector-zip [1 2 [3 4 5] 6])) #'user/z user=> (def s (zip/vector-zip [7 8 [9 10]])) #'user/s user=> (zip/root (zip-splice z 4 s)) [1 2 [3 7 8 [9 10] 5] 6] Be aware of the '=' there. This you might want to replace with some smarter function to check for the identity of a node. I stumpled over that in this thread: http://groups.google.com/group/clojure/browse_thread/thread/bfd6539ec367a95b I'm not really a tree expert. So if you have solved this problem, please let me know. I'm still interested in this. Sincerely Meikel --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
read txt file
Hi all, I have created the function that read a txt file. I have a very little experience in clojure and I will be thankfull for any correction and ideas. This is source code: (defn create-freader [filename] (def f-reader (new java.io.FileReader filename)) f-reader) (defn create-breader [f-reader] (def b-reader (new java.io.BufferedReader f-reader)) b-reader) (defn close-freader [f-reader] (. f-reader close)) (defn eof [line] (if (= line nil) true false)) (defn read-lines [reader] (loop [result []] (def next-line (. reader readLine)) (if (eof next-line) result (recur (conj result next-line) (defn read-file [filename] (def f-reader (create-freader filename)) (def b-reader (create-breader f-reader)) (def lines (read-lines b-reader)) (close-freader f-reader) lines) user ==> (read-file "my_file") This is source code in pastebin (http://pastebin.com/m477bd86d). Thanks in advance. Andrey. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Eval not working on unexpanded macro quote
On Thu, 2009-08-20 at 21:52 -0700, gutzofter wrote: > thanks for the version number: > > Clojure 1.1.0-alpha-SNAPSHOT > > is this from the github? It's from github, yes. The version number is actually a bit meaningless: the 'master' branch on github has been on 1.1.0-alpha-SNAPSHOT ever since 1.0 was branched and released. Regards, -- Michel --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: read txt file
Hi, On Aug 21, 10:11 am, DemAS wrote: > I have created the function that read a txt file. I have a very > little experience in clojure and I will be thankfull for any > correction and ideas. For the record: see (doc line-seq) But here some suggestions, since learning clojure is your main objective, reading a file is only the playground... * `def` is only used at the toplevel, to declare locals use let: (let [f-reader (..) b-reader (...) ...] ...) * The `def` (and hence the `let`) in the create-* functions can be left out: (defn create-freader [fname] (java.io.FileInputStream. fname)) * Note: the trailing dot above is shorthand notation for `new`: (Foo.) <=> (new Foo) * in `eof`: since `line` is either a String or nil you can test it directly: (if line false true), this makes `eof` completely unnecessary, just use (if next-line (recur ...) result) in `read- lines` * you can use `with-open` to automatically close the file when you are done: (with-open [f-reader (create-freader ..) b-reader (create- breader f-reader] ...), then `close-freader` is not necessary, `with- open` will also take care in case of an exception More advanced stuff: Currently you read the whole file into memory. You may want to turn that into a lazy sequence, which just reads lines as necessary. So you can also handle files which do not fit into memory. (defn read-lines [reader] (lazy-seq (when-let [line (.readLine reader)] (cons line (read-lines reader) Note the `when-let`: when .readLine returns nil, the `when` returns also nil. If .readLine returns a String, it will be available to the code in `when-let` block as `line`. However you have to take care not to let the sequence slip out your `with-open`. Because reading lines happens defered, the file will already be closed outside the `with- open` block. To prevent this wrap the call to `read-lines` into a `doall`, this realises the seq, but then the whole file is in memory again. But this is really only an appetizer. Check out some tutorials from the web before wrapping your head around lazy-seq. :) Hope this helps. Sincerely Meikel --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: read txt file
Just an FYI, you can also use the builtin slurp function, which will return the contents of a given file. (slurp "file.txt") Regards, Travis On Aug 21, 4:11 am, DemAS wrote: > Hi all, > > I have created the function that read a txt file. I have a very > little experience in clojure and I will be thankfull for any > correction and ideas. > > This is source code: > > (defn create-freader [filename] > (def f-reader (new java.io.FileReader filename)) > f-reader) > > (defn create-breader [f-reader] > (def b-reader (new java.io.BufferedReader f-reader)) > b-reader) > > (defn close-freader [f-reader] > (. f-reader close)) > > (defn eof [line] > (if (= line nil) > true > false)) > > (defn read-lines [reader] > (loop [result []] > (def next-line (. reader readLine)) > (if (eof next-line) > result > (recur (conj result next-line) > > (defn read-file [filename] > (def f-reader (create-freader filename)) > (def b-reader (create-breader f-reader)) > (def lines (read-lines b-reader)) > (close-freader f-reader) > lines) > > user ==> (read-file "my_file") > > This is source code in pastebin (http://pastebin.com/m477bd86d). > > Thanks in advance. > Andrey. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: replace-subtree for zippers?
On Aug 21, 12:05 pm, Jan Rychter wrote: > think crossover in genetic programming (this is actually what I need > this for). > > --J. Hi, I've written a genetic programming framework in Clojure, it might be of interest if you're looking for some inspiration: http://bitbucket.org/svdm/clojuregp/src/ Though I experimented with using zippers initially, I ended up not using them for the fundamental tree operations like the one discussed here. I found zippers to be quite a bit slower than more low level approaches for traversing trees, which becomes significant in typical GP experiments where things like subtree replacement are used a large number of times. After some experimentation I settled on an approach using an atom to keep track of the current node's index in the tree, while walking it depth-first, rebuilding it as we go. If the index matches the one we want to replace, we cons on the replacement node instead of the old one. This turns out to be pretty fast, but it's a bit of a monstrosity, what with the mutable state, etc. I've been meaning to replace it with something nicer, but haven't found a good alternative yet. Here it is: (defn tree-replace [idx new-node tree] (let [i (atom -1) r-fn (fn do-replace [node] (swap! i inc) (cond (= @i idx) new-node (> @i idx) node (coll? node) (cons (first node) (doall (map do-replace (next node :else node))] (r-fn tree))) Also available here if it gets mangled: http://bitbucket.org/svdm/clojuregp/src/tip/src/cljgp/breeding.clj#cl-111 The index of each node is equal to its index in a tree-seq created as follows: (tree-seq coll? next tree) In action: user> (def tree1 '(- 100 99)) #'user/tree1 user> (def tree2 '(+ (* 1 2) (- 3 4))) #'user/tree2 user> (tree-seq coll? next tree2) ((+ (* 1 2) (- 3 4)) (* 1 2) 1 2 (- 3 4) 3 4) user> (tree-replace 1 tree1 tree2) (+ (- 100 99) (- 3 4)) user> (tree-replace 2 tree1 tree2) (+ (* (- 100 99) 2) (- 3 4)) user> (tree-replace 0 tree1 tree2) (- 100 99) So that's one way of doing it. Not the prettiest, but it does what I need it to. I'm not sure it does exactly what you want though, because: user> (tree-replace 3 '(7 8 (9 10)) '(1 2 (3 4 5) 6)) (1 2 (3 (7 8 (9 10)) 5) 6) Compared to: > should produce => '(1 2 (3 7 8 (9 10) 6)) I'm kind of wondering why you want the replacement subtree (tree2 if you will) to be spliced "flat" into the original tree like that. It seems to me that such an operation would not actually do a subtree replacement in the typical genetic programming sense. I've tried to illustrate what I mean in a little diagram: http://i30.tinypic.com/hsizkh.png Regards, Stefan PS. I hope this ends up in the right place, I'm new to mailing list posting. Apologies in advance if I accidentally break something. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Using a map vs. using a metadata map
I'm trying to make a decision in a parser library between using regular maps as states (containing a sequence of remaining tokens, as well as other info) vs. using metadata maps attached to the sequence of remaining tokens. In other words: {:remainder [\r \e \m], :index 3, :line 5, :column 2} vs. #^{:index 3, :line 5, :column 2} [\r \e \m] The two germane properties I can think of are accessing speed and equality of states in testing. If I do the former, then I can directly alter the index, line, and column. But in my tests, I have to write something like (make-state [\r \e \m] 3 5 2) every time. (That might be a good or bad thing; it's a lot more cluttered, but always explicit about what the expected values of the whole state are.) If I do the latter, then to access or alter the index, line, or column, I have to use 'meta or 'with-meta. Yet my tests would be a lot cleaner, because I wouldn't have to repeat the line or column every time I make a state. What I'm worried about is the speed impact that 'meta and 'with-meta may have. Are their time effects negligible? Which would be "better" Clojure? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: clojure success story ... hopefully :-)
Hi, I read the related story on InfoQ and found it an extremely interesting and motivating read, Clojure being applied in such an interesting field as machine learning! There is something in the article I'd like to understand better, so i'm just asking here on the group: "The way that Rich elected to de-couple destructuring bind from pattern matching was brilliant." Could someone point me to what the difference is? I know pattern matching e.g. from the PLT scheme implementation, and there the pattern matching also provides the binding and destructuring I think...? Excuse me if it's a stupid question, it just made me curious to know :-; Sigrid --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: clojure success story ... hopefully :-)
On Fri, 2009-08-21 at 11:02 -0700, Sigrid wrote: > Hi, > > I read the related story on InfoQ and found it an extremely > interesting and motivating read, Clojure being applied in such an > interesting field as machine learning! > > There is something in the article I'd like to understand better, so > i'm just asking here on the group: > > "The way that Rich elected to de-couple destructuring bind from > pattern matching was brilliant." > > Could someone point me to what the difference is? I know pattern > matching e.g. from the PLT scheme implementation, and there the > pattern matching also provides the binding and destructuring I > think...? > Clojure allows destructuring of vectors, which happens to be what its functions' argument lists are, so you get most of the benefits of pattern matching. It's not full-blown, though, so (correct me if I'm wrong) the equivalent of this is not possible: length [] = 0 length (_:xs) = 1 + (length xs) Regards, -- Michel --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: clojure success story ... hopefully :-)
user=> (defmulti length empty?) #'user/length user=> (defmethod length true [x] 0) # user=> (defmethod length false [x] (+ 1 (length (rest x # user=> (length [1 2 3 4]) 4 On Fri, Aug 21, 2009 at 12:41 PM, Michel Salim wrote: > > On Fri, 2009-08-21 at 11:02 -0700, Sigrid wrote: >> Hi, >> >> I read the related story on InfoQ and found it an extremely >> interesting and motivating read, Clojure being applied in such an >> interesting field as machine learning! >> >> There is something in the article I'd like to understand better, so >> i'm just asking here on the group: >> >> "The way that Rich elected to de-couple destructuring bind from >> pattern matching was brilliant." >> >> Could someone point me to what the difference is? I know pattern >> matching e.g. from the PLT scheme implementation, and there the >> pattern matching also provides the binding and destructuring I >> think...? >> > Clojure allows destructuring of vectors, which happens to be what its > functions' argument lists are, so you get most of the benefits of > pattern matching. It's not full-blown, though, so (correct me if I'm > wrong) the equivalent of this is not possible: > > length [] = 0 > length (_:xs) = 1 + (length xs) > > Regards, > > -- > Michel > > > > > -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: clojure success story ... hopefully :-)
Hi, Am 21.08.2009 um 20:02 schrieb Sigrid: Could someone point me to what the difference is? I know pattern matching e.g. from the PLT scheme implementation, and there the pattern matching also provides the binding and destructuring I think...? The difference is, that in pattern matching you can also specify values on the left side. For example in OCaml: type foo = [ Foo of int ]; value frobnicate x = match x with [ Foo 5 -> do_something () | Foo 7 -> do_something_else () | Foo x -> do_more x ]; (Please bear with me if I don't remember all the details of the syntax.) While this is not possible in Clojure: (let [[x 5 y] [1 2 3]] ...) The five on the left hand side is not allowed. Hope this helps. Sincerely Meikel smime.p7s Description: S/MIME cryptographic signature
Re: clojure success story ... hopefully :-)
On Fri, 2009-08-21 at 12:50 -0700, Kevin Downey wrote: > user=> (defmulti length empty?) > #'user/length > > user=> (defmethod length true [x] 0) > # > > user=> (defmethod length false [x] (+ 1 (length (rest x > # > > user=> (length [1 2 3 4]) > 4 > Très cool! This could be applied to Meikel's post as well -- you *can* write your own predicate function that in effect test for values. It will just be -- ugly. Is there a performance hit with this style (due to using multimethods) or will this be optimized away in practice? -- Michel signature.asc Description: This is a digitally signed message part
Re: clojure success story ... hopefully :-)
On Aug 21, 5:55 pm, Michel Salim wrote: > Is there a performance hit with this style (due to using multimethods) > or will this be optimized away in practice? There is a slight performance penalty over a normal function call. I think the dispatching takes one function call, a hash lookup, and an equality test. -SS --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Continuous Integration Builds for Clojure are Back!
Hrm - I couldn't clone any of the clojure builds as they wer'nt listed, and I see theres no agent that'll build maven2 so my builds just stuck in the queue. I guess the servers only setup to build with ant? -- On Thu, Aug 20, 2009 at 1:13 PM, Howard Lewis Ship wrote: > > Why don't you do the group rename first. > > Bamboo is easy enough to use, I can give you a hand if you need it. > It's open to register and you can clone the existing Clojure or > Clojure Contriub plans to form one for your plugin. > > On Wed, Aug 19, 2009 at 3:24 PM, Mark Derricutt wrote: > > Excellent - any chance: > >http://github.com/talios/clojure-maven-plugin > > could be added as well? I was about to go and see about getting it > deployed > > to oss.sonatype.org but I do like the idea of a 'central clojure > > repository'. > > However, currently I'm using the 'org.clojure' groupid which, giving its > not > > an official clojure thing, I don't really feel comfortable with using if > its > > being pushed to a repository, so was thinking of moving the groupid and > code > > under com.theoryinpractise like my other projects. > > Mark > > -- > > > > On Thu, Aug 20, 2009 at 4:43 AM, Howard Lewis Ship > wrote: > >> > >> There are nightly builds for both projects as well. > > > > > > > > > > > > > -- > Howard M. Lewis Ship > > Creator of Apache Tapestry > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Calculating digits of π (Salamin-Brent)
Took a shot at implementing PI in Clojure using a reasonably fast algorithm. So why is it so slow ? Is BigDecimal just that bad ? Would fixed point arithmetic be better using BigInteger ? (MacBook Pro - Intel Core 2 Duo 2.26 GHz - 4GB RAM) (set! *warn-on-reflection* true) (import 'java.lang.Math) (import 'java.math.MathContext) (import 'java.math.RoundingMode) (import 'java.math.BigInteger) (import 'java.math.BigDecimal) (defn sb-pi [digits] "Calculates PI digits using the Salamin-Brent algorithm and Java's BigDecimal class." (let [mcdigits (new MathContext (+ 10 digits)) ; add 10 guard digits zero (new BigDecimal 0) one (new BigDecimal 1) two (new BigDecimal 2) four (new BigDecimal 4)] (defn big-sqrt[#^BigDecimal num digits] "Calculates square root using Newton's method." (defn big-sqrt-int [#^BigDecimal num #^BigDecimal x0 #^BigDecimal x1 digits] (if (not (. x0 equals x1)) (big-sqrt-int num x1 (. (. x1 add (. num divide x1 digits RoundingMode/ HALF_DOWN)) divide two digits RoundingMode/HALF_DOWN) digits) x1)) (big-sqrt-int num zero (. BigDecimal valueOf (Math/sqrt (. num doubleValue))) (+ 1 digits))) (defn sb-pi-int [#^BigDecimal a #^BigDecimal b #^BigDecimal x #^BigDecimal p n] (let [#^BigDecimal a1 (. (. a add b) divide two mcdigits) #^BigDecimal b1 (big-sqrt (. a multiply b mcdigits) digits) #^BigDecimal x1 (. x subtract (. p multiply (. (. a subtract a1) multiply (. a subtract a1) mcdigits) mcdigits)) #^BigDecimal p1 (. two multiply p mcdigits)] (if (> n digits) (. (. (. (. a1 add b1) multiply (. a1 add b1) mcdigits) divide (. four multiply x1) mcdigits) setScale digits RoundingMode/HALF_UP) (recur a1 b1 x1 p1 (* 2 n) (let [#^BigDecimal sa one #^BigDecimal sb (. one divide (big-sqrt two digits) mcdigits) #^BigDecimal sx (. one divide four) #^BigDecimal sp one] (sb-pi-int sa sb sx sp 1) ))) (time (println (sb-pi 1))) ;; 4.058 ms (time (println (sb-pi 10))) ;; 6.611 ms (time (println (sb-pi 100))) ;; 21.615 ms (time (println (sb-pi 1000)));; 211.314 ms (time (println (sb-pi 1))) ;; 22090.236 ms --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---