composite functions

E.Silber
2006-07-27
2013-04-19
  • E.Silber

    E.Silber - 2006-07-27

    didn't need much RTFM to figure out how to
    map println onto a list , but haven't yet
    figured out how to map a composite function onto
    a list.  Can someone show how to do the equivalent
    of this perl code?:

      map
      {
          print g($_), "\n";
      } @list

    I'm still quite new to functionalj and have a lot
    more to digest, looks like the
    "verbosity-savings" may get lost when having
    to modify functions with addParameter() etc. ????

     
    • Fred Daoud

      Fred Daoud - 2006-07-27

      Hi,

      Here is an example of using a composed function:

      import info.javelot.functionalj.*;
      import java.util.*;

      public class Example
      {
          public static void main(String[] p_args)
          {
              List list = Arrays.asList(1, 2, 3, 4);

              Function f = new InstanceFunction(System.out, "println");
              Function g = new StaticFunction(Example.class, "addFive");

              Function h = new ComposedFunction(f, g);
              Functions.map(h, list);
          }

          public static int addFive(int p_int)
          {
              return p_int + 5;
          }
      }

      This will print out
      6
      7
      8
      9

      As for using addParameter(), you can also specify the parameters to the function at construction time instead.

      Hope this helps,
      Frederic

       

Log in to post a comment.