BerlinBrown a écrit : > > On Jan 16, 12:47 pm, Christophe Grand <christo...@cgrand.net> wrote: > >> BerlinBrown a écrit : >> >> >>> Thanks all, this is what I ended up with: >>> For noobs like myself, I am using this code to monitor files and >>> refresh it when the file gets modified. I used 'ref' and 'agent'. >>> >>> (def file-monitor-agent (agent false)) >>> (def file-state (ref {:open-state false})) >>> (defn get-file-state [] (@file-state :open-state)) >>> (defn set-file-state [state] (dosync (commute file-state assoc :open- >>> state state))) >>> >>> (defn file-monitor-loop [] >>> (let [delay-t (prop-int resources-core-sys >>> "Octane_Sys_filemonitor_delay") >>> enable-file-mon (prop-bool resources-win-opts >>> "file_monitor_enabled")] >>> (send file-monitor-agent >>> (fn [_] >>> (when enable-file-mon >>> (loop [] >>> (when (not (. shell (isDisposed))) >>> (. Thread sleep delay-t) >>> (println "Woot!!!") >>> (recur)))))))) >>> >> Since the action you send to your agent doesn't end you should send it >> using send-off ('send-off will give your action its own thread while >> 'send use a fixed thread pool). >> But, given that you don't care about the agent value, it would be better >> to simply use a thread (clojure's fns are Runnables): >> >> (defn file-monitor-loop [] >> (let [delay-t (prop-int resources-core-sys >> "Octane_Sys_filemonitor_delay") >> enable-file-mon (prop-bool resources-win-opts >> "file_monitor_enabled")] >> (when enable-file-mon >> (.start (Thread. >> #(while (not (.isDisposed shell)) >> (Thread/sleep delay-t) >> (println "Woot!!!"))))))) >> >> Christophe >> > > What is the '#' in this example. > It denotes an anonymous function literal.
Anonymous function literal (#()) #(...) => (fn [args] (...)) where args are determined by the presence of argument literals taking the form %, %n or %&. % is a synonym for %1, %n designates the nth arg (1-based), and %& designates a rest arg. This is not a replacement for /*fn*/ - idiomatic used would be for very short one-off mapping/filter fns and the like. #() forms cannot be nested. see http://clojure.org/reader Christophe --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---