Arrays in Haxe with multiple types - haxe

In Javascript, it's possible to create an array with nested arrays inside it, in a single line of code. Is it possible to do the equivalent in Haxe?
var a = [
["This is a nested array"],
["This is another nested array"],
"This is not a nested array"
];

Yes, it is exactly the same syntax in this case and a should be typed as Array<Dynamic>.

One other approach for handling mixed-type arrays is to use enums. This will change the run time values, but give the compiler a way to define and handle the types that the array could contain.
https://haxe.org/manual/types-enum-using.html
This approach won't work if you can't control how the arrays are being filled.

Related

node-postgres how to escape parameters?

I know about parameterized query, but since I have to programmatically construct the where condition, taking count of the parameters and building the parameters array is a task much more complex than simply calling an escape function when required. So:
is there a parameters escape function in node-postgres?
Yes, since this PR there are client.escapeIdentifier and client.escapeLiteral (sadly still undocumented). However, it is not recommended to use them when you can use parameterised queries, and for dynamic WHERE condition you can easily construct a query object with text and values on the fly.

Ensure variable is a list

I often find myself in a situation where I have a variable that may or may not be a list of items that I want to iterate over. If it's not a list I'll make a list out of it so I can still iterate over it. That way I don't have to write the code inside the loop twice.
def dispatch(stuff):
if type(stuff) is list:
stuff = [stuff]
for item in stuff:
# this could be several lines of code
do_something_with(item)
What I don't like about this is (1) the two extra lines (2) the type checking which is generally discouraged (3) besides I really should be checking if stuff is an iterable because it could as well be a tuple, but then it gets even more complicated. The point is, any robust solution I can think of involves an unpleasant amount of boilerplate code.
You cannot ensure stuff is a list by writing for item in [stuff] because it will make a nested list if stuff is already a list, and not iterate over the items in stuff. And you can't do for item in list(stuff) either because the constructor of list throws an error if stuff is not an iterable.
So the question I'd like to ask: is there anything obvious I've missed to the effect of ensurelist(stuff), and if not, can you think of a reason why such functionality is not made easily accessible by the language?
Edit:
In particular, I wonder why list(x) doesn't allow x to be non-iterable, simply returning a list with x as a single item.
Consider the example of the classes defined in the io module, which provide separate write and writelines methods for writing a single line and writing a list of lines. Provide separate functions that do different things. (One can even use the other.)
def dispatch(stuff):
do_something_with(item)
def dispatch_all(stuff):
for item in stuff:
dispatch(item)
The caller will have an easier time deciding whether dispatch or dispatch_all is the correct function to use than your do-it-all function will have deciding whether it needs to iterate over its argument or not.

How to store structs in a SharedArray in Julia?

So I have a situation where I want to store a large array of data as a shared array so I can do some parallelism on the data. However, the arrays is an array of types. When I try to initialize it as SharedArray though, I get an error saying that it must be a bit type. My question is how can I have a normal struct be considered a bit type in Julia so that it may be used by SharedArray? Or do I have to do some weird conversion where I convert my object to an string/integer in array and the deserialize it in each child process?
Here is the type I am trying store
type Rating
user::Int32
item::Int32
value::Float32
end
Now I, I know I could try to break this up into parrallel arrays, but that would require that rewrite large portions of the code and I'd rather not do that. As such, is there any other way I can store this struct or in an SharedArray?
If you make it an immutable, then you should be able to store it in the SharedArray.
immutable Rating
user::Int32
item::Int32
value::Float32
end
makes a "plain data" immutable structure, i.e.,
one for which isbits function returns true.

Data Structure to use instead of hash_map

I want to make an array containing three wide character arrays such that one of them is the key.
"LPWCH,LPWCH,LPWCH" was not able to use the greater than/lesser than symbols since it thinks it is a tag
Hash_map only lets me use a pair. wKey and the element associated with it. Is there another data structure that lets me use this?
This set will be updated by different threads almost simultaneously. And thats the reason why I don't want to use a class or another struct to define the remaining two wide character arrays.
You can use LPWCH as a key and std::pair<LPWCH, LPWCH> as an element.
Using any of LP-typedefs is not good. You would only be comparing the points, and not strings.
LPWCH is nothing but a WCHAR* which can be drilled down to void*. When you compare two pointers, you are comparing where they are pointing, and not what they are pointing.
You either need to have another comparer attached to your map/hash_map, or use actual string datatype (like std::string, CString)

Tuples in .net 4.0.When Should I use them

I have come across Tuples in net 4.0. I have seen few example on msdn,however it's still not clear to me about the purpose of it and when to use them.
Is it the idea that if i want to create a collections of mix types I should use a tuple?
Any clear examples out there I can relate to?
When did you last use them?
Thanks for any suggestions
Tuples are just used on the coding process by a developer. If you want to return two informations instead of one, then you can use a Tuple for fast coding, but I recoment you make yourself a type that will contain both properties, with appropriate naming, and documentation.
Tuples are not used to mix types as you imagine. Tuples are used to make compositions of other types. e.g. a type that holds both an int and a string can be represented by a tuple: Tuple<int,string>.
Tuples exists in a lot of sizes, not only two.
I don't recommend using tuples in your final code, since their meaning is not clear.
Tuples can be used as multipart keys for dictionaries or grouping statements because being value types they understand equality. Avoid using them to move data around because they have poor language support in C# and named values (classes, structs) are better (simpler) than ordered values (tuples).

Resources