Can a predicate be an object in JavaScript?










-1















Can a predicate be an object in JavaScript (as in C++)?



For example, is it possible to find the index of an element in an array (with arr.findIndex(pred)) with a predicate like this?



 class Predicate

constructor()

this.sum = 0;


evaluate(elem)

this.sum += elem;
return sum > 25;



const pred = new Predicate();

const index = arr.findIndex(pred);


EDIT1: If no, what is the easiest way to find the index of an element that makes the sum of it and all the previous elements exceed 25?










share|improve this question



















  • 1





    Can a predicate be an object in JavaScript? -> NO

    – Anand Undavia
    Nov 15 '18 at 13:14















-1















Can a predicate be an object in JavaScript (as in C++)?



For example, is it possible to find the index of an element in an array (with arr.findIndex(pred)) with a predicate like this?



 class Predicate

constructor()

this.sum = 0;


evaluate(elem)

this.sum += elem;
return sum > 25;



const pred = new Predicate();

const index = arr.findIndex(pred);


EDIT1: If no, what is the easiest way to find the index of an element that makes the sum of it and all the previous elements exceed 25?










share|improve this question



















  • 1





    Can a predicate be an object in JavaScript? -> NO

    – Anand Undavia
    Nov 15 '18 at 13:14













-1












-1








-1








Can a predicate be an object in JavaScript (as in C++)?



For example, is it possible to find the index of an element in an array (with arr.findIndex(pred)) with a predicate like this?



 class Predicate

constructor()

this.sum = 0;


evaluate(elem)

this.sum += elem;
return sum > 25;



const pred = new Predicate();

const index = arr.findIndex(pred);


EDIT1: If no, what is the easiest way to find the index of an element that makes the sum of it and all the previous elements exceed 25?










share|improve this question
















Can a predicate be an object in JavaScript (as in C++)?



For example, is it possible to find the index of an element in an array (with arr.findIndex(pred)) with a predicate like this?



 class Predicate

constructor()

this.sum = 0;


evaluate(elem)

this.sum += elem;
return sum > 25;



const pred = new Predicate();

const index = arr.findIndex(pred);


EDIT1: If no, what is the easiest way to find the index of an element that makes the sum of it and all the previous elements exceed 25?







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:19







Alexey Starinsky

















asked Nov 15 '18 at 13:11









Alexey StarinskyAlexey Starinsky

677111




677111







  • 1





    Can a predicate be an object in JavaScript? -> NO

    – Anand Undavia
    Nov 15 '18 at 13:14












  • 1





    Can a predicate be an object in JavaScript? -> NO

    – Anand Undavia
    Nov 15 '18 at 13:14







1




1





Can a predicate be an object in JavaScript? -> NO

– Anand Undavia
Nov 15 '18 at 13:14





Can a predicate be an object in JavaScript? -> NO

– Anand Undavia
Nov 15 '18 at 13:14












3 Answers
3






active

oldest

votes


















1














It should be a callback, this the function definition



arr.findIndex(callback(element[, index[, array]])[, thisArg])


You can provide your Predicate objects evaluate method. Like this



const index = arr.findIndex(pred.evaluate,pred);// pass pred as thisArg





share|improve this answer




















  • 1





    You need to either bind the method or pass the instance as a thisArg

    – Yury Tarabanko
    Nov 15 '18 at 13:19











  • findIndex is being called on arr (Array), method is bound to the array. It runs perfectly fine.

    – Abhi
    Nov 15 '18 at 13:31











  • Well, no jsfiddle.net/4ga3sLy9

    – Yury Tarabanko
    Nov 15 '18 at 13:37











  • my bad, i removed his logic and just tested the callback. Edited answer.Thank you for pointing out! jsfiddle.net/4ga3sLy9/3

    – Abhi
    Nov 15 '18 at 13:59



















1














Predicate has to be a function. MDN. But you could always create a function that returns another function.






const runningSum = (sum, current = 0) => value => (current += value) > sum

const index = [1, 2, 3, 4, 5, 6, 7, 8, 9].findIndex(runningSum(25))

console.log(index)








share|improve this answer






























    1














    There is no such thing as an evaluate method (that would get invoked when an object gets called like a function) in JavaScript. However, functions themselves are objects and you could even create them with class syntax. It's not very useful though, in JS you would just write a closure to create a function with instance-specific values:



    function predicate(max) 
    var sum = 0;
    return function(elem)
    sum += elem;
    return sum > max;
    ;


    const pred = predicate(25);

    const index = arr.findIndex(pred);





    share|improve this answer






















      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: "1"
      ;
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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/3.0/"u003ecc by-sa 3.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%2fstackoverflow.com%2fquestions%2f53320270%2fcan-a-predicate-be-an-object-in-javascript%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      It should be a callback, this the function definition



      arr.findIndex(callback(element[, index[, array]])[, thisArg])


      You can provide your Predicate objects evaluate method. Like this



      const index = arr.findIndex(pred.evaluate,pred);// pass pred as thisArg





      share|improve this answer




















      • 1





        You need to either bind the method or pass the instance as a thisArg

        – Yury Tarabanko
        Nov 15 '18 at 13:19











      • findIndex is being called on arr (Array), method is bound to the array. It runs perfectly fine.

        – Abhi
        Nov 15 '18 at 13:31











      • Well, no jsfiddle.net/4ga3sLy9

        – Yury Tarabanko
        Nov 15 '18 at 13:37











      • my bad, i removed his logic and just tested the callback. Edited answer.Thank you for pointing out! jsfiddle.net/4ga3sLy9/3

        – Abhi
        Nov 15 '18 at 13:59
















      1














      It should be a callback, this the function definition



      arr.findIndex(callback(element[, index[, array]])[, thisArg])


      You can provide your Predicate objects evaluate method. Like this



      const index = arr.findIndex(pred.evaluate,pred);// pass pred as thisArg





      share|improve this answer




















      • 1





        You need to either bind the method or pass the instance as a thisArg

        – Yury Tarabanko
        Nov 15 '18 at 13:19











      • findIndex is being called on arr (Array), method is bound to the array. It runs perfectly fine.

        – Abhi
        Nov 15 '18 at 13:31











      • Well, no jsfiddle.net/4ga3sLy9

        – Yury Tarabanko
        Nov 15 '18 at 13:37











      • my bad, i removed his logic and just tested the callback. Edited answer.Thank you for pointing out! jsfiddle.net/4ga3sLy9/3

        – Abhi
        Nov 15 '18 at 13:59














      1












      1








      1







      It should be a callback, this the function definition



      arr.findIndex(callback(element[, index[, array]])[, thisArg])


      You can provide your Predicate objects evaluate method. Like this



      const index = arr.findIndex(pred.evaluate,pred);// pass pred as thisArg





      share|improve this answer















      It should be a callback, this the function definition



      arr.findIndex(callback(element[, index[, array]])[, thisArg])


      You can provide your Predicate objects evaluate method. Like this



      const index = arr.findIndex(pred.evaluate,pred);// pass pred as thisArg






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 '18 at 13:59

























      answered Nov 15 '18 at 13:16









      AbhiAbhi

      35310




      35310







      • 1





        You need to either bind the method or pass the instance as a thisArg

        – Yury Tarabanko
        Nov 15 '18 at 13:19











      • findIndex is being called on arr (Array), method is bound to the array. It runs perfectly fine.

        – Abhi
        Nov 15 '18 at 13:31











      • Well, no jsfiddle.net/4ga3sLy9

        – Yury Tarabanko
        Nov 15 '18 at 13:37











      • my bad, i removed his logic and just tested the callback. Edited answer.Thank you for pointing out! jsfiddle.net/4ga3sLy9/3

        – Abhi
        Nov 15 '18 at 13:59













      • 1





        You need to either bind the method or pass the instance as a thisArg

        – Yury Tarabanko
        Nov 15 '18 at 13:19











      • findIndex is being called on arr (Array), method is bound to the array. It runs perfectly fine.

        – Abhi
        Nov 15 '18 at 13:31











      • Well, no jsfiddle.net/4ga3sLy9

        – Yury Tarabanko
        Nov 15 '18 at 13:37











      • my bad, i removed his logic and just tested the callback. Edited answer.Thank you for pointing out! jsfiddle.net/4ga3sLy9/3

        – Abhi
        Nov 15 '18 at 13:59








      1




      1





      You need to either bind the method or pass the instance as a thisArg

      – Yury Tarabanko
      Nov 15 '18 at 13:19





      You need to either bind the method or pass the instance as a thisArg

      – Yury Tarabanko
      Nov 15 '18 at 13:19













      findIndex is being called on arr (Array), method is bound to the array. It runs perfectly fine.

      – Abhi
      Nov 15 '18 at 13:31





      findIndex is being called on arr (Array), method is bound to the array. It runs perfectly fine.

      – Abhi
      Nov 15 '18 at 13:31













      Well, no jsfiddle.net/4ga3sLy9

      – Yury Tarabanko
      Nov 15 '18 at 13:37





      Well, no jsfiddle.net/4ga3sLy9

      – Yury Tarabanko
      Nov 15 '18 at 13:37













      my bad, i removed his logic and just tested the callback. Edited answer.Thank you for pointing out! jsfiddle.net/4ga3sLy9/3

      – Abhi
      Nov 15 '18 at 13:59






      my bad, i removed his logic and just tested the callback. Edited answer.Thank you for pointing out! jsfiddle.net/4ga3sLy9/3

      – Abhi
      Nov 15 '18 at 13:59














      1














      Predicate has to be a function. MDN. But you could always create a function that returns another function.






      const runningSum = (sum, current = 0) => value => (current += value) > sum

      const index = [1, 2, 3, 4, 5, 6, 7, 8, 9].findIndex(runningSum(25))

      console.log(index)








      share|improve this answer



























        1














        Predicate has to be a function. MDN. But you could always create a function that returns another function.






        const runningSum = (sum, current = 0) => value => (current += value) > sum

        const index = [1, 2, 3, 4, 5, 6, 7, 8, 9].findIndex(runningSum(25))

        console.log(index)








        share|improve this answer

























          1












          1








          1







          Predicate has to be a function. MDN. But you could always create a function that returns another function.






          const runningSum = (sum, current = 0) => value => (current += value) > sum

          const index = [1, 2, 3, 4, 5, 6, 7, 8, 9].findIndex(runningSum(25))

          console.log(index)








          share|improve this answer













          Predicate has to be a function. MDN. But you could always create a function that returns another function.






          const runningSum = (sum, current = 0) => value => (current += value) > sum

          const index = [1, 2, 3, 4, 5, 6, 7, 8, 9].findIndex(runningSum(25))

          console.log(index)








          const runningSum = (sum, current = 0) => value => (current += value) > sum

          const index = [1, 2, 3, 4, 5, 6, 7, 8, 9].findIndex(runningSum(25))

          console.log(index)





          const runningSum = (sum, current = 0) => value => (current += value) > sum

          const index = [1, 2, 3, 4, 5, 6, 7, 8, 9].findIndex(runningSum(25))

          console.log(index)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 13:23









          Yury TarabankoYury Tarabanko

          30.9k64668




          30.9k64668





















              1














              There is no such thing as an evaluate method (that would get invoked when an object gets called like a function) in JavaScript. However, functions themselves are objects and you could even create them with class syntax. It's not very useful though, in JS you would just write a closure to create a function with instance-specific values:



              function predicate(max) 
              var sum = 0;
              return function(elem)
              sum += elem;
              return sum > max;
              ;


              const pred = predicate(25);

              const index = arr.findIndex(pred);





              share|improve this answer



























                1














                There is no such thing as an evaluate method (that would get invoked when an object gets called like a function) in JavaScript. However, functions themselves are objects and you could even create them with class syntax. It's not very useful though, in JS you would just write a closure to create a function with instance-specific values:



                function predicate(max) 
                var sum = 0;
                return function(elem)
                sum += elem;
                return sum > max;
                ;


                const pred = predicate(25);

                const index = arr.findIndex(pred);





                share|improve this answer

























                  1












                  1








                  1







                  There is no such thing as an evaluate method (that would get invoked when an object gets called like a function) in JavaScript. However, functions themselves are objects and you could even create them with class syntax. It's not very useful though, in JS you would just write a closure to create a function with instance-specific values:



                  function predicate(max) 
                  var sum = 0;
                  return function(elem)
                  sum += elem;
                  return sum > max;
                  ;


                  const pred = predicate(25);

                  const index = arr.findIndex(pred);





                  share|improve this answer













                  There is no such thing as an evaluate method (that would get invoked when an object gets called like a function) in JavaScript. However, functions themselves are objects and you could even create them with class syntax. It's not very useful though, in JS you would just write a closure to create a function with instance-specific values:



                  function predicate(max) 
                  var sum = 0;
                  return function(elem)
                  sum += elem;
                  return sum > max;
                  ;


                  const pred = predicate(25);

                  const index = arr.findIndex(pred);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 13:25









                  BergiBergi

                  377k63574903




                  377k63574903



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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.

                      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%2fstackoverflow.com%2fquestions%2f53320270%2fcan-a-predicate-be-an-object-in-javascript%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto