How to make closure have its own copy of variable -does groovy have lambda
<p>What is the command in groovy to ensure a class variable is unaffected by a closure? As we know a closure captures the environment that its wrapped in. So, if I have a closure that increments an integer class variable for example, then that variable is changed for the class. What I want is for a closure to have its own copy of  a variable so it does not affect the class variable. Is it possible? In Objective C for example we would use the __Block command and that would make the block be able to change the value of a captured variable. Now, I am asking for the opposite of __Block in my case as closures are already changing the variables they are scoped in. </p>

<p>Lets take a look at a clear example of what I want:</p>

<pre><code>def class myCoolClass {
    def x=1

    def myMethodThatReturnsClosure(){
        //lets return a closure who's scope will include the x=1
        myClosure
    }

    def showMeXFromMyCoolClass(){
        println "this is x from myCoolClass:$x"
    }

    def myClosure={
        println "im printing x:$x"
        //lets change x now from within closure
        x++
    }

}


def x = new myCoolClass();

def c=x.myMethodThatReturnsClosure();
c(); //we are changing x ...x = 1
c(); //we are changing x again  ...x = 2
c(); //we are changing x again ...x = 3

x.showMeXFromMyCoolClass(); //...x = 4
//i dont want x to be 4 in the last call, i want x to be 1. 
//i want it unchanged. how to tell closure to take its own copy
</code></pre>

<p>I realize in groovy a closure knows about its environment. It "closes out" around the function that its wrapped in. Is there lambda in groovy then? I think lambda would not know about its environment right? But lets say I want only want variable to not know about its environment and others should, then lambda would not be good.  </p>