On 28.05.2009, at 21:23, CuppoJava wrote:

> eg.
> (with_file "myfile.txt"
>   (write "asdf")
>   (close))
>
> compared to.
> (with_file "myfile.txt"
>   (fn []
>     (write "asdf")
>     (close)))


For that kind of application, you might also want to use the state  
monad (see http://onclojure.com/2009/03/23/a-monad-tutorial-for- 
clojure-programmers-part-3/). You'd have your function

        (defn with-file [filename file-action] ...)

whose file-action argument is a state-monad value, i.e. a function  
that takes the opened file as its argument and returns a result plus  
the same file object.

The advantage of using the state monad is that you can compose the  
basic file actions (read, write, ...) into composite file actions.  
You could for example define a file action that reads a file in a  
specific format and returns the contents in a suitable data  
structure. Moreover, having the file object hidden in the state of  
state monad functions makes it very difficult to use the file  
incorrectly (i.e. assigning it to a var for later use).

Konrad.


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