Hello,

Try with this, which is closer to the java version:

; in a file scan/core.clj
(ns scan.core
  (:import java.util.Scanner)
  (:gen-class))

(defn -main []
  (let [sc (Scanner. System/in)]
    (loop [num (int (.nextInt sc))]
      (when-not (= 42 num)
        (println num)
        (recur (.nextInt sc))))))

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