append item to dictionary list, kdb - kdb

I have a dictionary
q).test.dict:(`test1;`test2)!(1i;2i)
q).test.dict
test1| 1
test2| 2
and I need to append an item to one of the dictionary lists to get the following result:
q).test.dict
test1| 1
test2| 2 4i
However I am having trouble assigning to that dictionary list.
I have tried the following:
q).test.dict[`test2]:.test.dict[`test2],4i
'type
[0] .test.dict[`test2]:.test.dict[`test2],4i
And have tried other methods of assignment which also result in a type error.
I feel like im missing something quiet obvious here but cant seem to put my finger on it.

The issue is that you've initiated/defined the dictionary to have a uniform value (integer atoms) and so kdb expects/enforces the values to remain integer atoms. You can avoid this by creating a default entry (with say generic null ::) to force the value to be a mixed list. Then you can append
.test.dict:(`;`test1;`test2)!(::;1i;2i)

If you instead define your dictionary like this
.test.dict:(`test1;`test2)!(enlist 1i;enlist 2i)
It will work. The problem is, the value of your dictionary is a list of integers, not a list of lists of integers.

Related

kdb/q: apply the function, pass the return value to the function again, multiple rounds

I have a list of symbols, say
`A`B`C
. I have a table tab0; A function that takes in a table plus a string as arguments.
tab1: f[tab0;`A]
tab2: f[tab1;`B]
tab3: f[tab2;`C]
I only care about the final values. But my list of symbols can be long and can have variable length, so I don't want to hardcode above. How do I achieve it?
I think it has something to do with https://code.kx.com/q/ref/accumulators/ but I really struggle to figure out the syntax.
This is exactly the use case for the binary application of over (/) (https://code.kx.com/q/ref/accumulators/#binary-application)
So you should use:
f/[tab0;`A`B`C]

q/kdb - hitting 'constants error when adding new key-value assignments to a dictionary within a function

I am currently working on a script where within a function, key-value pairs are being added to a dictionary x - consider x as a single dictionary of different inputs used to query data, and different key-values are appended to this depending on certain conditions being fulfilled.
However, when I load in the script into my session with some new assignment logic added, I am hitting a 'constants error. This is despite all assignments being kept to this dictionary x. When these two new assignments within x are commented out, the script will load in successfully.
I know the 'constants error usually refers to the max number of constants within a certain scope being exceeded, but surely this shouldn't be happening when all assignment is happening within this dictionary x. Is there a way to get around this? What is causing this issue?
I think you are trying to do too much in one function. I think you are indexing or assigning values to the dictionary with too many constants. Below code will return the constants error:
dict:(10 + til 100)!til 100
value (raze -1_"{","dict[",/:(string[10+til 97],\:"];")),"}"
// with til 96
{dict[10];dict[11] ... dict[104]}
It's the code that is indexing the dictionary is causing the issue rather than the dictionary itself.

How to make a list contain ints and strings? A challenge using lists only

I am beginning in scala and I need to contain a bunch of ints, and something other than a number in a List.
For example List(4,null) or List(4,"STOPSTR").
How would I declare a variable of type List[Int or String] (Kind
of)?
Also, is it at all possible to declare a variable for a list that can
only contain a certain string. E.g. List[Int or String=="STOPSTR"]
and have it checked at compile time.
For the reasons of a challenge, I can only use Lists for this exercise, no classes, no maps, not even Arrays.
You can do it 2 different ways:
The first is using Any as the type parameter to List. The problem here though, is that it's not constrained to just Int and String types. You could add any other type to the list as well.
E.g.
List[Any](1, 2, "Three")
The second is to use Either. Either has a Left and a Right type that can be different. e.g.
List[Either[Int, String]](Left(1), Left(2), Right("Three"))

Matlab Coder throws mixed field type error in structure array while loading MAT file with coder.load

I'm working on a MATLAB Coder project where I want to load some constant values. After trying many possibilities, all unsuccessfully, I came up with the "coder.load" directive that loads variables from MAT files and assumes them as compile time constants in generated C-code.
This is the error that I get:
Found unsupported class for variable using function 'coder.load'.
Mixed field types in structure arrays are not supported. Type at
'ind_x.params(1).name' differed from type at 'ind_x.params(2).name'.
But the problem is that the "name" field of the "params" structure array has the same type for each array element. Indeed, trying it out on the command window gives me the same type:
>> class(ind_x.params(1).name)
ans =
char
>> class(ind_x.params(2).name)
ans =
char
There are other fields of the structure array that are of type "double", and one of type "bool", but the type doesen't change inside different array elements of the same field, that's why I don't understand the error.
Ok I think I found the answer to my question. The problem indeed was the character string length. If one of the fields of the structure array is of type "char", then it has to be of the same lenght for every array element.
That is, if you define
ind_x.params(1).name = 'john';
ind_x.params(2).name = 'harry';
It will throw an error if you try to load that structure with coder.load() because length(ind_x.params(1).name) is different from length(ind_x.params(2).name). A workaround could be to define a maximum length and add trailing spaces to the string.
This limitation may come from the constants definitions in C, but what I found messy is the misleading error message. Thanks anyway for the help!
EDIT : I realized that the above restriction for constant structure arrays is valid not only for type "char", but for every type! You can't have a field whose length is varying within different array elements.

In what order when add item to "map" in swift?

An example in swift tour in https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
The final result of occupations is :
["Kaylee": "Mechanic", "Jayne": "Public Relations", "Malcolm": "Captain"]
My question:
Is var occupations a Map?
In what order does new item be added?
The existing answers are highly likely to be confusing depending on what sort of programming background you come from.
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
Given that code snippet, the type of occupations is a Swift Dictionary. As the other answers point out, a dictionary, in Swift, is a collection of key-value pairs.
Other languages have a similar data structure, but may refer to it by a different name.
C++'s map type
.NET's Hashtable type
Java's HashMap type
Objective-C's NSDictionary type
The list could go on. But these all represent roughly the same data structure: a store of key & value pairs.
In Swift, dictionaries do not have an order. Any perceived ordering you notice from printing or iterating over items in a dictionary is just that--perceived. It should not be relied on.
If you need an ordered dictionary, you will need to implement it on your own. Although I'm sure someone has probably already implemented it and you can find it on github. A very simple implementation for an ordered pairs of data could simply involve an array of tuples, but you wouldn't be able to do the same key look up you can with dictionaries.
What is important here is that there is no defined order for data in a Swift dictionary, so any perceived ordering that is happening should not be relied on.
"occupations" is a dictionary (also known as a hash in some languages). It is key-value paired which means that the keys are in no particular order but each unique key is only tied to a single value (which can of course take the form of an array or another dictionary, but still counts as one value assigned to one key).
"Map" is a function that takes a list (such as the keys or values of "occupations") and returns an array of the original list with a function applied to it.
For example
(0..<4).map({$0 + 1})
Returns an array of [1, 2, 3, 4] because the $0 is Swift shorthand for "the value of the original array that am currently applying this function to.
As mentioned in the answers above, when an item is added to a dictionary its position is unimportant, as the dictionary is not by nature sorted like an array would be.
To turn Eric and Luk's comments into an official answer:
Your var 'occupations' is a Swift Dictionary object.
A Dictionary in Swift (and Objective-C as well) does not have any order at all. It is an "unordered collection."As Luk says, it's a key-value store. You save and retrieve values using a key.
Your questions:
Is var occupations a Map?
EDIT:
It's not called a Map in iOS, but it the equivalent of a map in other languages like C#, C++, and Java.
In what order does new item be added?
Dictionaries do no have any order whatsoever.