On Nov 4, 2012, at 1:10 PM, Stuart Sierra wrote:

> Hello,
> 
> It is not really possible to make a direct translation from Java to Clojure. 
> Java has mutable variables and imperative flow-control, for which there is no 
> equivalent in Clojure.
> 
> -S

First, some suggestions that are not recommended, because they are fraught with 
perils if you go that way.   I mention them in case for some reason you like 
making your life difficult, or really want to be ultra-careful to use these 
correctly in multi-threaded programs.  They can be useful if you have some 
inner loop that needs screaming performance tweaks and for some reason you want 
written in Clojure, not Java, e.g. you are writing toy programs for benchmarks 
like I've spent too much time on:

    http://shootout.alioth.debian.org/u64q/compare.php?lang=clojure

There is with-local-vars that can help you write more imperative style code if 
you really really want to, but it is very rarely if ever used in Clojure code I 
have seen and written.  Short example:

    http://clojuredocs.org/clojure_core/clojure.core/with-local-vars

As far as I know, with-local-vars actually doesn't introduce problems with 
multi-threaded programs, because the variables it creates can only be modified 
from a single thread, textually within the boundary of the with-local-vars in 
your program.  I could be wrong about that, though, if you were devious enough 
to pass a function that did set-var in a future or something like that.

There is also deftype which permits you to create mutable fields in an object, 
again with big warning bells that if you ever use such objects from more than 
one thread and don't know what you are doing, you are likely introducing subtle 
hard-to-find bugs.

It isn't too difficult to use mutable Java arrays from within Clojure.


I agree with the other responders, Vladimir, that the two methods that will 
give you the most satisfying final result would be:

(1) Call already-written Java code from Clojure, while being as careful as you 
would in pure Java code of mutable state and side effects of the calls you are 
making

or

(2) Rewrite the code by hand, starting from what the functionality is that you 
want to achieve, and writing it in a more Clojure-like style.  Writing code in 
a more functional style tends to make global changes to the structure of the 
code, not just local method-by-method or function-by-function changes.

This takes much more time, and for programs with any complexity at all you'd 
have to do lots of re-testing to ensure your Clojure implementation was 
correct, but the end result should be more maintainable.

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

Reply via email to