You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Matt Whipple (JIRA)" <ji...@apache.org> on 2015/08/25 15:23:46 UTC

[jira] [Comment Edited] (GROOVY-7556) Use better example for Closure composition

    [ https://issues.apache.org/jira/browse/GROOVY-7556?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14711248#comment-14711248 ] 

Matt Whipple edited comment on GROOVY-7556 at 8/25/15 1:23 PM:
---------------------------------------------------------------

Sure, I could probably submit a PR also in the next several days if that would be easier.  A basic example based on the existing example would be:

{code}
//Forward
 def twice = { a -> a * 2 }
 def add3 = { a -> a + 3 }
 def transform = twice >> add3
 // equivalent: transform = { a -> add3(twice(a)) }
 assert transform(3) == 9

//Reverse
 def twice = { a -> a * 2 }
 def add3 = { a -> a + 3 }
 def transform = twice << add3
 // equivalent: transform = { a -> twice(add3(a)) }
 assert transform(3) == 12
{code}

I'd actually missed the fact that the arguments were also swapped between the two examples so the same could be done here but in the existing example it wouldn't have an impact either way.


was (Author: mwhipple):
Sure, I could probably submit a PR also in the next several days if that would be easier.  A basic example based on the existing code would be:

{code}
//Forward
 def twice = { a -> a * 2 }
 def add3 = { a -> a + 3 }
 def transform = twice >> add3
 // equivalent: transform = { a -> add3(twice(a)) }
 assert transform(3) == 9

//Reverse
 def twice = { a -> a * 2 }
 def add3 = { a -> a + 3 }
 def transform = twice << add3
 // equivalent: transform = { a -> twice(add3(a)) }
 assert transform(3) == 12
{code}

I'd actually missed the fact that the arguments were also swapped between the two examples so the same could be done here but in the existing example it wouldn't have an impact either way.

> Use better example for Closure composition
> ------------------------------------------
>
>                 Key: GROOVY-7556
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7556
>             Project: Groovy
>          Issue Type: Documentation
>            Reporter: Matt Whipple
>            Priority: Minor
>
> The present examples in http://docs.groovy-lang.org/latest/html/api/groovy/lang/Closure.html of using solely multiplication for #leftShift and #rightShift isn't particularly illuminating about the differences between forward and reverse composition. Using different operators or something not associative/commutative so that the results differ could help. 



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)