It is often a pattern that I wish to poll a file for changes (when it was last written). When the file does change from its previous value, I wish to execute some function. Something of the form.
(poll-for-changes file-str on-change-fx current-value)
where
file-stris just a string that specifies the files locationon-change-fxis the function that should be called when the file atfile-strchanges. Let us say that theon-change-fxshould take the File object pointing tofile-stras a argument.current-valuethe current value of the file in milliseconds. You might set to 0 to guarantee that this function will run at least once, or to the actual value to only run this function when you actually detect a change.
I would just like this function implemented in the clearest, most concise, Clojurist way possible. Thank you.
If you’re looking to poll a directory or files and act on it, I think
watchtoweris pretty good to look at.Java 7 has a WatchService, which uses file system events to react to changes. In this case, you don’t poll at all, but block on a future file event. I don’t think there are any projects in Clojure that are out there leveraging that, although I spent some time toying with it to write a small library. The source for it is here
I don’t claim my library is even complete, but it does use the Java 7 service, so you could use that for inspiration on your own project.