title: Interface as argument, returning result
body: <p>I am doing the following programming exercise: <a href="https://www.codewars.com/kata/56dbe0e313c2f63be4000b25" rel="nofollow noreferrer">Moves in squared strings (I)</a>. The statement is:</p>
  
  <blockquote>
    <p>You are given a string of n lines, each substring being n characters
    long: For example:</p>
    
    <p>s = "abcd\nefgh\nijkl\nmnop"</p>
    
    <p>We will study some transformations of this square of strings.</p>
  
  <pre><code>Vertical mirror: vert_mirror (or vertMirror or vert-mirror)
  
  vert_mirror(s) =&gt; "dcba\nhgfe\nlkji\nponm"
  
  Horizontal mirror: hor_mirror (or horMirror or hor-mirror)
  
  hor_mirror(s) =&gt; "mnop\nijkl\nefgh\nabcd"
  </code></pre>
    
    <p>or printed:</p>
    
    <p>vertical mirror   |horizontal mirror    abcd --> dcba     |abcd -->
    mnop  efgh     hgfe     |efgh     ijkl  ijkl     lkji     |ijkl<br>
    efgh  mnop     ponm     |mnop     abcd </p>
    
    <h1>Task:</h1>
  
  <pre><code>Write these two functions
  </code></pre>
    
    <p>and</p>
  
  <pre><code>high-order function oper(fct, s) where
      fct is the function of one variable f to apply to the string s (fct will be one of vertMirror, horMirror)
  </code></pre>
    
    <h1>Examples:</h1>
    
    <p>s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) =>
    "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) =>
    "mnop\nijkl\nefgh\nabcd"</p>
  </blockquote>
  
  <p>I would like some help because of it is the first time I need to pass functions as method's arguments. The initial test cases are (taken from the exercise):</p>
  
  <pre><code>import static org.junit.Assert.*;
  import org.junit.Test;
  
  public class OpstringsTest {
  
      private static void testing(String actual, String expected) {
          assertEquals(expected, actual);
      }
      @Test
      public void test() {
          System.out.println("Fixed Tests vertMirror");
          String s = "hSgdHQ\nHnDMao\nClNNxX\niRvxxH\nbqTVvA\nwvSyRu";
          String r = "QHdgSh\noaMDnH\nXxNNlC\nHxxvRi\nAvVTqb\nuRySvw";
          testing(Opstrings.oper(Opstrings::vertMirror, s), r);
          s = "IzOTWE\nkkbeCM\nWuzZxM\nvDddJw\njiJyHF\nPVHfSx";
          r = "EWTOzI\nMCebkk\nMxZzuW\nwJddDv\nFHyJij\nxSfHVP";
          testing(Opstrings.oper(Opstrings::vertMirror, s), r);
  
          System.out.println("Fixed Tests horMirror");
          s = "lVHt\nJVhv\nCSbg\nyeCt";
          r = "yeCt\nCSbg\nJVhv\nlVHt";        
          testing(Opstrings.oper(Opstrings::horMirror, s), r);
          s = "njMK\ndbrZ\nLPKo\ncEYz";
          r = "cEYz\nLPKo\ndbrZ\nnjMK";
          testing(Opstrings.oper(Opstrings::horMirror, s), r);
      }
  }
  </code></pre>
  
  <p>I have tried the following implementation:</p>
  
  <pre><code>import java.util.function.*;
  class Opstrings {    
    public static String vertMirror (String string) {
      String[] words = string.split("\\s+");
      StringBuilder sb = new StringBuilder();
      for(String word : words){
        sb.append(new StringBuilder(word).reverse()+"\n");
      }
      return string.strip().toString();
    }
    public static String horMirror (String string) {
      return new StringBuilder(string).reverse().toString();
    }
    public static String oper/*️*/(Consumer&lt;String&gt; operator, String s) {
      return operator.accept(s);
    }
  }
  </code></pre>
  
  <p>And when we execute the previous code, the compiler outputs:</p>
  
  <blockquote>
    <p>./src/main/java/Opstrings.java:15: error: incompatible types: void
    cannot be converted to String
        return operator.accept(s);
                              ^ 1 error</p>
  </blockquote>
  
  <p>I understand that Consumer is defined to return void. So then I decided to investigate and I read that Callable is able to return results. So then I tried:</p>
  
  <pre><code>import java.util.concurrent.Callable;
  class Opstrings {    
    public static String vertMirror (String string) {
      String[] words = string.split("\\s+");
      StringBuilder sb = new StringBuilder();
      for(String word : words){
        sb.append(new StringBuilder(word).reverse()+"\n");
      }
      return string.strip().toString();
    }
    public static String horMirror (String string) {
      return new StringBuilder(string).reverse().toString();
    }
    public static String oper/*️*/(Callable operator, String s) {
      return operator.accept(s);
    }
  }
  </code></pre>
  
  <p>However this time our compiler says that we can not call the functions without arguments when String is required (from the test cases):</p>
  
  <blockquote>
    <p>./src/test/java/OpstringsTest.java:14: error: incompatible types:
    invalid method reference
            testing(Opstrings.oper(Opstrings::vertMirror, s), r);
                                   ^
        method vertMirror in class Opstrings cannot be applied to given types
          required: String
          found: no arguments
          reason: actual and formal argument lists differ in length ./src/test/java/OpstringsTest.java:17: error: incompatible types:
    invalid method reference</p>
  </blockquote>
  
  <p>In addition, I have also read:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4685563/how-to-pass-a-function-as-a-parameter-in-java">How to pass a function as a parameter in Java?</a></li>
  <li><a href="https://www.geeksforgeeks.org/java-8-consumer-interface-in-java-with-examples/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/java-8-consumer-interface-in-java-with-examples/</a></li>
  <li><a href="https://stackoverflow.com/questions/53676196/returning-method-value-inside-a-consumer-expression">Returning Method value inside a Consumer expression</a></li>
  <li><a href="https://stackoverflow.com/questions/29945627/java-8-lambda-void-argument">Java 8 lambda Void argument</a></li>
  <li><a href="https://www.baeldung.com/java-runnable-callable" rel="nofollow noreferrer">https://www.baeldung.com/java-runnable-callable</a></li>
  <li><a href="https://www.arquitecturajava.com/java-callable-interface-y-su-uso/" rel="nofollow noreferrer">https://www.arquitecturajava.com/java-callable-interface-y-su-uso/</a></li>
  </ul>
  
  <p>Could you help me please figuring out how could we pass in Functional Interfaces as method's arguments and return its results?</p>
  
  <p>EDIT: As @JB Nizet says it can be done using UnaryOperator:</p>
  
  <pre><code>import java.util.function.*;
  
  class Opstrings {    
    public static String vertMirror (String string) {
      String[] words = string.split("\\s+");
      for(int i = 0; i &lt; words.length; i++){
        words[i] = new StringBuilder(words[i]).reverse().toString();
      }
      return String.join("\n",words);
    }
  
    public static String horMirror (String string) {
      String[] words = string.split("\\s+");
      StringBuilder sb = new StringBuilder();
      for(int i = words.length-1; i &gt;= 0; i--){
        sb.append(words[i]+"\n");
      }
      return sb.toString().strip();
    }
  
    public static String oper/*️*/(UnaryOperator&lt;String&gt; operator, String s) {
      return operator.apply(s);
    }
  }
  </code></pre>