Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4322

Support for Closure composition

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.8-beta-1
    • 1.8-beta-2
    • None
    • 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]
      

      Attachments

        Activity

          People

            paulk Paul King
            paulk Paul King
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: