Details

    • Type: New Feature New Feature
    • Status: 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

      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

        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        10d 7h 7m 1 Paul King 03/Aug/10 09:44
        Resolved Resolved Closed Closed
        134d 13h 35m 1 Paul King 15/Dec/10 22:19
        Mark Thomas made changes -
        Reporter Paul King [ paulk@asert.com.au ] Paul King [ paulk ]
        Mark Thomas made changes -
        Workflow jira [ 12970815 ] Default workflow, editable Closed status [ 12978587 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12733264 ] Default workflow, editable Closed status [ 12744997 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Paul King made changes -
        Resolution Fixed [ 1 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Hide
        Paul King added a comment -

        added

        Show
        Paul King added a comment - added
        Hide
        Guillaume Delcroix 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 Delcroix added a comment - Really looking forward to it, that's pretty powerful. Patch looking good to me after a very quick glance only.
        Paul King made changes -
        Attachment groovy4322_ClosureComposition.patch [ 50291 ]
        Paul King made changes -
        Attachment groovy4322_ClosureComposition_B.patch [ 50434 ]
        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
        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.
        Paul King made changes -
        Field Original Value New Value
        Attachment groovy4322_ClosureComposition.patch [ 50291 ]
        Hide
        Paul King added a comment -

        Proposed patch attached

        Show
        Paul King added a comment - Proposed patch attached
        paulk@asert.com.au created issue -

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development