Automate tasks with LambdasWeekend Challenge - Poker Hand EvaluationToo much fun with EnumMap-related features and lambdas?Collatz conjecture using Java lambdasSimple calculator implemented with lambdasCustom Serialization of Game WorldFinding the total time elapsed in the union of time intervalsCreating a pipeline operator in JavaCompare 2 unordered, rooted trees for shape-isomorphismThreads with lambdas and runnable in JavaObject-oriented calculator

What does "classe [year]" mean, in a biography?

What's a good use case for SELECT * in production code?

If password expiration is applied, should door-lock expiration be applied too?

Pointlessly recurse down the alphabet

What is this nut?

Is there a sonic boom when flying nearby?

Can Vice President Pence be impeached before President Trump?

Re. Office 365, how do I delete my name from the blue title bar of a Word document, AS WELL AS the pink circle next to it that has my initials in it

Can we purchase addon to increase limits on SingleEmail Apex limits?

How does "unlimited holidays" work in practice?

Is the axiom of choice needed in proving that metric spaces in which every infinite subset has a limit point are compact?

Why would Earth be long-term unsuitable for an advanced alien species that's already colonized it?

My professor changed a take-home test to an in-class test with no notice. Can I fight the grade?

Why is it ethical for Ambassador Sondland to have been given an ambassadorship for campaign contributions?

Cheap and safe way to dim 100+ 60W Incandescent bulbs

How can I make my sealing ritual neccessary?

Why does rm manual say that we can run it without any argument, when this is not true?

What is the largest piece of space debris volumetrically?

What anti-aircraft magic adaptation would be better for dragons than flame spitting?

Cooking with sugar makes pan very difficult to clean

Rotational Mechanics: Is Angular Acceleration Possible without any External Torque?

Employer wants me to do something explicitly illegal

Implementing a hash table with true concurrency

What are the stat modifiers (other than Dex) that can be used when calculating AC?



Automate tasks with Lambdas


Weekend Challenge - Poker Hand EvaluationToo much fun with EnumMap-related features and lambdas?Collatz conjecture using Java lambdasSimple calculator implemented with lambdasCustom Serialization of Game WorldFinding the total time elapsed in the union of time intervalsCreating a pipeline operator in JavaCompare 2 unordered, rooted trees for shape-isomorphismThreads with lambdas and runnable in JavaObject-oriented calculator






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









6















$begingroup$


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?










share|improve this question











$endgroup$





















    6















    $begingroup$


    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?










    share|improve this question











    $endgroup$

















      6













      6









      6





      $begingroup$


      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?










      share|improve this question











      $endgroup$




      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?







      java






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 3 at 14:33







      Aashish Pawar

















      asked Sep 18 at 11:53









      Aashish PawarAashish Pawar

      1157 bronze badges




      1157 bronze badges























          2 Answers
          2






          active

          oldest

          votes


















          6

















          $begingroup$

          The pattern you have implemented is basically a reduction or foldLeft method.

          Compare it to the Stream#reduce method in Java, it is very similar.



          To answer your question about the complexity of the lambda to put into the method: There is no limit. You can do everything you like in there. A lambda expression is nothing more than an implementation of the single-method interface you have created. And there is no limit on the complexity of a class implementing an interface.



          Now for some real code review:



          Use proper and understandable names and use built-in types



          Your interface is simply named func. This probably means "function" but you don't have to save on characters, Java is already quite verbose.

          What you have, is a special kind of BiFunction or more precisely, BinaryOperator. Now I know that generics don't allow you to specify primitive types for the generic arguments, but your func interface is not necessary and can be replaced by a BinaryOperator<Integer>.



          Your run method has a very generic name. Sure, there is code that is run, but it doesn't explain, what this method does.

          It would be more precise to call your method reduce or foldLeft or something similar, that explains in the name, what this method does.



          Make your method do one thing



          Currently your method calculates a result and, in addition to that, prints the value of result for every iteration. What if you or someone who uses your method does not want to have every single intermediate result printed to them?



          If you want to print the intermediate values, do so in your lambda.



          You wanted to think about the future



          Your method currently only works with primitive integer arrays. Nothing stops you from making it work with generic types or even lists.

          And if you go that far, you'll probably use streams anyway.






          share|improve this answer










          $endgroup$














          • $begingroup$
            really helpful. I got your point. what should I do to define a method that supports various lambda expressions what we use in streams?
            $endgroup$
            – Aashish Pawar
            Sep 18 at 12:37










          • $begingroup$
            What exactly do you mean with "various lambda expressions"? You can use any expression that satisfies the interface (ie. takes two integers as input and returns one integer). You can't really chain the expressions one after another because a reduce operation is terminal and returns exactly one value.
            $endgroup$
            – GiantTree
            Sep 18 at 13:06










          • $begingroup$
            by Various I mean arithmetic operations .
            $endgroup$
            – Aashish Pawar
            Sep 18 at 15:24






          • 1




            $begingroup$
            Ah, yeah, you can do anything in the lambda expression. There are no limits.
            $endgroup$
            – GiantTree
            Sep 18 at 16:25










          • $begingroup$
            Can I delete Array elements with lambda ? Im sure that won’t be as like reduce method
            $endgroup$
            – Aashish Pawar
            Sep 18 at 16:30


















          4

















          $begingroup$

          Most is already said by GiantTree, so only one additional remark: after replacing func with BinaryOperator (or in this case IntBinaryOperator) and the calculation with reduce, the only thing your run method really does is an array lookup.



          You can solve this directly with the existing standard library - the following is equivalent to your code:



          IntStream.rangeClosed(0, 5).map(i -> array[i]).reduce(0, (a, b) -> a + b);


          My advice: don't reinvent the wheel, get a good grip on the basic libraries instead.






          share|improve this answer










          $endgroup$














          • $begingroup$
            I know IntStream and reduce but It doesn't allow me to modify the underlying array. But In my pattern, I might modify the original array elements.
            $endgroup$
            – Aashish Pawar
            Oct 3 at 14:29












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );














          draft saved

          draft discarded
















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f229236%2fautomate-tasks-with-lambdas%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown


























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6

















          $begingroup$

          The pattern you have implemented is basically a reduction or foldLeft method.

          Compare it to the Stream#reduce method in Java, it is very similar.



          To answer your question about the complexity of the lambda to put into the method: There is no limit. You can do everything you like in there. A lambda expression is nothing more than an implementation of the single-method interface you have created. And there is no limit on the complexity of a class implementing an interface.



          Now for some real code review:



          Use proper and understandable names and use built-in types



          Your interface is simply named func. This probably means "function" but you don't have to save on characters, Java is already quite verbose.

          What you have, is a special kind of BiFunction or more precisely, BinaryOperator. Now I know that generics don't allow you to specify primitive types for the generic arguments, but your func interface is not necessary and can be replaced by a BinaryOperator<Integer>.



          Your run method has a very generic name. Sure, there is code that is run, but it doesn't explain, what this method does.

          It would be more precise to call your method reduce or foldLeft or something similar, that explains in the name, what this method does.



          Make your method do one thing



          Currently your method calculates a result and, in addition to that, prints the value of result for every iteration. What if you or someone who uses your method does not want to have every single intermediate result printed to them?



          If you want to print the intermediate values, do so in your lambda.



          You wanted to think about the future



          Your method currently only works with primitive integer arrays. Nothing stops you from making it work with generic types or even lists.

          And if you go that far, you'll probably use streams anyway.






          share|improve this answer










          $endgroup$














          • $begingroup$
            really helpful. I got your point. what should I do to define a method that supports various lambda expressions what we use in streams?
            $endgroup$
            – Aashish Pawar
            Sep 18 at 12:37










          • $begingroup$
            What exactly do you mean with "various lambda expressions"? You can use any expression that satisfies the interface (ie. takes two integers as input and returns one integer). You can't really chain the expressions one after another because a reduce operation is terminal and returns exactly one value.
            $endgroup$
            – GiantTree
            Sep 18 at 13:06










          • $begingroup$
            by Various I mean arithmetic operations .
            $endgroup$
            – Aashish Pawar
            Sep 18 at 15:24






          • 1




            $begingroup$
            Ah, yeah, you can do anything in the lambda expression. There are no limits.
            $endgroup$
            – GiantTree
            Sep 18 at 16:25










          • $begingroup$
            Can I delete Array elements with lambda ? Im sure that won’t be as like reduce method
            $endgroup$
            – Aashish Pawar
            Sep 18 at 16:30















          6

















          $begingroup$

          The pattern you have implemented is basically a reduction or foldLeft method.

          Compare it to the Stream#reduce method in Java, it is very similar.



          To answer your question about the complexity of the lambda to put into the method: There is no limit. You can do everything you like in there. A lambda expression is nothing more than an implementation of the single-method interface you have created. And there is no limit on the complexity of a class implementing an interface.



          Now for some real code review:



          Use proper and understandable names and use built-in types



          Your interface is simply named func. This probably means "function" but you don't have to save on characters, Java is already quite verbose.

          What you have, is a special kind of BiFunction or more precisely, BinaryOperator. Now I know that generics don't allow you to specify primitive types for the generic arguments, but your func interface is not necessary and can be replaced by a BinaryOperator<Integer>.



          Your run method has a very generic name. Sure, there is code that is run, but it doesn't explain, what this method does.

          It would be more precise to call your method reduce or foldLeft or something similar, that explains in the name, what this method does.



          Make your method do one thing



          Currently your method calculates a result and, in addition to that, prints the value of result for every iteration. What if you or someone who uses your method does not want to have every single intermediate result printed to them?



          If you want to print the intermediate values, do so in your lambda.



          You wanted to think about the future



          Your method currently only works with primitive integer arrays. Nothing stops you from making it work with generic types or even lists.

          And if you go that far, you'll probably use streams anyway.






          share|improve this answer










          $endgroup$














          • $begingroup$
            really helpful. I got your point. what should I do to define a method that supports various lambda expressions what we use in streams?
            $endgroup$
            – Aashish Pawar
            Sep 18 at 12:37










          • $begingroup$
            What exactly do you mean with "various lambda expressions"? You can use any expression that satisfies the interface (ie. takes two integers as input and returns one integer). You can't really chain the expressions one after another because a reduce operation is terminal and returns exactly one value.
            $endgroup$
            – GiantTree
            Sep 18 at 13:06










          • $begingroup$
            by Various I mean arithmetic operations .
            $endgroup$
            – Aashish Pawar
            Sep 18 at 15:24






          • 1




            $begingroup$
            Ah, yeah, you can do anything in the lambda expression. There are no limits.
            $endgroup$
            – GiantTree
            Sep 18 at 16:25










          • $begingroup$
            Can I delete Array elements with lambda ? Im sure that won’t be as like reduce method
            $endgroup$
            – Aashish Pawar
            Sep 18 at 16:30













          6















          6











          6







          $begingroup$

          The pattern you have implemented is basically a reduction or foldLeft method.

          Compare it to the Stream#reduce method in Java, it is very similar.



          To answer your question about the complexity of the lambda to put into the method: There is no limit. You can do everything you like in there. A lambda expression is nothing more than an implementation of the single-method interface you have created. And there is no limit on the complexity of a class implementing an interface.



          Now for some real code review:



          Use proper and understandable names and use built-in types



          Your interface is simply named func. This probably means "function" but you don't have to save on characters, Java is already quite verbose.

          What you have, is a special kind of BiFunction or more precisely, BinaryOperator. Now I know that generics don't allow you to specify primitive types for the generic arguments, but your func interface is not necessary and can be replaced by a BinaryOperator<Integer>.



          Your run method has a very generic name. Sure, there is code that is run, but it doesn't explain, what this method does.

          It would be more precise to call your method reduce or foldLeft or something similar, that explains in the name, what this method does.



          Make your method do one thing



          Currently your method calculates a result and, in addition to that, prints the value of result for every iteration. What if you or someone who uses your method does not want to have every single intermediate result printed to them?



          If you want to print the intermediate values, do so in your lambda.



          You wanted to think about the future



          Your method currently only works with primitive integer arrays. Nothing stops you from making it work with generic types or even lists.

          And if you go that far, you'll probably use streams anyway.






          share|improve this answer










          $endgroup$



          The pattern you have implemented is basically a reduction or foldLeft method.

          Compare it to the Stream#reduce method in Java, it is very similar.



          To answer your question about the complexity of the lambda to put into the method: There is no limit. You can do everything you like in there. A lambda expression is nothing more than an implementation of the single-method interface you have created. And there is no limit on the complexity of a class implementing an interface.



          Now for some real code review:



          Use proper and understandable names and use built-in types



          Your interface is simply named func. This probably means "function" but you don't have to save on characters, Java is already quite verbose.

          What you have, is a special kind of BiFunction or more precisely, BinaryOperator. Now I know that generics don't allow you to specify primitive types for the generic arguments, but your func interface is not necessary and can be replaced by a BinaryOperator<Integer>.



          Your run method has a very generic name. Sure, there is code that is run, but it doesn't explain, what this method does.

          It would be more precise to call your method reduce or foldLeft or something similar, that explains in the name, what this method does.



          Make your method do one thing



          Currently your method calculates a result and, in addition to that, prints the value of result for every iteration. What if you or someone who uses your method does not want to have every single intermediate result printed to them?



          If you want to print the intermediate values, do so in your lambda.



          You wanted to think about the future



          Your method currently only works with primitive integer arrays. Nothing stops you from making it work with generic types or even lists.

          And if you go that far, you'll probably use streams anyway.







          share|improve this answer













          share|improve this answer




          share|improve this answer










          answered Sep 18 at 12:23









          GiantTreeGiantTree

          6185 silver badges9 bronze badges




          6185 silver badges9 bronze badges














          • $begingroup$
            really helpful. I got your point. what should I do to define a method that supports various lambda expressions what we use in streams?
            $endgroup$
            – Aashish Pawar
            Sep 18 at 12:37










          • $begingroup$
            What exactly do you mean with "various lambda expressions"? You can use any expression that satisfies the interface (ie. takes two integers as input and returns one integer). You can't really chain the expressions one after another because a reduce operation is terminal and returns exactly one value.
            $endgroup$
            – GiantTree
            Sep 18 at 13:06










          • $begingroup$
            by Various I mean arithmetic operations .
            $endgroup$
            – Aashish Pawar
            Sep 18 at 15:24






          • 1




            $begingroup$
            Ah, yeah, you can do anything in the lambda expression. There are no limits.
            $endgroup$
            – GiantTree
            Sep 18 at 16:25










          • $begingroup$
            Can I delete Array elements with lambda ? Im sure that won’t be as like reduce method
            $endgroup$
            – Aashish Pawar
            Sep 18 at 16:30
















          • $begingroup$
            really helpful. I got your point. what should I do to define a method that supports various lambda expressions what we use in streams?
            $endgroup$
            – Aashish Pawar
            Sep 18 at 12:37










          • $begingroup$
            What exactly do you mean with "various lambda expressions"? You can use any expression that satisfies the interface (ie. takes two integers as input and returns one integer). You can't really chain the expressions one after another because a reduce operation is terminal and returns exactly one value.
            $endgroup$
            – GiantTree
            Sep 18 at 13:06










          • $begingroup$
            by Various I mean arithmetic operations .
            $endgroup$
            – Aashish Pawar
            Sep 18 at 15:24






          • 1




            $begingroup$
            Ah, yeah, you can do anything in the lambda expression. There are no limits.
            $endgroup$
            – GiantTree
            Sep 18 at 16:25










          • $begingroup$
            Can I delete Array elements with lambda ? Im sure that won’t be as like reduce method
            $endgroup$
            – Aashish Pawar
            Sep 18 at 16:30















          $begingroup$
          really helpful. I got your point. what should I do to define a method that supports various lambda expressions what we use in streams?
          $endgroup$
          – Aashish Pawar
          Sep 18 at 12:37




          $begingroup$
          really helpful. I got your point. what should I do to define a method that supports various lambda expressions what we use in streams?
          $endgroup$
          – Aashish Pawar
          Sep 18 at 12:37












          $begingroup$
          What exactly do you mean with "various lambda expressions"? You can use any expression that satisfies the interface (ie. takes two integers as input and returns one integer). You can't really chain the expressions one after another because a reduce operation is terminal and returns exactly one value.
          $endgroup$
          – GiantTree
          Sep 18 at 13:06




          $begingroup$
          What exactly do you mean with "various lambda expressions"? You can use any expression that satisfies the interface (ie. takes two integers as input and returns one integer). You can't really chain the expressions one after another because a reduce operation is terminal and returns exactly one value.
          $endgroup$
          – GiantTree
          Sep 18 at 13:06












          $begingroup$
          by Various I mean arithmetic operations .
          $endgroup$
          – Aashish Pawar
          Sep 18 at 15:24




          $begingroup$
          by Various I mean arithmetic operations .
          $endgroup$
          – Aashish Pawar
          Sep 18 at 15:24




          1




          1




          $begingroup$
          Ah, yeah, you can do anything in the lambda expression. There are no limits.
          $endgroup$
          – GiantTree
          Sep 18 at 16:25




          $begingroup$
          Ah, yeah, you can do anything in the lambda expression. There are no limits.
          $endgroup$
          – GiantTree
          Sep 18 at 16:25












          $begingroup$
          Can I delete Array elements with lambda ? Im sure that won’t be as like reduce method
          $endgroup$
          – Aashish Pawar
          Sep 18 at 16:30




          $begingroup$
          Can I delete Array elements with lambda ? Im sure that won’t be as like reduce method
          $endgroup$
          – Aashish Pawar
          Sep 18 at 16:30













          4

















          $begingroup$

          Most is already said by GiantTree, so only one additional remark: after replacing func with BinaryOperator (or in this case IntBinaryOperator) and the calculation with reduce, the only thing your run method really does is an array lookup.



          You can solve this directly with the existing standard library - the following is equivalent to your code:



          IntStream.rangeClosed(0, 5).map(i -> array[i]).reduce(0, (a, b) -> a + b);


          My advice: don't reinvent the wheel, get a good grip on the basic libraries instead.






          share|improve this answer










          $endgroup$














          • $begingroup$
            I know IntStream and reduce but It doesn't allow me to modify the underlying array. But In my pattern, I might modify the original array elements.
            $endgroup$
            – Aashish Pawar
            Oct 3 at 14:29















          4

















          $begingroup$

          Most is already said by GiantTree, so only one additional remark: after replacing func with BinaryOperator (or in this case IntBinaryOperator) and the calculation with reduce, the only thing your run method really does is an array lookup.



          You can solve this directly with the existing standard library - the following is equivalent to your code:



          IntStream.rangeClosed(0, 5).map(i -> array[i]).reduce(0, (a, b) -> a + b);


          My advice: don't reinvent the wheel, get a good grip on the basic libraries instead.






          share|improve this answer










          $endgroup$














          • $begingroup$
            I know IntStream and reduce but It doesn't allow me to modify the underlying array. But In my pattern, I might modify the original array elements.
            $endgroup$
            – Aashish Pawar
            Oct 3 at 14:29













          4















          4











          4







          $begingroup$

          Most is already said by GiantTree, so only one additional remark: after replacing func with BinaryOperator (or in this case IntBinaryOperator) and the calculation with reduce, the only thing your run method really does is an array lookup.



          You can solve this directly with the existing standard library - the following is equivalent to your code:



          IntStream.rangeClosed(0, 5).map(i -> array[i]).reduce(0, (a, b) -> a + b);


          My advice: don't reinvent the wheel, get a good grip on the basic libraries instead.






          share|improve this answer










          $endgroup$



          Most is already said by GiantTree, so only one additional remark: after replacing func with BinaryOperator (or in this case IntBinaryOperator) and the calculation with reduce, the only thing your run method really does is an array lookup.



          You can solve this directly with the existing standard library - the following is equivalent to your code:



          IntStream.rangeClosed(0, 5).map(i -> array[i]).reduce(0, (a, b) -> a + b);


          My advice: don't reinvent the wheel, get a good grip on the basic libraries instead.







          share|improve this answer













          share|improve this answer




          share|improve this answer










          answered Sep 18 at 15:14









          mtjmtj

          3,1793 silver badges14 bronze badges




          3,1793 silver badges14 bronze badges














          • $begingroup$
            I know IntStream and reduce but It doesn't allow me to modify the underlying array. But In my pattern, I might modify the original array elements.
            $endgroup$
            – Aashish Pawar
            Oct 3 at 14:29
















          • $begingroup$
            I know IntStream and reduce but It doesn't allow me to modify the underlying array. But In my pattern, I might modify the original array elements.
            $endgroup$
            – Aashish Pawar
            Oct 3 at 14:29















          $begingroup$
          I know IntStream and reduce but It doesn't allow me to modify the underlying array. But In my pattern, I might modify the original array elements.
          $endgroup$
          – Aashish Pawar
          Oct 3 at 14:29




          $begingroup$
          I know IntStream and reduce but It doesn't allow me to modify the underlying array. But In my pattern, I might modify the original array elements.
          $endgroup$
          – Aashish Pawar
          Oct 3 at 14:29


















          draft saved

          draft discarded















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f229236%2fautomate-tasks-with-lambdas%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown









          Popular posts from this blog

          Tamil (spriik) Luke uk diar | Nawigatjuun

          Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

          Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?