Thursday 15 July 2010

How can I recur from a Clojure catch block in a more functional manner? -


I am writing for an IRC bot, I want to continue trying to open a socket, even if someone < Code> UnkownHostException . My first idea was something like that.

  (defn open-socket [{keys: [host port]: as connection]] (try (java.net.Socket. Host port) Java.net.UnknownHostException _ (connection Re: Connection)))   

But this does not work because you can not recurs any cache block, instead I sit on something like this:

  (let [socket] (when (not (not realized) (try socket (socket socket host port)) (hold java.net.UnknownHostException_))) Socket @ socket] ...   

And it works, but it does Ehad is necessary. Can I have a more functional way I can not see it doing so?

Just add a or :

  (open socket defn [{keys: [host port]: as a connection]] (or try (java.net .Socket host port) (Hold java.net, Unknown host exception _ zero)) (Recur connections))    

No comments:

Post a Comment