I was reading the documentation of the foreach package in r and was able to understand how .combine is done on c, rbind and cbind. But the documentation says that you can also pass the name of a function to the .combine in the statement if you want to combine using this function. I'm not able to understand the output when trying to access the individual elements of each foreach inside this function that I'm combining on. (Let me call this function as mycombinefunc)
This is what I have tried. I don't understand why the output is printed twice in the output of the foreach statement.
> mycombinefunc= function(...){mylist = list(...);print(mylist)}
> foreach(i=1:2, .combine='mycombinefunc') %do% {c(1:5)}
[[1]]
[1] 1 2 3 4 5
[[2]]
[1] 1 2 3 4 5
[[1]]
[1] 1 2 3 4 5
[[2]]
[1] 1 2 3 4 5
#just calling the function directly to show what the expected output for one iteration of the foreach loop looks like. I would have expected that the above function call would produce two copies of the below output
> mycombinefunc(c(1:5))
[[1]]
[1] 1 2 3 4 5