I don't understand the usage of code where there's no block of code after if (..isSimulation) that instead just does return after which it follows up with an actual code that is suppose to access restful api from another server.
Right so:
if (this.isSimulation) return;
var charsInfo = new Array();
_keys.forEach(function (key, index, array) {
EveOnline.fetch('account:Characters', { keyID: key.key1, vCode: key.key2 }, Meteor.bindEnvironment(function(err, result) {
if (err) {
In most C-based languages you can omit the code block ({ and } brackets) if the inner block evaluates to one statement.
For instance this:
if (x == 0) {
x++;
}
and this:
if (x == 0)
x++;
do exactly the same thing. This works also for nested conditionals, like in your example.
So this:
for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (i>j) {
x++;
}
}
}
and this:
for (i=0; i<10; i++)
for (j=0; j<10; j++)
if (i>j)
x++;
are also the same thing. The nested conditional/loop is seen as a single evaluated "thing" until it gets to the end. Basically, the brackets defining the code blocks are assumed in the second example.
What you can't do is have a multi line code block not be surrounded in curlies.
For instance this:
if (i>j) {
x++;
y++;
}
And this:
if (i>j)
x++;
y++;
do NOT do the same thing at all. In the first case, x and y are both incremented only if i is greater than j. In the second case, x will be incremented only if i is greater than j, but y gets incremented all the time as it's not part of the if conditional before it. This is because x++ is considered one "statement" and y++ is considered another, and only the first statement is assumed to be part of the proceeding if block.
Related
I want to initialize a square of textures with sf::sprite elements.
To make the main()-function as clean as possible, I wrote a function in another file, it looks like this:
vector<vector<sf::Sprite>> init_board_graphics(int size) {
sf::Texture _t1;
sf::Texture _t2;
_t1.loadFromFile("images/whitefield.png");
_t2.loadFromFile("images/blackfield.png");
vector<vector<sf::Sprite>> board;
//init vector for insert row-values
vector<sf::Sprite> help_vector;
for (int i = 0; i < size; i++) {
help_vector.clear();
for (int j = 0; j < size; j++) {
sf::Sprite insert_element;
if ((i + j) % 2 == 0) {
insert_element = sf::Sprite(_t1);
}
else {
insert_element = sf::Sprite(_t2);
}
insert_element.setPosition(i * pixel_field, j * pixel_field);
help_vector.push_back(insert_element);
}
board.push_back(help_vector);
}
return board;
}
When I call the function in main(), everything is fine at first, the elements are at the correct positions and the textures are also of correct size.
When I try to debug the next steps, I can observe that the textures of the elements are getting changed in size, even though the next lines have absolutely nothing to do with my object. First, I thought the entries are getting changed because I allocate the entries in the <vector<vector<sf::sprite>> - object in a wrong way but I fixed this:
if (parameters::visual_output) {
//Draw field
for (int i = 0; i < size_board; i++) {
vector<sf::Sprite> help_vec = board[i];
for (int j = 0; j < size_board; j++) {
window.draw(help_vec[j]);
}
}
}
The textures are still getting changed for some reason. Why is this happening?
In my flex mobile application, I have a loop running for over 100 iterations. In each iteration I'm updating some properties of specific Label(s). Since the loop is time consuming, I need to update the screen and display intermediate results at each iteration. How can I break the loop and refresh the display list?
function theFunction():void{
for var i:int = 0; i < n; i++{
doBusyStuff();
label_1.text = "iteration"+" i";
}
}
In that situation, I prefer to use flash.utils.setTimeout()
import flash.utils.setTimeout;
function theFunction(limit:int, current:int = 0):void
{
if (current >= limit)
return;
doBusyStuff();
label_1.text = "iteration "+ current.toString();
setTimeout(theFunction, 0, limit, current+1);
}
However, both setTimeout() and callLater() depend on the tick or the frame rate, meaning that they won't do as fast as they can. So if you also want it to run faster, you should have it run a few loops per each call.
Another solution, similar to Chaniks' answer, uses DateTime to check how long the loop has been running on each iteration. Once it detects that it's been running for too long, it ends the loop and picks up again on the next Frame.
var i:int;
function callingFunction():void {
i = 0;
stage.addEventListener(Event.ENTER_FRAME, theFunction);
}
function theFunction(e:Event):void {
var time:DateTime = new DateTime();
var allowableTime:int = 30; //Allow 30ms per frame
while ((new DateTime().time - time.time < allowableTime) && i < n) {
doBusyStuff();
i++;
}
if (i >= n) {
stage.removeEventListener(Event.ENTER_FRAME, theFunction);
}
label_1.text = "iteration"+" i";
}
There are several methods to force redraw of components:
invalidateDisplayList();
invalidateProperties();
invalidateSize();
Just use whatever you need for your components inside a function and call it after your script using callLater(yourRedrawFunction);
EDIT: For example, in your case:
function theFunction():void{
for var i:int = 0; i < n; i++{
doBusyStuff();
label_1.text = "iteration"+" i";
}
callLater(yourRedrawFunction);
}
This is a recursive solver to try to solve Euler#60. http://projecteuler.net/problem=60
The solver runs through, but fails to find a solution for the last array member, so backtracks (like I think it's supposed to) but when I get back to the first array member, the loop runs out all the way. Can anybody spot for me why it doesn't stop at the next prime?
I've posted just the solver function below; the other function (Concat check) works properly and returns true for a partially filled array.
int Solver (int primes[5])
{
int i=1;
int x=0;
while (primes[x]!=0) {++x;} //work on the next one
if ((x>5) && Concat_Check(primes)) {return 1;} //solved array
for (i=3; i<=SIZE; i++) //try each value, if successful, return true
{
if (Is_Prime(i)) {primes[x]=i; cout<<"primes["<<x<<"] = "<<i<<endl;}
if ((Concat_Check (primes)) && Solver (primes)) {return 1;}
}
primes[x-1] = 0;
return 0;
}
I can't get the purpose of recursion in your code, seems a loop...
Anyway, maybe you forgot to increment x in loop, and the test seems incomplete.
for (i=3; i<=SIZE; i+=2) //try each value, if successful, return true
{
if (Is_Prime(i)) {
primes[x++]=i; cout<<"primes["<<x<<"] = "<<i<<endl;}
if ((Concat_Check (primes)) && Solver (primes)) {return 1;}
}
}
I have 2 while loops, and in the second there is a break; (see the code below)
My question is: will the break cause a stop in the second loop or for the 2?
while select dirPartyRelationship
join dirPartyTable
where dirPartyTable.RecId == dirPartyRelationship.ChildParty
join dirPersonName
where dirPersonName.Person == dirPartyTable.RecId
{
while select checkDirRelationship
where checkDirRelationship.ChildParty == dirPartyRelationship.RecId
{
if (checkDirRelationship.RelationshipTypeId == _relationshipType)
{
break;
}
}...
The break will only break out of the current code block.
Create a job and use this sample code;
for(i=0; i<100; i++)
{
for(j=0; j<100; j++)
{
info(strfmt("inner loop count %1",j));
break;
}
info(strfmt("outer loop count %1",i));
}
You will see a quick example of j never getting over 0, but being printed 100 times.
Edit;
If you want to break out of the nested loop you could work around by declaring a boolean, maybe called breakAll, and setting breakAll to true before the break; line in the inner loop. In the outer loop check for breakAll like this;
for(i=0; i<100; i++)
{
for(j=0; j<100; j++)
{
info(strfmt("inner loop count %1",j));
if (somethingToCheck)
{
breakAll = true;
break;
}
}
info(strfmt("outer loop count %1",i));
if (breakAll)
{
break;
}
}
I’m new to Handlebars.js and just started using it. Most of the examples are based on iterating over an object. I wanted to know how to use handlebars in basic for loop.
Example.
for(i=0 ; i<100 ; i++) {
create li's with i as the value
}
How can this be achieved?
There's nothing in Handlebars for this but you can add your own helpers easily enough.
If you just wanted to do something n times then:
Handlebars.registerHelper('times', function(n, block) {
var accum = '';
for(var i = 0; i < n; ++i)
accum += block.fn(i);
return accum;
});
and
{{#times 10}}
<span>{{this}}</span>
{{/times}}
If you wanted a whole for(;;) loop, then something like this:
Handlebars.registerHelper('for', function(from, to, incr, block) {
var accum = '';
for(var i = from; i < to; i += incr)
accum += block.fn(i);
return accum;
});
and
{{#for 0 10 2}}
<span>{{this}}</span>
{{/for}}
Demo: http://jsfiddle.net/ambiguous/WNbrL/
Top answer here is good, if you want to use last / first / index though you could use the following
Handlebars.registerHelper('times', function(n, block) {
var accum = '';
for(var i = 0; i < n; ++i) {
block.data.index = i;
block.data.first = i === 0;
block.data.last = i === (n - 1);
accum += block.fn(this);
}
return accum;
});
and
{{#times 10}}
<span> {{#first}} {{#index}} {{#last}}</span>
{{/times}}
If you like CoffeeScript
Handlebars.registerHelper "times", (n, block) ->
(block.fn(i) for i in [0...n]).join("")
and
{{#times 10}}
<span>{{this}}</span>
{{/times}}
This snippet will take care of else block in case n comes as dynamic value, and provide #index optional context variable, it will keep the outer context of the execution as well.
/*
* Repeat given markup with given times
* provides #index for the repeated iteraction
*/
Handlebars.registerHelper("repeat", function (times, opts) {
var out = "";
var i;
var data = {};
if ( times ) {
for ( i = 0; i < times; i += 1 ) {
data.index = i;
out += opts.fn(this, {
data: data
});
}
} else {
out = opts.inverse(this);
}
return out;
});
Couple of years late, but there's now each available in Handlebars which allows you to iterate pretty easily over an array of items.
https://handlebarsjs.com/guide/builtin-helpers.html#each