Class ComposedClosure<V>

java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.lang.Closure<V>
org.codehaus.groovy.runtime.ComposedClosure<V>
All Implemented Interfaces:
GroovyCallable<V>, GroovyObject, Serializable, Cloneable, Runnable, Callable<V>

public final class ComposedClosure<V> extends Closure<V>
A wrapper for Closure to support composition. Normally used only internally through the rightShift() and leftShift() methods on Closure.

Typical usages:

 def twice = { a -> a * 2 }
 def inc = { b -> b + 1 }
 def f = { x -> twice(inc(x)) } // longhand
 def g = inc >> twice
 def h = twice << inc
 assert f(10) == 22
 assert g(10) == 22
 assert h(10) == 22

 def s2c = { it.chars[0] }
 def p = Integer.&toHexString >> s2c >> Character.&toUpperCase
 assert p(15) == 'F'

 def multiply = { a, b -> a * b }
 def identity = { a -> [a, a] }
 def sq = identity >> multiply
 assert (1..5).collect{ sq(it) } == [1, 4, 9, 16, 25]

 def add3 = { a, b, c -> a + b + c }
 def add2plus10 = add3.curry(10)
 def multBoth = { a, b, c -> [a*c, b*c] }
 def twiceBoth = multBoth.rcurry(2)
 def twiceBothPlus10 = twiceBoth >> add2plus10
 assert twiceBothPlus10(5, 10) == 40
 
See Also:
  • Constructor Details

    • ComposedClosure

      public ComposedClosure(Closure first, Closure<V> second)
      Constructs a composed closure by chaining two closures. The first closure's output becomes the second closure's input.
      Parameters:
      first - the first closure to apply
      second - the second closure to apply to the result of the first
  • Method Details

    • setDelegate

      public void setDelegate(Object delegate)
      Description copied from class: Closure
      Allows the delegate to be changed such as when performing markup building
      Overrides:
      setDelegate in class Closure<V>
      Parameters:
      delegate - the new delegate
    • getDelegate

      public Object getDelegate()
      Overrides:
      getDelegate in class Closure<V>
      Returns:
      the delegate Object to which method calls will go which is typically the outer class when the closure is constructed
    • setResolveStrategy

      public void setResolveStrategy(int resolveStrategy)
      Description copied from class: Closure
      Sets the strategy which the closure uses to resolve property references and methods. The default is Closure.OWNER_FIRST
      Overrides:
      setResolveStrategy in class Closure<V>
      Parameters:
      resolveStrategy - The resolve strategy to set
      See Also:
    • getResolveStrategy

      public int getResolveStrategy()
      Description copied from class: Closure
      Gets the strategy which the closure uses to resolve methods and properties
      Overrides:
      getResolveStrategy in class Closure<V>
      Returns:
      The resolve strategy
      See Also:
    • clone

      public Object clone()
      Description copied from class: Closure
      Overrides:
      clone in class Closure<V>
    • getParameterTypes

      public Class[] getParameterTypes()
      Overrides:
      getParameterTypes in class Closure<V>
      Returns:
      the parameter types of the longest doCall method of this closure
    • doCall

      public Object doCall(Object... args)
    • call

      public V call(Object... args)
      Description copied from class: Closure
      Invokes the closure with given argument(s), returning any value if applicable.
      Overrides:
      call in class Closure<V>
      Returns:
      The value if applicable or null if there is no return statement in the closure.
    • additionalReferences

      protected Object[] additionalReferences()
      A composed closure calls through first and then second, so those are recursion edges in addition to the three the cycle check walks by default.
      Overrides:
      additionalReferences in class Closure<V>
      Returns:
      the additional closures reached when this one is invoked; never null