EmilSverige | 1 Apr 2011 10:41
Picon

[groovy-user] How to uncurry a closure

I have a curried closure, but want to change the parameter it is curried
with.
I have built a simple test to illustrate what I am trying to achieve:

class UncurryTests extends GroovyTestCase{
    void test_uncurry() {
        // First, create a closure and curry it
        def closure = {int param -> param * 2}
        def curriedClosure = closure.curry(10)

        assertEquals( 20, curriedClosure() )

        // Then, uncurry and call the closure with another parameter
        def uncurriedClosure = curriedClosure.uncurry()
        assertEquals( 200, uncurriedClosure( 100 ) )
    }
}

This doesn't work since there is no "uncurry" method on CurriedClosure. 
Is there a way to do this?

--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-uncurry-a-closure-tp4274956p4274956.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

(Continue reading)

Hamlet DArcy | 1 Apr 2011 10:50
Favicon

Re: [groovy-user] How to uncurry a closure

This is horrible, but it works: 

import org.codehaus.groovy.runtime.CurriedClosure;

def x = { it }
CurriedClosure y = x.curry('hi')
assert 'hi' == y()
y. <at> curriedParams[0] = 'Hello'
assert 'Hello' == y()

A curried closure is of type CurriedClosure. 

It has a field named curriedParams of type Object[], but it is private and should not be accessed. Here you
see I am throwing caution to the wind and accessing it anyway. If you use this then test it very well because
upgrading your Groovy version could break it... you are depending on a private implementation. 

----- Original Message -----
> I have a curried closure, but want to change the parameter it is
> curried
> with.
> I have built a simple test to illustrate what I am trying to achieve:
> 
> class UncurryTests extends GroovyTestCase{
>     void test_uncurry() {
>         // First, create a closure and curry it
>         def closure = {int param -> param * 2}
>         def curriedClosure = closure.curry(10)
> 
>         assertEquals( 20, curriedClosure() )
> 
(Continue reading)

Jochen Theodorou | 1 Apr 2011 12:22
Picon
Gravatar

Re: [groovy-user] How to uncurry a closure

Am 01.04.2011 10:41, schrieb EmilSverige:
> I have a curried closure, but want to change the parameter it is curried
> with.
> I have built a simple test to illustrate what I am trying to achieve:
>
> class UncurryTests extends GroovyTestCase{
>      void test_uncurry() {
>          // First, create a closure and curry it
>          def closure = {int param ->  param * 2}
>          def curriedClosure = closure.curry(10)
>
>          assertEquals( 20, curriedClosure() )
>
>          // Then, uncurry and call the closure with another parameter
>          def uncurriedClosure = curriedClosure.uncurry()
>          assertEquals( 200, uncurriedClosure( 100 ) )
>      }
> }
>
> This doesn't work since there is no "uncurry" method on CurriedClosure.
> Is there a way to do this?

I have not checked back, but isn't the owner of the curried Closure the 
uncurried one?

def uncurriedClosure = curriedClosure.getOwner()

bye blackdrag

--

-- 
(Continue reading)

EmilSverige | 1 Apr 2011 12:52
Picon

[groovy-user] Re: How to uncurry a closure

Yes Jochen, you're right.

If I use owner I get the uncurried closure:

    void test_uncurry() {
        def closure = {int param -> param * 2}
        def curriedClosure = closure.curry(10)
        assertEquals(20, curriedClosure())

        def unCurriedClosure = curriedClosure.owner

        assertEquals(200, unCurriedClosure(100))
    }

The test now succeeds


2011/4/1 Jochen Theodorou [via Groovy]
<[hidden email]>:


> Am 01.04.2011 10:41, schrieb EmilSverige:
>> I have a curried closure, but want to change the parameter it is curried
>> with.
>> I have built a simple test to illustrate what I am trying to achieve:
>>
>> class UncurryTests extends GroovyTestCase{
>>      void test_uncurry() {
>>          // First, create a closure and curry it
>>          def closure = {int param ->  param * 2}
>>          def curriedClosure = closure.curry(10)
>>
>>          assertEquals( 20, curriedClosure() )
>>
>>          // Then, uncurry and call the closure with another parameter
>>          def uncurriedClosure = curriedClosure.uncurry()
>>          assertEquals( 200, uncurriedClosure( 100 ) )
>>      }
>> }
>>
>> This doesn't work since there is no "uncurry" method on CurriedClosure.
>> Is there a way to do this?
> I have not checked back, but isn't the owner of the curried Closure the
> uncurried one?
>
> def uncurriedClosure = curriedClosure.getOwner()
>
> bye blackdrag
>
> --
> Jochen "blackdrag" Theodorou
> The Groovy Project Tech Lead
> http://blackdragsview.blogspot.com/
> For Groovy programming sources visit http://groovy.codehaus.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>
>
> ________________________________
> If you reply to this email, your message will be added to the discussion
> below:
> http://groovy.329449.n5.nabble.com/How-to-uncurry-a-closure-tp4274956p4275091.html
> To unsubscribe from How to uncurry a closure, click here.

View this message in context: Re: How to uncurry a closure
Sent from the groovy - user mailing list archive at Nabble.com.

Gmane