1

Hi everyone How can I use more than 3 arguments in a Bifunction? So I will use both a label and a project

 public Flux<TaskDto> findAll() {
        Flux<Task> taskFlux = tasksRepository.findAll();
        Flux<Due> dueFlux = dueRepository.findAll();
        Flux<Project> projectFlux = projectRepository.findAll();
     return Flux.zip(taskFlux,dueFlux,taskDueTaskDtoBiFunction);
 }

private final BiFunction<Task,Due,TaskDto> taskDueTaskDtoBiFunction = (x1, x2) -> TaskDto.builder()
        .id(x1.getId())
        .url(x1.getUrl())
        .content(x1.getUrl())
        .orderdata(x1.getOrderdata())
        .completed(x1.getCompleted())
        .comment_count(x1.getComment_count())
        .priority(x1.getPriority())
        .due(Due.builder().recurring(x2.getRecurring()).my_data(x2.getMy_data()).my_string(x2.getMy_string()).id(x2.getId()).build())
        .build();
}
  • BiFunction takes 2 (Bi) arguments. You will need to write your own try functional interface, `R apply(T t, U u, V v);` – K.Nicholas Mar 26 '21 at 12:58
  • no nicholas dont my question –  Mar 26 '21 at 13:04
  • actually my problem is r2dbc join tables and for a week i can't find how to print it, do you have something to recommend me? –  Mar 26 '21 at 13:06
  • I will be very happy if you help me –  Mar 26 '21 at 13:07
  • 2
    Yes, I recommend you make your question comprehensible. – K.Nicholas Mar 26 '21 at 13:07
  • I'm editing now –  Mar 26 '21 at 13:09
  • please dont ask one question, and when you are not happy with the answer you change your question to something completely other. Stack overflow is a Q&A site, not a forum, you ask one well formulated question, and you get one answer. – Toerktumlare Mar 26 '21 at 13:41

1 Answers1

1

How can I use more than 3 arguments in a Bifunction?

You cannot. A Bifunction specifically takes two arguments, and returns one value. That can never change.

However, I think what you're actually asking here is how you can zip more than two Flux publishers into a single one of a custom type, when the corresponding zip method only takes two publishers and then a BiFunction to combine them.

For the purpose of more than two publishers, we have a further overload of zip which allows you to specify an arbitrary number of publishers, and a combinator to combine them into a set type. The function takes an Object[] which you then cast to the corresponding element, so in your case it would be something like:

private final Function<Object[],TaskDto> function = (Object[] arr) -> TaskDto.builder()
        .id(((Task)arr[0]).getId())
        .url(((Task)arr[0]).getUrl())
        .content(((Task)arr[0]).getUrl())
        .orderdata(((Task)arr[0]).getOrderdata())
        .completed(((Task)arr[0]).getCompleted())
        .comment_count(((Task)arr[0]).getComment_count())
        .priority(((Task)arr[0]).getPriority())
        .due(Due.builder().recurring(((Due)arr[1]).getRecurring()).my_data(((Due)arr[1]).getMy_data()).my_string(((Due)arr[1]).getMy_string()).id(((Due)arr[1]).getId()).build())
        .build();

Note that this zip overload uses varargs therefore requires the function parameter to be specified first, so your return statement then changes to something like return Flux.zip(function,taskFlux,dueFlux);.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216