Hi Daniel,

On Wed, Jun 23, 2010 at 1:15 PM, Daniel Werner <
daniel.d.wer...@googlemail.com> wrote:

>
> Judging from previous discussions on this list, I get the feeling that
> relying too much on the Java implementation details underlying the
> public Clojure APIs is discouraged. This certainly makes sense
> considering that apparently one of the future plans (no pun intended)
> is to give the CLR and JavaScript ports of the language more focus.
>
> I don't know much about the CLR version of Clojure but can understand
that.  My goal was just trying eliminate the need look at the source code to
know about the timeout feature of the Futures.  Having the below function
solves that problem.


> IMHO this seems like a useful improvement. Maybe your proposal would
> draw more attention if you backed it up with a patch -- even a
> preliminary one? :-)
>
>
What I was thinking for a future-await function was:

(defn- convert-time-unit [unit]
    (case unit
          :nanoseconds (java.util.concurrent.TimeUnit/NANOSECONDS)
          :microseconds (java.util.concurrent.TimeUnit/MICROSECONDS)
          :milliseconds (java.util.concurrent.TimeUnit/MILLISECONDS)
          :seconds (java.util.concurrent.TimeUnit/SECONDS)
          :minutes (java.util.concurrent.TimeUnit/MINUTES)
          :hours (java.util.concurrent.TimeUnit/HOURS)
          :days (java.util.concurrent.TimeUnit/DAYS)
          (throw (IllegalArgumentException. (str unit " is not a valid unit
of time")))))

(defn future-await
    "Returns the value of the future just like a deref.  If the future has
not completed by timeout, nil is returned"
      ([^java.util.concurrent.Future f timeout-in-millis]
         (future-await f timeout-in-millis :milliseconds))
      ([^java.util.concurrent.Future f timeout unit]
           (try
             (->> unit
                     convert-time-unit
                     (.get f timeout))
            (catch java.util.concurrent.TimeoutException e
              nil))))

It's usage is below

(def fut
       (future
        (Thread/sleep 1000000)
        "done"))
(future-await fut 1000) ;throws exception
(future-await fut 2 :seconds) ;throws exception

(def fut2
       (future
        (Thread/sleep 10000)
        "done"))
(future-await fut2 2 :minutes) ; => "done"


 -Ryan

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