Torch: Index operator with string returning the original array no matter what the input string - torch

Perhaps not of great practical concern but still tickles my curiosity.
When looking at the index operator syntax for torch, I was led to try v[{''}], which to my surprise returned the original table. And so did any arbitrary input string, such as v[{'abc'}] and v[{'what ever str you can think of'}].
Is this an intended behavior? If so, for what purpose?

Related

Does Kotlin have pointers?

Does Kotlin have pointers?
If yes,
How to increment a Pointer?
How to decrement a Pointer?
How to do Pointer Comparisons?
It has references, and it doesn't support pointer arithmetic (so you can't increment or decrement).
Note that the only thing that "having pointers" allows you is the ability to create a pointer and to dereference it.
The closest thing to a "pointer comparison" is referential equality, which is performed with the === operator.
There is no pointers in Kotlin for low-level processing as C.
However, it's possible emulate pointers in high-level programming.
For low-level programming it is necessary using special system APIs to simulate arrays in memories, that exists in Windows, Linux, etc. Read about memory mapped files here and here. Java has library to read and write directly in memory.
Single types (numeric, string and boolean) are values, however, other types are references (high level pointers) in Kotlin, that one can compare, assign, etc.
If one needs increment or decrement pointers, just encapsulate the desired data package into a array
For simulate pointers to simple values it just wrap the value in a class:
data class pStr ( // Pointer to a String
var s:String=""
)
fun main() {
var st=pStr("banana")
var tt=st
tt.s = "melon"
println(st.s) // display "melon"
var s:String = "banana"
var t:String = s
t.s = "melon"
println(s.s) // display "banana"
}
I found this question while googling over some interesting code I found and thought that I would contribute my own proverbial "two cents". So Kotlin does have an operator which might be confused as a pointer, based on syntax, the spread operator. The spread operator is often used to pass an array as a vararg parameter.
For example, one might see something like the following line of code which looks suspiciously like the use of a pointer:
val process = ProcessBuilder(*args.toTypedArray()).start()
This line isn't calling the toTypedArray() method on a pointer to the args array, as you might expect if you come from a C/C++ background like me. Rather, this code is actually just calling the toTypedArray() method on the args array (as one would expect) and then passing the elements of the array as an arbitrary number of varargs arguments. Without the spread operator (i.e. *), a single argument would be passed, which would be the typed args array, itself.
That's the key difference: the spread operator enables the developer to pass the elements of the array as a list of varargs as opposed to passing a pointer to the array, itself, as a single argument.
I hope that helps.

What is the correct way to handle pointer structs in loops in Go?

In this example on the go playground, you can see that looping over a list of objects and putting them into an array of pointer structs ends up putting the same entry into the array multiple times.
http://play.golang.org/p/rICA21kFWL
One possible solution to the issue is to make a new string and sprint the string out of the looped string into the new string. This seems silly though.
What is the idiomatically correct way to handle this problem?
In case I understood correctly and you simply want an array of pointers pointing to the respective string in the original array, you can always do this
# choose correct size from beginning to avoid costly resize
o := make([]*string, len(f))
# iterate only over index
for i := range f {
o[i] = &f[i].username
}
Here's your go playground with the changes sketched out above.

XQuery idiom return()

Why do I see return() at the end of an XQuery query?
e.g.
let $updateIssues:=
for $issue in fn:collection("InProgress_Audit")/ProductIssue
return local:UpdateIssue($issue)
return ()
It's not entirely clear what your question is. Given the way you've formulated it, possible answers include:
You see it because it's there.
You see it because someone typed it when they wrote the query.
Perhaps you mean 'why is it there?' Possible answers include:
It's there because without a return clause the FLWOR expression begun by the let clause is not complete.
It's there because the author of this query has a quirky sense of humor, and thought a four-line query was an amusing way to write an expression denoting the empty sequence (which can be more concisely written ()).
It's there because the author of the query wants you as a reader to know he'd really rather be writing C or Java or, really, any other programming language.
It's there because the author of the query finds it amusing that the return keyword in XQuery can be written to look like a function call. (The author may enjoy hazing newbies and practical jokes, too; approach with caution.)
Perhaps you mean "what does this query mean and how does it mean it?" Possible answers include:
The query returns the empty sequence.
The query takes the form of a FLWOR expression which defines a binding for the variable updateIssues (whose value is given by a nested FLWOR expression), and then returns () (that is, the empty sequence). Since nothing in the return clause depends on the value of updateIssues, there is no need for an XQuery engine to evaluate the expression used to specify the value (but there is also no guarantee that it won't do so -- processors are allowed to be smart about optimization but not required to be).
In your example, the return () expression is the body of your let expression.
The assignment to $updateIssues is walking through a bunch of nodes, returning the value from a call to local:UpdateIssue(), and the sequence of those return values is being used as the variable value.
But, then, nothing is done with the variable and the function simply returns nothing to its caller.
I would be worried that an optimizing processor may choose to throw out the assignment because the variable being assigned isn't being used anywhere, but the processor you are using is likely not doing that if you are getting the update results you are expecting.
So, all that last line is doing is always returning an empty sequence as the value of the function when called.
If I get your question right you are asking about return ().
return () means that you are returning a blank so in short you just want to assign the global variable to something and do not want to return anything from the function.
Else if you want to know the details on Return expression. It is a Part of XQuery FLWOR Expressions.
Being a functional language XQuery works on Functions.
For Loop, Let Loop, Where, Order By (FLWO) are all internally (inside XQuery Engine) acting as functions. Thus they all require a return after call to specify what needs to be the output from them.
If you require more information on XQuery functions you may refer the offical documentation.

VBScipt: Call builtin functions shadowed by global variables

VBScript on ASP Classic contains an "int" function. (It rounds numbers towards -∞.) Suppose that some excessively "clever" coder has created a global variable named "int". Is there any way to get at the original function? I've tried all manner of workarounds with scoping and dodgy execs, but no dice. I suspect that it is impossible, but I'm hoping that someone will know more about it than I do.
EDIT: Thanks for the responses. Since y'all asked, the global variable, called "Int" (though unfortunately, vbscript is not case-sensitive), is a factory for a class similar to Java's Integer. The default property is essentially a one-arg constructor; i.e. "Int(42)" yields a new IntClass object holding 42. The default property of IntClass in turn simply returns the raw number.
The creator was trying to work around the lack of proper namespaces and static methods, and the solution's actually pretty seamless. Pass in an IntClass where an int is expected and it will automatically trigger the default property. I'm trying to patch the last remaining seam: that external code calling "int" will not round properly (because the constructor uses CLng).
Not that I know of, getref only works on custom functions not on build-ins. I would suggest renaming the custom'int' function and update all references to this custom ones. You can use the search function visual studio (express) or any other tool of your liking for this. Shouldn't be to much work.
I didn't think reserved words would be allowed for function names or variables.
Duncanson's right. Do the pain and rename int. Chances are there are worse things going on than just this.
(why would someone make a global variable named int... that's going to take some thinking)
Or you can use CInt instead on Int
response.write trim(cint(3.14)) + "<br>"
Wrong!!
See NobodyMan comments

Is the QHash::contains method case-sensitive or case-incensitive?

I have a QHash<QString,QString>.
I use the following expression to check whether a value is there in the QHash::keys() or not.
//programme
QHash<QString,QString> samplehash;
QString value = "somevalue";
if(samplehash.contains(value)) // Condition - 1
{
//some code
}
Sometimes the above conditions matches, sometimes not for the same letters of different case. Is the QHash::contains method case-sensitive?
QHash.contains() is case sensitive as John T mentioned. Without the code there is not much to figure out. You can imagine it doing a == between the keys.
Please do not forget that accessing a non existent element via [] will create an empty entry in the hash, this might be what causes your bug. contains does not insert an entry into the hash, neither does value
It is case sensitive. The common practice for handling data from multiple sources and comparing it is to convert it all to the same format first, which usually involves making everything lowercase prior to comparison.
This is a common practice, especially on websites for handling logins, or user input in applications to avoid the old 'PEBKAC' situations and make it easier for users.
One solution could be to subclass QString and override the operator== to do the comparison with case-insensitive mode, using QString::compare.
But the solution of John T is definitely better if it fits your constraints.

Resources