Reading configuration files is a good example of a run-once function.  It's
effectively self-memoizing -- it should run once, cache its result, and on
future calls just return the cached value.  I could pull in or to do that, but
adding an extra module to the project just for one or two functions feels
pretty heavy. (Also, memoizing is overkill if the functions are thunks.)
Is there a simple way to do this in pure Racket?

In Perl I would do something like this (for simplicity I'm ignoring
encoding and assuming that the config file is JSON):

use JSON;
sub read_conf {
  state $conf = do {
    local $/;
    open my $fh, "<", "db.conf" or die "failed to open config: $!";
    from_json( <$fh> )

I could do this in Racket using set!, but that's not very Rackety:

(require json)
(define conf #f)
(define (read-conf)
   (or conf
           (set! conf (with-input-from-file "db.conf" (thunk (read-json))))

I could do it with a parameter but that's only sweeping the above ugliness
under the rug:

(define conf (make-parameter #f))
(define (read-conf)
   (or (conf)
           (conf (with-input-from-file "db.conf" (thunk (read-json))))

What is the right way?

You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
For more options, visit

Reply via email to