You might be interested in micro-code-revue (not arguing about the
design, but simpler details) as well:

(apply f [x]) can be replaced by (f x)

(not= x nil) can be (almost) replaced by x if you know x cannot be the
value 'false' (sufficient in your case)

(. scanner nextInt) is a somewhat deprecated form. Write it (.nextInt scanner)

(fn [x] (println x)) is just println => you can pass println function "as is"

Try hard avoiding direct recursive function calls like (def f [...]
... (f ...))) => this fills the stack, and the JVM has a stack limited
in size.
Prefer the use of recur (when possible ! => it is in your case)

Side note.
If you were not in a case where you're seeking for the max perf., you
would probably address the problem differently : decompose the problem
in multisteps.

For example, here, you could have a function whose purpose is to
create a sequence of vals from the input:

(defn incoming-ints [in]
  (let [sc (java.util.Scanner. in)]
    (repeatedly #(.nextInt sc))))

Then you would walk over this sequence while its value is different from 42 :

(doseq [i (incoming-ints System/in) :while (not= i 42)]
  (println i))

HTH,

-- 
Laurent

2011/4/19 Michael Golovanov <mike.golova...@gmail.com>:
> Hi everyone
>
> I have the same task implementation on Java and Clojure. Task is very
> simple: User input integers to the console. Program need print inputed
> integers until user input is 42.
> Clojure implementation is 10 times slower, how to optimize Clojure
> implementation performance?
>
> Java impl:
>
> import java.util.Scanner;
>
>    public class Main {
>      public static void main(String[] args) {
>        Scanner sc = new Scanner(System.in);
>        do {
>          int num = sc.nextInt();
>          if (num == 42)
>            break;
>         System.out.println(num);
>       } while( true );
>    }
>  }
>
> Clojure impl:
>
> (import java.util.Scanner)
>
> (defn getNextInt [scanner term f x]
>  (if (not= x nil)
>    (apply f [x]))
>
>  (let [val (. scanner nextInt)]
>    (if (not= val term)
>      (getNextInt scanner term f val))))
>
> (getNextInt (Scanner. System/in) 42 (fn [x] (println x)) nil)
>
> Thanx
>
> --
> 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

-- 
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