Can a predicate be an object in JavaScript?
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
add a comment |
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
1
Can a predicate be an object in JavaScript? -> NO
– Anand Undavia
Nov 15 '18 at 13:14
add a comment |
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
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
javascript
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
1
You need to either bind the method or pass the instance as athisArg
– 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
add a comment |
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)
add a comment |
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);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
1
You need to either bind the method or pass the instance as athisArg
– 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
add a comment |
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
1
You need to either bind the method or pass the instance as athisArg
– 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
add a comment |
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
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
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 athisArg
– 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
add a comment |
1
You need to either bind the method or pass the instance as athisArg
– 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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 15 '18 at 13:23
Yury TarabankoYury Tarabanko
30.9k64668
30.9k64668
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 15 '18 at 13:25
BergiBergi
377k63574903
377k63574903
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Can a predicate be an object in JavaScript? -> NO
– Anand Undavia
Nov 15 '18 at 13:14