我需要一个函数,它接收一个可以在内部调用的函数作为参数。

huangapple go评论65阅读模式
英文:

I need a function that it receive a function by parameter that can be called inside

问题

我想为在间隔内接收的任何函数执行的功能创建一个函数。
为了更好地解释,我将给您一个示例。

我想要使用此函数来调用REST客户端,但是,不是使用包含10个元素的列表进行调用,而是要使用2个元素进行5次调用。

我的问题是我不想在所有需要按间隔调用的方法中管理循环和间隔,我想做类似这样的事情:Util.executeInIntervals(2(间隔),this.restClient.doPost(fullList))。此方法应按照以下所示的间隔调用此restClient。

我已经能够创建这个示例类,但结果不正确:

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class FunctionAsParameter {
  
  public static <T> void executeInIntervals(List<T> list, int interval, Function<T, String> function) {
    // 定义间隔的数量
    final int INTERVAL = interval;

    int cont = 0;
    while (list.size() - cont > INTERVAL) {
      function.apply(list.subList(cont, cont + INTERVAL).toString());
      cont = cont + INTERVAL;
    }
    // 处理剩余部分
    function.apply(list.subList(cont, list.size()));
  }
}

class Main {
  public static void main(String[] args) {

    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    
    FunctionAsParameter.executeInIntervals(list, 2, value -> {
      // 这里将调用REST客户端,使用子列表而不是完整列表
      System.out.println(value);
      // 这个我想要移除,但Java指示函数不能是void
      return "";
    });
  }
}

结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

正确的结果应该是:

[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]

提前感谢。

英文:

I want to do a function for execute any function received by parameter in intervals.
To explain myself better, I will give you an example.

I want to use this function for make calls to a rest client but, instead of do the call with a list with 10 elements, I want to can do 5 calls with 2 elements.

My problem is I don't want have to manage loops and intervals in all methods that I want call in intervals, I would like do something like this: Util.executeInIntervals (2 (interval), this.restClient.doPost(fullList) ); This method should do calls to this restClient in intervals as indicated below.

I have been able to this example class but the result isn't the correct:

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class FunctionAsParameter {
  
  public static &lt;T&gt; void executeInIntervals(List&lt;T&gt; list, int interval, Function function) {
    // It&#39;s defined the num of interval
    final int INTERVAL = interval;

    int cont = 0;
    while(list.size() - cont &gt; INTERVAL) {
      function.apply(list.subList(cont,cont + INTERVAL).toString());
      cont = cont + INTERVAL;
    }
    // The rest are processed
    function.apply(list.subList(cont,list.size()));
  }
}
class Main {
  public static void main(String[] args) {

    List&lt;Integer&gt; list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
    
    FunctionAsParameter.executeInIntervals(list, 2, value -&gt; {
      // Here it would be the call to rest client with the sublist, not the full list 
      System.out.println(list);
      // This I would like remove but java indicates that the function can&#39;t be void
      return &quot;&quot;;
    });
  }
}

Result:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Correct result would be:

[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]

Thanks for advance.

答案1

得分: 1

您的回调函数忽略了您传递给它的参数,而且不清楚参数应该是什么(您一次使用String,一次使用List<T>)。

您可以尝试像这样修复您的代码:

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class FunctionAsParameter {
  
  public static <T> void executeInIntervals(List<T> list, int interval, Function<List<T>, String> function) {
    // 定义了间隔的数量
    final int INTERVAL = interval;

    int cont = 0;
    while (list.size() - cont > INTERVAL) {
      function.apply(list.subList(cont, cont + INTERVAL));
      cont = cont + INTERVAL;
    }
    // 剩下的部分被处理
    function.apply(list.subList(cont, list.size()));
  }
}

class Main {
  public static void main(String[] args) {

    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

    FunctionAsParameter.executeInIntervals(list, 2, value -> {
      System.out.println(value);
      return "";
    });
  }
}

如果您想要删除return "";(因为您无论如何不使用该函数的返回值),那么Function是错误的接口 - 您应该使用Consumer

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerAsParameter {

    public static <T> void executeInIntervals(List<T> list, int interval, Consumer<List<T>> consumer) {
        // 定义了间隔的数量
        final int INTERVAL = interval;

        int cont = 0;
        while (list.size() - cont > INTERVAL) {
            consumer.accept(list.subList(cont, cont + INTERVAL));
            cont = cont + INTERVAL;
        }
        // 剩下的部分被处理
        consumer.accept(list.subList(cont, list.size()));
    }
}

class Main {
    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        ConsumerAsParameter.executeInIntervals(list, 2, value -> {
            System.out.println(value);
        });
    }
}

请注意,在这个简单的示例中,您甚至可以进一步替换executeInIntervals()的调用:

ConsumerAsParameter.executeInIntervals(list, 2, System.out::println);

对于您在评论中提到的示例,代码可能如下所示:

ConsumerAsParameter.executeInIntervals(fullList, 2, this.restClient::doPost);
英文:

Your callback ignores the parameter you pass into it and it is also unclear what the parameter should be (you call it once with a String and once with a List&lt;T&gt;).

You could try to fix your code like this:

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class FunctionAsParameter {
  
  public static &lt;T&gt; void executeInIntervals(List&lt;T&gt; list, int interval, Function&lt;List&lt;T&gt;, String&gt; function) {
    // It&#39;s defined the num of interval
    final int INTERVAL = interval;

    int cont = 0;
    while (list.size() - cont &gt; INTERVAL) {
      function.apply(list.subList(cont, cont + INTERVAL));
      cont = cont + INTERVAL;
    }
    // The rest are processed
    function.apply(list.subList(cont, list.size()));
  }
}

class Main {
  public static void main(String[] args) {

    List&lt;Integer&gt; list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
    
    FunctionAsParameter.executeInIntervals(list, 2, value -&gt; {
      System.out.println(value);
      return &quot;&quot;;
    });
  }
}

If you want to remove that return &quot;&quot;; (because you don't use the return value of that function anyway) then Function is the wrong interface - you should use a Consumer:

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerAsParameter {

    public static &lt;T&gt; void executeInIntervals(List&lt;T&gt; list, int interval, Consumer&lt;List&lt;T&gt;&gt; consumer) {
        // It&#39;s defined the num of interval
        final int INTERVAL = interval;

        int cont = 0;
        while (list.size() - cont &gt; INTERVAL) {
            consumer.accept(list.subList(cont, cont + INTERVAL));
            cont = cont + INTERVAL;
        }
        // The rest are processed
        consumer.accept(list.subList(cont, list.size()));
    }
}

class Main {
    public static void main(String[] args) {

        List&lt;Integer&gt; list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);

        ConsumerAsParameter.executeInIntervals(list, 2, value -&gt; {
            System.out.println(value);
        });
    }
}

Note that in this simple example you could replace the executeInIntervals() call even further:

ConsumerAsParameter.executeInIntervals(list, 2, System.out::println);

For the example that you mention in a comment, the code could look like this:

ConsumerAsParameter.executeInIntervals(fullList, 2, this.restClient::doPost));

huangapple
  • 本文由 发表于 2023年3月7日 23:17:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定