Can I use #define fori(i,n) for(int i=0;i<=n;i++)
in Java?
And of course, Java doesn't have precompiler so we cannot do that in java. So I thought we can do that fori
method either using stream()
or manually defining a method. But I thought instead of just traversing why can't I put the third parameter as lambda expression that operates.
Functional interface:
interface func
{
int calculate(int a,int b);
}
Method implementation:
int run(int[] array,int start,int end,func t)
{
int result =0;
for(int i=start;i<=end;i++)
{
System.out.println(result);
result = t.calculate(array[i], result);
}
return result;
}
Usage:
public static void main(String[] args)
{
int[] array = {1,2,3,4,5,3,5,3,2,2,3,2,23,2};
System.out.println(run(array, 0, 5, (a,b) -> a + b ));
}
I would like a review on this code pattern. My major concern is that how much complicated lambda expression I can use here. Also, what is the future scope of this what could be done with this pattern?