groovy
  1. groovy
  2. GROOVY-4322

Support for Closure composition

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.8-beta-1
    • Fix Version/s: 1.8-beta-2
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      1

      Description

      It would be useful to support something like F#'s compose '>>' operator for Closures, e.g.:

      def twice = { a -> a * 2 }
      def inc = { b -> b + 1 }
      def f = inc >> twice
      def g = { x -> twice(inc(x)) } // longhand equivalent
      assert f(10) == 22
      assert g(10) == 22
      
      def string2char = { it.chars[0] }
      def p = Integer.&toHexString >> string2char >> 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]
      

        Activity

        Hide
        Paul King added a comment -

        Proposed patch attached

        Show
        Paul King added a comment - Proposed patch attached
        Paul King made changes -
        Field Original Value New Value
        Attachment groovy4322_ClosureComposition.patch [ 50291 ]
        Hide
        Paul King added a comment -

        Originally I had two variations. The current one:

        f = g >> h
        f = { h(g(it)) } // equivalent
        

        And a "forced spreading" one:

        f = g >>> h
        f = { h(*g(it)) } // equivalent
        

        but now I try to spread automatically if I can. If anyone can see a problem, let me know.

        Show
        Paul King added a comment - Originally I had two variations. The current one: f = g >> h f = { h(g(it)) } // equivalent And a "forced spreading" one: f = g >>> h f = { h(*g(it)) } // equivalent but now I try to spread automatically if I can. If anyone can see a problem, let me know.
        Hide
        Paul King added a comment - - edited

        Revised patch (includes fix for GROOVY-4336 which is required otherwise MixinTest breaks).

        Patch now includes support for forward and reverse composition:

        def times6 = { it * 2 } >> { it * 3 }
        assert times6(3) == 18
        assert { it * 2 } << { it * 3 } << 3 == 18
        
        Show
        Paul King added a comment - - edited Revised patch (includes fix for GROOVY-4336 which is required otherwise MixinTest breaks). Patch now includes support for forward and reverse composition: def times6 = { it * 2 } >> { it * 3 } assert times6(3) == 18 assert { it * 2 } << { it * 3 } << 3 == 18
        Paul King made changes -
        Paul King made changes -
        Attachment groovy4322_ClosureComposition.patch [ 50291 ]
        Hide
        Guillaume Laforge added a comment -

        Really looking forward to it, that's pretty powerful.
        Patch looking good to me after a very quick glance only.

        Show
        Guillaume Laforge added a comment - Really looking forward to it, that's pretty powerful. Patch looking good to me after a very quick glance only.
        Hide
        Paul King added a comment -

        added

        Show
        Paul King added a comment - added
        Paul King made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Paul King
            Reporter:
            Paul King
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: