Can i put a range for my integer paramater - checker-framework

Can i put a range for an integer parameter to ensure integer value to be between a given range
method(int #Range(0,9) i)
which literally checks if parameter i is between those given range?

If you want compile-time checking, you can use the Checker Framework's Index Checker or Constant Value Checker and write an #IntRange annotation:
method(int #IntRange(0,9) i) { ... }

Related

Rust negative for loops?

Simple question, is it possible to do a for loop on a range of negative values? Specifically starting at -1 and iterating down to some negative value. for i in -1..x apparently isn't kosher.
Other than the ugly solution of iterating on the absolute values and then applying the sign within the loop.
The problem isn't in the negative numbers, the problem is in the reverse range. If we assume that x < -1 then you have a range (a,b) with a > b.
The default step of a rust range in a for loop is +1, so the loop never executes. If you write it out, C++ style, you'd be saying
for (i=-1; i < x; i++) { ... }
So what you want to do is reverse the range:
for i in (x..=-1).rev()
EDIT: Because (x..-1) does not include -1, we need to change it to (x..=-1). In that case the range will include both x and 1. If we don't want that, we have to fiddle with the x bound as well, e.g. ((x+1)..=-1).rev
The standard Range type (what you get with a..b) is a half-open increasing range. If you have a range where a >= b, then the range is empty.
If you want to iterate a range in reverse (decreasing) order, then you need to use Iterator::rev to reverse the range, which is enabled by range being a DoubleEndedIterator and providing the reverse iteration next_back.
So in this case, you'd iterate over ((x + 1)..0).rev().
Just as a fun side note, you can also get basically any iterator you want out of iter::successors, by just providing a fn next(&T) -> Option<T>; in this case, |x| x - 1, if that's clearer in a specific use case than reversing an increasing range.

Groovy using a different value from what was initialized

In my JMeter script i have defined a User Defined Variable i whose value is set to 1. Then i created a JSR22 pre-processor for my HTTP sample that just increments the value of i using the below code.
log.info(vars.get("i"));
Integer intI = vars.get("i");
intI = intI + 1;
vars.put("i", intI.toString());
log.info(vars.get("i"))
The problem seems to be that instead of picking up 1 as the initial value of i it is picking 49 and increment it to 50.
I was able to fix it by changing
Integer intId = vars.get("id")
to
Integer intId = vars.get("id").toInteger();
But i am curious to know what could be causing this.
As requested, the conclusion from the comments as answer:
vars.get() returns a String which, when assigned to an Integer gets implicitly converted to the corresponding ASCII value (if the String is exactly 1 character, otherwise it will cause a runtime exception).
This can be observed in the snippet:
def v = "1"; Integer intId = v; println intId -> prints 49

How to implement an ISBETWEEN-like function for arrays of values without having to calculate the array more than once?

An ISBETWEEN function tests whether a value falls between a lower bound and a higher bound. With no native ISBETWEEN function in Excel, the value under test must be compared twice; first with '>' and then with '<' (or '>=' and '<=' for an ISBETWEEN test that is inclusive of the bounds.)
Comparing the value twice means having to calculate it twice, and this can be extremely expensive when that value is an array. With array functions being somewhat cryptic even at the best of times, doubling up on such a calculation also sends the readability of the function plummeting.
My question is whether anyone knows of a technique that delivers ISBETWEEN-like functionality for an array of values without the double calculation of that array? My preference is to do this with native Excel functionality but, if anyone has some great VBA, that would be good too.
Many thanks for your time!
Will
Building from my comment above: This doesn't provide a 100% answer to your question, but since it was pretty generic, I think this is the closest to an answer that I can get.
Imagine a spreadsheet set up like:
We can get a count of all the values that are between 3 and 5 using CTE/Array formula:
={SUM(IF(LOOKUP(A1:A6,{3,"B";6,"C"})="B",1,0))}
Results:
5
That's a pretty round-about way of doing this, but the array of A1:A6 only needs to be referenced once. Which is pretty cool.
Note that the squirrely brackets in the above formula aren't actually entered, but are placed by excel when you enter the array formula to indicate that it's an array formula... you probably already know that though if you've read this far.
So I've been able to develop a piece of VBA, based on the idea here.
Dim vValueArg As Variant, vLowerArg As Variant, vUpperArg As Variant, vTestLower As Variant, vTestUpper As Variant
Function ISBETWEEN(vValue As Variant, vLower As Variant, vUpper As Variant, Optional bInc As Boolean = True) As Variant
vValueArg = vValue
vLowerArg = vLower
vUpperArg = vUpper
If bInc Then
vTestLower = [GetValue() >= GetLower()]
vTestUpper = [GetValue() <= GetUpper()]
Else
vTestLower = [GetValue() > GetLower()]
vTestUpper = [GetValue() < GetUpper()]
End If
ISBETWEEN = [IF((GetTestLower() * GetTestUpper()) = 1, TRUE, FALSE)]
End Function
Function GetValue() As Variant
GetValue = vValueArg
End Function
Function GetLower() As Variant
GetLower = vLowerArg
End Function
Function GetUpper() As Variant
GetUpper = vUpperArg
End Function
Function GetTestLower() As Variant
GetTestLower = vTestLower
End Function
Function GetTestUpper() As Variant
GetTestUpper = vTestUpper
End Function
The first argument can be a single value, range or array. If a single value, then the next two arguments must also be single values (but this kinda defeats the purpose of the code!)
The second and third arguments can also be a single value, range or array. If a range consisting of multiple cells or array of multiple values, then the dimensions of these arguments must match those of the first argument. (NB - I have NOT tested the code with 2 dimensional ranges or arrays!)
The final, optional, argument determines whether the ISBETWEEN test is performed including or excluding the bounds. TRUE = include bounds; i.e. arg2 <= arg1 <= arg3 (the default, and can therefore be omitted). FALSE = exclude bounds; i.e. arg2 < arg1 < arg3.
While this might not be the prettiest code in the world, it is compact, fast (no loops) and copes with ranges and arrays of any size.
Hope some of you find this useful! :)

Dynamically Size the Upper Bound of a VBA User Defined Type

I'm working with a two-dimensional array that stores a location and date under each iteration. I dynamically set the upper bound of that dimension using a counter variable ("i"), i.e.:
ReDim arrLocDt(1 To i, 0 To 1)
When debugging in the Watch window, I always found it cumbersome that I couldn't name the individual items, for example, instead of
arrLocDt(1,0) and arrLocDt(1,1)
I would like to see:
arrLocDt(1,Location), arrLocDt(1,Date), arrLocDt(2,Location), arrLocDt(2,Date), etc.
Recently, I learned about User-Define Types which would allow for just such labeling of items. Thus I tried the following:
1. Defined a new UDT
Public Type MyType
strLocation As String
strDate As String
End Type
2. Tried the following definition with a dynamic sizing of the upper bound
Dim test(1 to i) As MyType
The above failed because I got the error Constant Expression Required.
Please advise how I can dynamically size the upper bound of an UDT, and if that's not possible, an alternative that would still allow me to label my items.
It should be as easy as:
Dim arrLocDt() As MyType
ReDim arrLocDt(1 To x) As MyType
(The first Dim is actually not required. See this post for details.)
The reason you're running into problems is that you initialize (Dim) an array of any type with a variable size. E.g., this is illegal too:
Dim myStringArray(x) As String
...it's not just a UDT thing. Dynamically sizing or resizing an array can only be done at run-time with a ReDim.
That said, you could do this:
Dim arrLocDt(1 to 10) As MyType
And then later, when you need a different size, do this:
ReDim arrLocDt(1 To x) As MyType
...but you cannot simply start out with this:
Dim arrLocDt(1 to x) As MyType
So, again, if you don't know what the initial size is going to be, just declare the array without any bounds... Dim arrLocDt() As MyType and then ReDim as soon as you know the size and you need to use it.
Also, cool tip, if you need to resize a second time, and you want to keep what is in there, use the Preserve Key Word, like this:
ReDim Preserve arrLocDt(1 to y) As MyType 'resizing, but keeping my stuff

VBA type question beginner

I'm a newbie and I'm trying to learn VBA and i got a question about this code:
(Sorry for any mistake in English, I'm not a native speaker)
Option Explicit
Type rPaciente 'matriz de pacientes
CodPaciente As Long
PriorPaciente As Long
IntvChegDistr As String
Par1 As Double
Par2 As Double
Par3 As Double
Par4 As Double
IntvCheg As Double
InstCheg As Double
End Type
Public mPaciente() As rPaciente
Public glQtdPaciente As Long
What are these "things" between Type and End Type, are they cells of the range of my worksheet, cause I got columns like Par1,Par2,...,Par4 in my worksheet but I don't got columns named IntvCheg neither InstCheg. And what does the command Public someting() As otherthing means?
Thanks in advance for your help.
"Things" between Type and End Type are members of the rPaciente type. They are not mapped to anything, they only describe the set of data this type is meant to contain.
Probably there is a routine somewhere that maps the cells on your sheet to members of this type by creating an instance of the type and copying values from the sheet to the member variables of the instance.
Public someting() As otherthing is an array of type otherthing. The declaration does not include the dimensions for the array, so before it can be used, you must ReDim it to something:
ReDim someting(1 to 10)
The Type declaration simply declare a Visual Basic Type structure. Is something like (really simplified view) a struct in c++ or a class without methods in any labguage.
The instruction
Public mPaciente() As rPaciente
declare a dinamyc array of rPaciente objects that probably will be filled with every row inside your excel file.
The instruction
Public glQtdPaciente As Long
declare a variable of type Long with the name glQtdPaciente

Resources