Groovy web console

subscribe to the feed Subscribe
to this
site
Say It Once (via #groovywebconsole)
tweet this snippet Tweet
this
script

Say It Once

Published 1 month ago by Anonymous with tags closure
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def script_scope = new HashMap(); 

def doOnce = {keyword, action -> 
    if (!script_scope.containsKey(keyword)) { 
        synchronized(script_scope) { 
            if (!script_scope.containsKey(keyword)) { 
                script_scope.put(keyword, action()); 
            } 
        }
    }
    return script_scope.get(keyword);
};

def sayItOnce = { what -> 
    doOnce what, {
        System.err.println(what); 
        return what; 
    }
};


sayItOnce "hello"
sayItOnce "hello"
sayItOnce "hello"
sayItOnce "goodbye"
sayItOnce "goodbye"
sayItOnce "hello"