What's the usage of Easymock.and(int, int)? - easymock

EasyMock has predefined argument matcher and(X first, X second)
Document says:
Matches if the matchers used in first and second both match. Available for all primitive types and for objects.
But I don't understand what's the use case for the numberical type and matcher. For example, and(int first, int second), it's meaningless if first not equals to second, how can an integer equals both first and second while first and second are different value?

The point is to use other matchers inside the and. Like and(lt(8), gt(4))

Related

Trying to create RegEx expression for string starting with a number and them matching a certain string inside?

I am trying to create a regular expression that starts with a number but then matches de or ba followed by 3 to digits right after it. What it should match is:
1Ghde345c
22Zui2ba777#
What I have so far is:
/^[0-9]?(be|de)*\d{3,5}/gm
But it doesn't seem to work. Is there something I am missing?
The provided regular expression currently requires that (be|de) immediately follow [0-9] at the beginning of the string:
^[0-9]?(be|de)*\d{3,5}
Based on the provided test strings, you instead want to match (be|de) followed by 3-5 digits at any position in the string, regardless of whether it immediately follows the digit at the start of the string. One way to achieve this is with the following regular expression:
^[0-9].*(be|de)\d{3,5}
^[0-9].* is used instead of ^[0-9]? for two reasons:
[0-9] is now a requirement instead of optional.
.* matches any character zero or more times, following the initial [0-9].
(be|de)* has been replaced with (be|de) to enforce only a single match, but can be reverted if sequential matches of be or de are allowed.
Example using regex101

How to Compare two arrays and its elements in visual foxpro?

I Am having two arrays named atest and NEWARRAY,I have tried to compare the elements of two arrays with simple if()and this is comparing only the first element of an array , how to compare all the array values at once,here's my code
IF (Alltrim(atest)== Alltrim(NEWARRAY))
Messagebox('Success',64,'Status')
Else
Messagebox('MisMatch',16,'Status')
ENDIF
Fox has a few functions that operate on whole arrays - like acopy, ascan and asort - but there is no built-in function that compares whole arrays. So you'll have to do the comparison element per element, for example with a for loop.
And yes, if you use an array name as an expression - including passing it by value - then you'll get the value of the first array element instead. There is one exception, though: when you pass an array to a built-in function in a place where an array parameter is expected then the compiler will automatically emit a reference token under the hood in order to arrange pass-by-reference instead of pass-by-value.
So, if you have a user-defined function f() to which you want to pass an array a then you need to call it like this: f(#m.a) but you can call built-in functions taking arrays like this: alen(a) (since the m. can be left off in this situation as well). In fact, Fox would complain if you coded something like alen(#m.a) or alen(#a), and older Foxen could even crash in such situations.
Conversely, if an array is the target of an assignment like a = 42 or store 42 to a then the value will be assigned to all array elements. This is convenient for initialising arrays to something like 0, '' or .null..
Hence, if you have two arrays a and b then a = b will assign the first value of b to all elements of a, and if a == b will compare the respective first cells only.
Sidenote: should you ever have to compare records from tables with equal or equivalent structure then you should remember to look up compobj(). It does for objects and scatter records what Fox won't do for arrays: it compares them whole-sale. That is, it compares the values of properties with matching names and tells you if there's a mismatch, and it does so much faster than hand-crafted code could do it.
Theoretically you could gather an array into a table/cursor record and then use scatter name Walther to produce a scatter record, which could then be compared to a scatter record named Herbert that was produced in a similar fashion from the contents of the other array: compobj(m.Walther, m.Herbert) would tell you whether the original arrays were equal or not. However, I'd be hard pressed to imagine circumstances where one might use something like that in production code...
You could create a simple procedure like this for comparison:
Procedure CompareArrays(ta1, ta2)
If Alen(ta1) != Alen(ta2)
Return .F.
EndIf
Local ix
For ix=1 to Alen(ta1)
If (Type('ta1[m.ix]') != Type('ta2[m.ix]') or ta1[m.ix] != ta2[m.ix])
Return .F.
endif
endfor
endproc
And pass your arrays by reference. ie:
isIdentical = CompareArrays(#laArr1, #laArr2)
If array members could hold objects, you should use compobj for comparison of array elements.

Why required "=" before ANY function with array as param, in postgres procedure?

I was answering a postgres question yesterday, and also came across a postgres thread (here) where they describe the following error:
ERROR: operator does not exist: text = text[]
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.
The error seems to appear whenever an ARRAY string type is fed to ANY without using = ANY. This seems completely strange since based on language, logic, and sql conventions, usually you have (e.g. IN):
variable FUNCTION(set)
instead of.
variable = FUNCTION(set) , unless ofcourse operator is a summation/count operation returning one result :)
It would make more senseto have variable ANY(Set/Array) instead of variable=ANY(Set/Array). Similar example is the IN function.
Can anyone explain what is going on here?
IN (...) is basically equivalent to = ANY (ARRAY[...])
Crucially, ANY is not a function. It's syntax defined by the SQL standard, and is no more a function than GROUP BY or the OVER clause in a window function.
The reason that = is required before ANY is that ANY can apply to other operators too. What it means is "Test the operator to the left against every element in the array on the right, and return true if the test is true for at least one element."
You can use > ANY (ARRAY[...]) or whatever. It's a general purpose operator that isn't restricted to =. Notably useful for LIKE ANY (albeit with somewhat bad performance).
There is ALL too, which does much the same thing but returns true only if all results are true.

Apply slicing to array valued function in Fortran [duplicate]

How does one access an element of an array that is returned from a function? For example, shape() returns an array of integers. How does one compare an element of that array to an integer? The following does not compile:
integer :: a
integer, dimension(5) :: b
a = 5
if (a .eq. shape(b)) then
print *, 'equal'
end if
The error is:
if (a .eq. shape(c)) then
1
Error: IF clause at (1) requires a scalar LOGICAL expression
I understand that this is because shape(c) returns an array. However, accessing an element of the array does not appear to be possible like so: shape(c)(1)
Now if I add these two lines:
integer, dimension(1) :: c
c = shape(b)
...and change the if clause to this:
if (a .eq. c(1)) then
... then it works. But do I really have to declare an extra array variable to hold the return value of shape(), or is there some other way to do it?
Further to the answers that deal with SHAPE and logical expressions etc, the general answer to your question "How does one access an element of an array that is returned from a function?" is
you assign the expression that has the function reference to an array variable, and then index that array variable.
you use the expression that has the function reference as an actual argument to a procedure that takes a dummy array argument, and does the indexing for you.
Consequently, the general answer to your last questions "But do I really have to declare an extra array variable to hold the return value of shape(), or is there some other way to do it?" is "Yes, you do need to declare another array variable" and hence "No, there is no other way".
(Note that reasonable optimising compilers will avoid the need for any additional memory operations/allocations etc once they have the result of the array function, it's really just a syntax issue.)
The rationale for this particular aspect of language design is sometimes ascribed to a need to avoid syntax ambiguity and confusion for array function results that are of character type (they could potentially be indexed and/or substringed - how do you tell what was intended?). Others think it was done this way just to annoy C programmers.
Instead of using shape(array), I would use size(array).
Note that this will return an integer indicating how many elements there are in ALL dimensions, unless you specify the DIM attribute, in which case it will return only the number of elements in the specified dimension.
Take a look at the gfortran documentation:
http://gcc.gnu.org/onlinedocs/gfortran/SIZE.html.
Also, look up lbound and ubound.
Note that the expression
a == shape(b)
returns a rank-1 array of logicals and the if statement requires that the condition reduce to a scalar logical expression. You could reduce the rank-1 array to a scalar like this:
if (all(a == shape(b)))
This is certainly not a general replacement for the syntactically-invalid application of array indexing to an array-returning function such as shape(b)(1).
It is possible even without the intermediate variable using ASSOCIATE:
real c(3,3)
associate (x=>shape(c))
print *,x(1),x(2)
end associate
end

Confusion with "..." operator in golang

What is the difference between the following two syntaxes in go?
x := [...]int{ 1:1, 2:2 }
x := []int{ 1:1, 2:2 }
Go's document says "The notation ... specifies an array length equal to the maximum element index plus one". But both the above syntaxes gives same lenght (3).
Is there a name for this operator "..."?
Didn't find a way to search this operator in google.
The first line creates an array using an array literal, its length computed automatically by the compiler. It is detailed in the Composite literals section of the Language Specification.
The notation ... specifies an array length equal to the maximum element index plus one.
Note: this is not to be confused with the ... used to specify variadic parameters or to pass slices as their values. It is detailed in the Function types section of the spec.
The second line uses a slice literal and will result in a slice. Note that under the hood an array will also be created, but that is opaque.

Resources