Filling eltType with nil values - constructor

So I have a chapel issue i can't seem to figure out. I have a queue that one can set size. The only thing is is that it's setting size and filling the queue with a bunch of 0s (which make's sense). I'm trying to fill the queue with null rather than numerical values so later on when I work on the add method I can check if queue is null. I have attached an image of how everything is set up. Let me know if you guys have any guidance or ideas.
The error that i'm getting is:
error: type mismatch in assignment from string to int(64)
I must be doing it the wrong way here.

The error you are seeing is about the line:
elements[i] = 'nil';
'nil' is a string, not the nil value, which is written as just nil without any quotes. Assigning a string to a slot in an array of int(64) doesn't work, so the compiler issues an error.
In Chapel only classes can have a nil value though, so you'll need to use a different way to keep track of which positions in the elements array are filled.
One way to do that would be to add two new integers to your class that keep track of the first and last positions containing valid values. As you add values to the queue the last position increases, and as you remove values the first position increases. When either of those values passes the end of the array, it wraps around back to the front. If last ever catches first, then the array is full. If first ever catches last then the array is empty.
A few other things I think should be corrected in your code are:
use semaphore.chpl; Use statements work with module names, not filenames, so this should probably be use semaphore;.
If I'm understanding your intent here, this code is trying to set the size of the elements array to 5.
var elementsDomain: domain(1);
var elements: [elementsDomain] eltType = 5;
The array's domain controls the size of the array, so the way to set the array size is through the domain:
var elementsDomain: domain(1) = {0..#5};
var elements: [elementsDomain] eltType;
elementsDomain = (0..capacity - 1); is setting elementsDomain to a range literal value. This works since the domain is 1-dimensional, but to make it more clear, you can set it to a domain literal value instead: {0..capacity - 1}.

Related

Convert List<dynamic> to List<String>

I am getting data from server. The run runtimeType shows that they have type List.
Currently I am using cast<String>() to get List<String>.
But is it's only\right way?
var value = await http.get('http://127.0.0.1:5001/regions');
if(value.statusCode == 200) {
return jsonDecode(value.body)['data'].cast<String>();
}
There are multiple ways, depending on how soon you want an error if the list contains a non-string, and how you're going to use the list.
list.cast<String>() creates a lazy wrapper around the original list. It checks on each read that the value is actually a String. If you plan to read often, all that type checking might be expensive, and if you want an early error if the last element of the list is not a string, it won't do that for you.
List<String>.from(list) creates a new list of String and copies each element from list into the new list, checking along the way that it's actually a String. This approach errs early if a value isn't actually a string. After creation, there are no further type checks. On the other hand, creating a new list costs extra memory.
[for (var s in list) s as String],
[... list.cast<String>()],
<String>[for (var s in list) s],
<String>[... list] are all other ways to create a new list of strings. The last two relies on implicit downcast from dynamic, the first two uses explicit casts.
I recommend using list literals where possible. Here, I'd probably go for the smallest version <String>[...list], if you want a new list. Otherwise .cast<String>() is fine.

Make beam/dataflow Create.of(List<String>) node emit exactly the number of elements in the list

My beam/dataflow job starts with a single static entry passed in like this:
Pipeline p = Pipeline.create(options);
PCollection<String> initValue = p.apply(Create.of("MyStringValue"));
However when I run it (on DataflowRunner), the Create node produced by that statement emits multiple values. The longer I wait, the more times it emits the single value:
This doesn't appear to be an artefact as later in the pipeline I get duplicate/triplicate/.. elements. Beam also logs a warning:
Can't verify serialized elements of type BoundedSource have well defined equals method. This may produce incorrect results on some PipelineRunner
How do I make my Create.of with one value emit just one value to the pipeline?
Do I need to attach an equals method or point it towards the equals method for String values (if so, how)!?

Request.post arguments in python

Is there a way to determine the max value of an argument in a requests.post command to a website if we don't know the amount of data in the website's dataset? I'm trying to execute the following code to get specific information on all daycares from this website, but don't know the value of the last argument (length). Currently, I'm assuming this value is 20, but it is subject to change from time to time. How do I keep it open ended so I don't have to guess the max value for lenth? Code as follows:
data_requested = requests.post("https://data.nj.gov/views/INLINE/rows.json?"
"accessType=WEBSITE&method=getByIds&asHashes=true&start=0&length=20",
json=data)
njcc_data = data_requested.json()
Notice that this has nothing to do with requests.post - the range of values length can take is determined by the creator of that API and is an unknown quantity both to you and to requests.
You can try to reason about what possible values it could take, is it the length of a person? If yes, it's probably not going to be more than 250cm.
You can also use trial and error and see how high you can make it before the API endpoint gives back an error, but I guess this is what you were trying to avoid.
If length is the number of items returned (the length of the returned json array) then you could just try setting it to a high number like 1000 and see if you can get away with it.

Octave Cell Array

I have a nested cell array with a field for information and one for another cell.
How can I delete a cell using an auxiliary variable?
For example:
T = start of cell array
P = T
P = P{2} as many times as needed.
P{2} = []
The deletion on of the cell is not visible in the original list.
I don't think the code you've done does what you think it does.
Octave\Matlab do not implement the concept of pointers in its full meaning.
As beaker said, the assignment operation is copying, so P = T is another cell array. Actually, for performance reasons it remains the same array until you change contents of one of them. i.e. until P{2}={} at which point T={} and P={[1,1] = [](0x0) [1,2] = {}(0x0)}. (The P = P{2} doesn't even work by the way.)
That is okay, since Octave\Matlab do not intend to expose memory addressing and such. i.e. you cannot determine whether variables P and T are stored at the same address. The equality operator works with elements of arrays and does not work completely for cells.
Octave\Matlab are designed for number crunching.
Easy creation manipulation arbitrary datastructures is outside of their scope.
For creation of lists the following link might be of help, albeit the solution is pretty complex:
MATLAB linked list

In Ethereum Solidity, when changing an array length, I get "Value must be an lvalue". Why?

In Solidity, you can increase the size of an array to make room for a new member by using array.length++. But I'm getting an error:
Value must be an lvalue
You can resize a dynamic array in storage (i.e. an array declared at the contract level) with “arrayname.length = ;” But if you get the “lvalue” error, you are probably doing one of two things wrong.
You might be trying to resize an array in memory, or
You might be trying to resize a non-dynamic array.
int8[] memory somearray; // CASE 1
somearray.length++; // illegal
int8[5] somearray; // CASE 2
somearray.length++; // illegal
IMPORTANT NOTE: In Solidity, arrays are declared backwards from the way you’re probably used to declaring them. And if you have a >=2D array with some dynamic and some non-dynamic components, you may violate #2 and not understand why. Note also that arrays are accessed the “normal” way. Here's are some examples of this "backward" declaration paradigm in action:
int8[][5] somearray; // This is 5 dyn arrays, NOT a dyn array-of-arrays w/len=5
// so...
somearray[4]; // the last dynamic array
somearray[1][12]; // the 13th element of the second dynamic array
// thus...
somearray.length++; // illegal. This array has length 5. Always.
somearray[0].length++;// legal
Encountered same issue and what I had to was use the storage keyword since I was trying to modify a global storage array.
bytes32[] storage someArray = someGlobalStorageArray;