Groovy for loop not incrementing correctly - for-loop

I am writing a jenkins pipeline with what is a mixture of declarative and scripted syntax. I want to loop 4 times to create 4 parallel stages in the pipeline. I want to capture what loop I'm on, and print that at the beggining of each step:
def parallelStages = [:]
int cypressRunners = 4
for (int i = 1; i <= cypressRunners; i++) {
parallelStages["Cypress ${i}"] = {
stage("Cypress ${i}") {
node("cypress-runner-agent") {
echo "------------------ Cypress ${i} Stage ------------------"
// more steps
}
}
}
}
In the jenkins log, I always get a printout of
------------------ Runner 5 Stage ------------------
Even though in the left side, where the stage names are listed, I get the correctly indexed names of Cypress 1, Cypress 2, Cypress 3, Cypress 4, like this:
I am not a java or groovy developer, but this seems like a pretty simple for loop in any language. Is there some aspect of async, or closures, that I am missing here? Tons of questions exist about for loops not working as expected, but none in a jenkins-groovy-pipeline context, and certainly not for this scenario.

I figured out a solution, rather than delete I will post an answer in the event that it may help someone else in the future. My issue was similar to Iterate and print content of groovy closures, though not exactly. With help from that question, I just capture the current value of the index within the closure of the for loop and used that:
for (int i = 1; i <= cypressRunners; i++) {
def j = i // < ------- capture value within closure
parallelStages["Cypress ${j}"] = {
stage("Cypress ${j}") {
node("cypress-runner-agent") {
echo "------------------ Cypress ${j} Stage ------------------"
// more steps
}
}
}
}

Related

What makes this a fixed-length list in Dart?

List<String> checkLength(List<String> input) {
if (input.length > 6) {
var tempOutput = input;
while (tempOutput.length > 6) {
var difference = (tempOutput.length/6).round() + 1;
for (int i = 0; i < tempOutput.length - 1; i + difference) {
tempOutput.removeAt(i); //Removing the value from the list
}
}
return tempOutput; //Return Updated list
} else {
return input;
}
}
I am trying to delete something out of a temporary list. Why does it not work? I do not see how it is fixed, in other problems I have solved, I used a similar approach and it worked (Even identical nearly)
Please note I am kind of new to Dart, so please forgive me this sort of question, but I couldn't figure out the solution.
Find the Code available in the Dart Link
Code in Dart
You can ensure that tempOutput is not a fixed-length list by initializing it as
var tempOutput = new List<String>.from(input);
thereby declaring tempOutput to be a mutable copy of input.
FYI it also looks like you have another bug in your program since you are doing i + difference in your for-loop update step but I think you want i += difference.
Can you please try this code and let me know is that works?
List<String> checkLength(List<String> input) {
if (input.length > 6) {
var tempOutput = input;
while (tempOutput.length > 6) {
var difference = (tempOutput.length/6).round() + 1;
for (int i = 0; i < tempOutput.length - 1; i = i + difference) {
tempOutput.removeAt(i); //Removing the value from the list
}
}
return tempOutput.toList(); //Return Updated list
} else {
return input.toList();
}
}
Note: You used "i + difference" which is same value say for example in first iteration you i=1 and difference = 1, then "tempOutput.removeAt(i)" will remove the value at "1" position, again in the second iteration you are trying to remove the same position, so the error clearly states "Cannot remove from the Fixed length"
Here the i value has to be incremented or decremented for each iteration process, in your for loop that is missing.
The answer of #harry-terkelsen was very helpful for solving the fixed-length problem.
For those who were asking about my algorithm:
The difference is for skipping the amount of characters when wanting to remove some. Also, I had to change the for-loop, as it did not quite do what I wanted it to.
The fix is here! https://github.com/luki/wordtocolor/blob/master/web/algorithms.dart
Thank you for understanding me!

Pex ignores default parameter assignment

I am using Pex to analyse function executions.
However, I noticed that default parameters are not looked at.
Here's an example of what I mean:
public int bla(int x = 2)
{
return x * 2;
}
When I run Pex, it generates the test case for int result = bla(0);. (x = 0)
Is there a way to tell Pex that it should also try to call bla( without parameter (i.e. int result = bla() )?
The 1st rule of IntelliTest/Pex is it tries to increase code coverage.
If all statements have been covered, Pex will stop.
There are many ways to add some code that only gets covered when x=2, such as in the test method. This might be the simplest that worked for me:
[PexMethod]
public int bla([PexAssumeUnderTest]Class1 target, int x)
{
if(x == 2)
{
PexAssert.ReachEventually();
}
int result = target.bla(x);
return result;
// TODO: add assertions to method Class1Test.bla(Class1, Int32)
}
The exploration results window should show:
x result
0 0
2 4
I don't know of any way to have Pex automatically generate test cases for all default parameters.
In real world production code it's highly likely the default value will be used in the code so you might not run into this problem often.
And if you have all the code paths covered by Pex does it really matter whether the default value is used or not?
It's probably more import to test the methods that call 'bla' with and without supplying a value.

Parsing script loops in C#

I'm writing an application that will parse a script in a custom language (based slightly on C syntax and Allman style formatting) and am looking for a better (read: faster) way of parsing blocks of the script code into string arrays than the way I'm currently doing it (the current method will do, but it was more for debug than anything else).
The script contents are currently read from a file into a string array and passed to the method.
Here's a script block template:
loop [/* some conditional */ ]
{
/* a whole bunch of commands that are to be read into
* a List<string>, then converted to a string[] and
* passed to the next step for execution */
/* some command that has a bracket delimited set of
* properties or attributes */
{
/* some more commands to be acted on */
}
}
Basically, the curly bracket blocks can be nested (just like in any other C-based language), and I'm looking for the best way to find individual blocks like this.
The curly bracket delimited blocks will ALWAYS be formatted like this - the contents of the brackets will start on the line after the open bracket and will be followed by a bracket on the line after the final attribute/command/comment/whatever.
An example might be:
loop [ someVar <= 10 ]
{
informUser "Get ready to do something"
readValue
{
valueToLookFor = 0x54
timeout = 10 /* in seconds */
}
}
This would tell the app to loop whilst someVar is less than 10 (sorry for the sucking eggs comment). Each time round, we pass a message to the user and look for a specific value from somewhere (with a timeout of 10 seconds).
Here's how I'm doing it at the minute (note: the method that calls this passes the entire string[] containing the current script into it with an index to read from):
private string[] findEntireBlock(string[] scriptContents, int indexToReadFrom,
out int newIndex)
{
newIndex = 0;
int openBraceCount = 0; // for '{' char count
int closeBraceCount = 0; // for '}' char count
int openSquareCount = 0; // for '[' char count
int closeSquareCount = 0; // for ']' char count
List<string> fullblock = new List<string>();
for (int i = indexToReadFrom; i < scriptContents.Length; i++)
{
if (scriptContents[i].Contains('}'))
{
if (scriptContents[i].Contains("[") && fullblock.Count > 0)
{
//throw new exception, as we shouldn't expect to
//to find a line which starts with [ when we've already
}
else
{
if (scriptContents[i].Contains('{')) openBraceCount++;
if (scriptContents[i].Contains('}')) closeBraceCount++;
if (scriptContents[i].Contains('[')) openSquareCount++;
if (scriptContents[i].Contains(']')) closeBraceCount++;
newIndex = i;
fullblock.Add(scriptContents[i]);
break;
}
}
else
{
if (scriptContents[i].Contains("[") && fullblock.Count > 0)
{
//throw new exception, as we shouldn't expect to
//to find a line which starts with [ when we've already
}
else
{
if (scriptContents[i].Contains('{')) openBraceCount++;
if (scriptContents[i].Contains('}')) closeBraceCount++;
if (scriptContents[i].Contains('[')) openSquareCount++;
if (scriptContents[i].Contains(']')) closeBraceCount++;
fullblock.Add(scriptContents[i]);
}
}
}
if (openBraceCount == closeBraceCount &&
openSquareCount == closeSquareCount)
return fullblock.ToArray();
else
//throw new exception, the number of open brackets doesn't match
//the number of close brackets
}
I agree that this might be a slightly obtuse and slow method to follow, that's why I'm asking for any ideas on how to re-implement this for speed and clarity (if a balance can be met, that is).
I'm looking to stay away from RegEx, because I can't use it to maintain a bracket count and I'm not sure on whether you can write a RegEx statement (is that the correct term?) that can act recursively. I was thinking of working from the inside outward, but I'm convinced that would be quite slow.
I'm not looking for someone to re-write it for me, but a general idea on algorithms or techniques/libraries that I could use that would improve my method.
As a side question, how do compilers deal with multiple nested brackets in source code?
Let's Build a Compiler, by Jack Crenshaw, is a fantastic, easy-to-read introduction to building a basic compiler. The techniques discussed should help with what you're trying to do here.

nice way of doing 'if ALL items in collection pass test'

I mean this:
bool passed = true;
for(int i = 0; i < collection.Length; i++)
{
if(!PassesTest(collection[i]))
{
passed = false;
break;
}
}
if(passed){/*passed code*/}
requires extra variable, extra test
for(int i = 0; i < collection.Length; i++)
{
if(!PassesTest(collection[i]))
{
return;
}
}
{/*passed code*/}
neat, but requires this to be it's own function, if this it's self is inside a loop or something, not the most performant way of doing things. also, writing a whole extra function is a pain
if(passed){/*passed code*/}
for(int i = 0; i < collection.Length; i++)
{
if(!PassesTest(collection[i]))
{
goto failed;
}
}
{/*passed code*/}
failed: {}
great, but you have to screw around with label names and ugly label syntax
for(int i = 0; ; i++)
{
if(!(i < collection.Length))
{
{/*passed code*/}
break;
}
if(!PassesTest(collection[i]))
{
break;
}
}
probably the nicest, but still a bit manual, kinda wasting the functionality of the for loop construct, for instance, you can't do this with a foreach
what is the nicest way to handle this problem?
it seems to me something like this would be nice:
foreach(...)
{
...
}
finally{...} // only executed if loop ends conventionally (without break)
am I missing something? because this is a very common problem for me, and I don't really like any of the solutions I've come up with.
I use c++ and C#, so solutions in either would be great.
but would also be interested in solutions in other languages. (though a design principle that avoids this in any language would be ideal)
If your language doesn't have this feature, write a function "forall," which takes two arguments: a list and a boolean-valued function which is to be true for all elements of the list. Then you only have to write it once, and it matters very little how idiomatic it is.
The "forall" function looks exactly like your second code sample, except that now "collection" and "passesTest" are the arguments to that function.
Calling forall looks roughly like:
if (forall(myList,isGood)) {
which is readable.
As an added bonus, you could implement "exists" by calling "forall" on the negated boolean function, and negating its answer. That is, "exists x P(x)" is implemented as "not forall x not P(x)".
You can use Linq in .NET.
Here's an example in C#:
if(collection.All(item => PassesTest(item)))
{
// do my code
}
C++ and STL
if (std::all_of(collection.begin(), collection.end(), PassesTest))
{
/* passed code */
}
Ruby:
if collection.all? {|n| n.passes_test?}
# do something
end
Clojure:
(if (every? passes-test? collection)
; do something
)
Groovy:
if (!collection.find { !PassesTest(it) }) {
// do something
}
Scala:
def passesTest(i: Int) = i < 5 // example, would likely be idiomatically in-line
var seq = List(1,2,3,4);
seq.forall(passesTest) // => True
Most, if not all of the answers presented here are saying: higher-order constructs -- such as "passing functions" -- are really, really nice. If you are designing a language, don't forget about the last 60+ years of programming languages/designs.
Python (since v2.5):
if all(PassesTest(c) for c in collection):
do something
Notes:
we can iterate directly on collection, no need for an index and lookup
all() and any() builtin functions were added in Python 2.5
the argument to any(), i.e. PassesTest(c) for c in collection, is a generator expression. You could also view it as a list comprehension by adding brackets [(PassesTest(c) for c in collection]
... for c in collection causes iteration through a collection (or sequence/tuple/list). For a dict, use dict.keys(). For other datatypes, use the correct iterator method.

parallel.for question

I have a foreach loop that processes a few thousand xmlnodes from an xmlnodelist. I'm trying to integrate the Parallel.For options from .net 4 but I get an error that "No overload for method 'For' takes 3 arguments'. But every example I see so far has it written this way. Does someone know what I need to change to get this to compile? Thanks.
Parallel.For( 0, jobs.Count, i =>
{
//..do work here..
});
Now the only other question I have is how do I break out of the current iteration? It doesn't seem to like my continue statements.
How exactly do you expect continue statements to work when each iteration can be running concurrently? You should read How to: Stop or Break from a Parallel.For Loop on the MSDN for more information about this and take a look at the Stop and Break methods.
Now the only other question I have is how do I break out of the
current iteration? It doesn't seem to like my continue statements.
Instead of continue, use break. Rather than:
for (int i = 1; i < jobs.Count; i++) {
// Initialise iteration.
if (iterationInvalid) continue;
// Do work.
}
What you're essentially getting is:
for (int i = 1; i < jobs.Count; i++)
action(i);
void action (int i) {
// Initialise iteration.
if (iterationInvalid) break;
// Do work.
}

Resources