I have a variable that consists structs. These structs can have CFBoolean variables, more structs, and other variables. In the beginning this was nested two levels deep. We are now moving up to four levels. I don't like my current approach. I could also imagine five levels happening. I have no control over the external system that needs this data. So I am looking for a more general approach.
function toJavaBoolean(any data){
//for now, assume it's a struct to DBO conversion
data.each(function(key, value) {
if (getMetadata(data[key]).getName() == 'coldfusion.runtime.CFBoolean') {
data[key] = javacast("boolean", data[key]);
}
if (isStruct(data[key])) {
data2 = data[key];
data2.each(function(key, value) {
if (getMetadata(data2[key]).getName() == 'coldfusion.runtime.CFBoolean') {
data2[key] = javacast("boolean", data2[key]);
}
if (isStruct(data2[key])) {
data3 = data2[key];
data3.each(function(key, value) {
if (getMetadata(data3[key]).getName() == 'coldfusion.runtime.CFBoolean') {
data3[key] = javacast("boolean", data3[key]);
}
if (isStruct(data3[key])) {
data4 = data3[key];
data4.each(function(key, value) {
if (getMetadata(data4[key]).getName() == 'coldfusion.runtime.CFBoolean') {
data4[key] = javacast("boolean", data4[key]);
}
});
}
});
}
});
}
});
You can use recursion like so...
function toJavaBoolean(any data){
data.each(function(key, value) {
if (getMetadata(data[key]).getName() == 'coldfusion.runtime.CFBoolean') {
data[key] = javacast("boolean", data[key]);
}
else if (isStruct(data[key]))
data[key] = toJavaBoolean(data[key]);
}
return data;
}
There are some non-recursive approaches that may be faster for larger sizes or great depths.
Related
I have this simple bit of code:
void test(int *testvec, int *testlen, int *testres) {
for (int i = 0; i < *testlen; i++){
testres[i]=0;
if (testvec[i] < 0){
testres[i] = -1;}
else if (testvec[i] == 0){
testres[i] = 0;}
else {testres[i] = 1;}
}
}
}
When I pass
testresult = function (testvec, len){
signC = .C("signC", as.integer(testvec), as.integer(length(testvec)), as.integer(testvector("integer"), length(testvec)))
return (signC)
}
testvec = c(1,2,-3,0,2)
It seems that nothing has been processed.
I am pretty new to C and I can't understand how to assign a value to an array dynamically within the contest of .C interface within R.
Thank you for your help.
.C interface functions can’t return values. If you want to modify values in C, you need to pass a vector of sufficient size into the function, and write into that vector:
sign = function (testvec) {
sign_c = integer(length(testvec))
.C("signC", as.integer(testvec), length(testvec), sign_c)
sign_c
}
So, I wanted to know if there is easier way to do than using (r.getRow() ==x) and do multiple lines for rows?
It's only suppose to stamp on specific rows and in original sheets there is over 50 subjects and they all have their own rows for stamp.
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if (s.getName() == "spreadsheet1") {
var r = s.getActiveCell();
if (r.getRow() == 3) {//kuitattavat rivit
var nextCell = r.offset (1, 0);
if (nextCell.getValue() === "") {//tarkistaa solun onko tyhjä}
nextCell.setValue(new Date()).setNumberFormat("HH:mm");
}
}
else {
var r = s.getActiveCell();
if (r.getRow() == 5) {//kuitattavat rivit
var nextCell = r.offset (1, 0);
if (nextCell.getValue() === "") {//tarkistaa solun onko tyhjä}
nextCell.setValue(new Date()).setNumberFormat("HH:mm");
}
}
}
else {
var r = s.getActiveCell();
if (r.getRow() == 7) {//kuitattavat rivit
var nextCell = r.offset (1, 0);
if (nextCell.getValue() === "") {//tarkistaa solun onko tyhjä}
nextCell.setValue(new Date()).setNumberFormat("HH:mm");
}
}
}
}
}
Since you are trying to use an onEdit(e) trigger anyway, you can just reference the sheet name and range of e and check the row number from there using logical OR operator ||.
function onEdit(e) {
var s = e.range.getSheet();
if (s.getName() == "spreadsheet1") {
var row = e.range.getRow();
if (row == 3 || row == 5 || row == 7) {
var nextCell = e.range.offset(1, 0);
if (nextCell.getValue() === "") {
nextCell.setValue(new Date()).setNumberFormat("HH:mm");
}
}
}
}
Sample:
References:
Simple Triggers
logical OR
I'm trying to compare list of music with releaseDate. But I can retrieve music without releaseDate and when I want to sort them, I got an error.
How can I sort / compare nullable datetime and put null releaseDate to the end?
_followedMusic.sort((a, b) {
if (a.releaseDate != null && b.releaseDate != null)
return a.releaseDate.compareTo(b.releaseDate);
else
// return ??
});
Thank you
If you take a look at the documentation for compareTo:
Returns a value like a Comparator when comparing this to other. That is, it returns a negative integer if this is ordered before other, a positive integer if this is ordered after other, and zero if this and other are ordered together.
https://api.dart.dev/stable/2.10.0/dart-core/Comparable/compareTo.html
So your compareTo should just result in returning the values -1, 0 or 1 according to if the compared object should be before, the same position or after the current object.
So in your case if you want your null entries to be at the start of the sorted list, you can do something like this:
void main() {
final list = ['b', null, 'c', 'a', null];
list.sort((s1, s2) {
if (s1 == null && s2 == null) {
return 0;
} else if (s1 == null) {
return -1;
} else if (s2 == null) {
return 1;
} else {
return s1.compareTo(s2);
}
});
print(list); // [null, null, a, b, c]
}
Or if you want the null at the end:
void main() {
final list = ['b', null, 'c', 'a', null];
list.sort((s1, s2) {
if (s1 == null && s2 == null) {
return 0;
} else if (s1 == null) {
return 1;
} else if (s2 == null) {
return -1;
} else {
return s1.compareTo(s2);
}
});
print(list); // [a, b, c, null, null]
}
Or, as #lrn suggests, make the last example in a more short and efficient way (but maybe not as readable :) ):
void main() {
final list = ['b', null, 'c', 'a', null];
list.sort((s1, s2) => s1 == null
? s2 == null
? 0
: 1
: s2 == null
? -1
: s1.compareTo(s2));
print(list); // [a, b, c, null, null]
}
what about _followdMusic.map((date) => return date ?? 1900.01.01).toList().sort(...)
the date is pseudo code, not sure how to write it. This way you put all unknown dates at one of the ends of the list.
The answer of #julemand101 also can be used with the extension function.
extension DateEx on DateTime? {
int compareToWithNull(DateTime? date2) {
if (this == null && date2 == null) {
return 0;
} else if (this == null) {
return -1;
} else if (date2 == null) {
return 1;
} else {
return this!.compareTo(date2);
}
}
}
Say I have a nested QJsonObject that looks like the following JSON:
var sample_set_ =
{child_sample_set_objs:[
{sample_containers:[
{"samples":[
{id:1, sample_val:42}
]}
]}
]}
or equivalently:
auto sample_set_ =
QJsonObject{
{"child_sample_set_objs",
QJsonArray{
{ QJsonObject{
{"sample_containers",
QJsonArray{
{ QJsonObject{
{"samples,
QJsonArray{
{ QJsonObject{
"id", 1,
"sample_val", 42
} }
}
}
}
} }
}
} }
}
}
}
How can I write the data to the leaf node and mutate sample_set_ without copying back all the way up the structure?
The following is what I believe I need to do and it is horrendously verbose.
Given int child_ss_idx and int sample_container_idx:
auto child_sample_set_objs = sample_set_["child_sample_set_objs"].toArray();
auto child_ss = child_sample_set_objs[child_ss_idx].toObject();
auto sample_containers = child_ss["sample_containers"].toArray();
auto sample_container = sample_containers[sample_container_idx].toObject();
auto samples = sample_container["samples"].toArray();
// the write vvvvvvvv
samples.push_back(QJsonObject{ {"sample_set_id", child_ss["id"]} });
sample_container["samples"] = samples;
sample_containers[sample_container_idx] = sample_container;
child_ss["sample_containers"] = sample_containers;
child_sample_set_objs[child_ss_idx] = child_ss;
sample_set_["child_sample_set_objs"] = child_sample_set_objs;
I created a variable q outside of any function. From within my function I am attempting to simply increment it with a ++. Will this increment the global q or is this simply appending the value to a local variable? As you can see in the code sample below I am attempting to use the value of the global variable (which I intend to be updated during each execution of this script) to set a variable which should trigger this function via .change. The function is initially trigger (when q = 1) however it is not trigger when a selection is made from the dropdown box with id = "selectedId2" which is leading me to believe that q has retained a value of 1 though I successfully incremented it when the function was ran prior. Any advise of how I can increment the variable "q" for each iteration of this script would be greatly appreciated.
if (q === 1) {
selectedDiv = '#selectId1';
selectedDiv2 = '#selectId2';
}
if (q === 2) {
selectedDiv = '#selectedId2';
selectedDiv2 = '#selectedId3';
}
if (q === 3) {
selectedDiv = '#selectedId3';
selectedDiv2 = '#selectedId4';
}
if (q === 4) {
selectedDiv = '#selectedId4';
selectedDiv2 = '#selectedId5';
}
if (q === 5) {
selectedDiv = '#selectedId5';
selectedDiv2 = '#selectedId6';
}
$(selectedDiv).change(function () {
if (q == 1) {
var pullDownDivs = '#2';
}
if (q == 2) {
var pullDownDivs = '#3';
}
if (q == 3) {
var pullDownDivs = '#4';
}
if (dropDownSelectJoined != null) {
var dropDownSelectJoined = dropDownSelectJoined + ", " + $(selectedDiv).val();
}
else {
var dropDownSelectJoined = $(selectedDiv).val();
}
var SelArea = $(selectedDiv).val();
if (SelArea != 0) {
var url = '#Url.Action("NetworkSubForm")';
q++;
$.post(url, { RemovedAreaId: $('#RemovedAreaId').val(), selectedNetworkId: $('#SelectedNetworkId').val(), dropDownSelectJoined: dropDownSelectJoined },
function (data) {
var productDropdown = $(selectedDiv2);
productDropdown.empty();
productDropdown.append("<option>-- Select Area --</option>");
for (var i = 0; i < data.length; i++) {
productDropdown.append($('<option></option>').val(data[i].Value).html(data[i].Text));
}
});
$(pullDownDivs).show();
$(pullDownDivs).html();
}
else {
$(pullDownDivs).hide();
$(pullDownDivs).html();
}
});
I don't know what the rest of your code looks like, but you can see this kind of behavior due to "shadowing":
var q = 0; //global "q"
function handler() {
var q = 0; //local "q" that shadows the global "q";
...
...
q++;
console.log(q);
}
Repeatedly calling handler will output 1 each time since you are redefining a local q within handler. However, the outer q remains unchanged. But if you did this:
var q = 0; //global "q"
function handler() {
var q = 0; //local "q" that shadows the global "q";
...
...
window.q++;
console.log(window.q);
}
The global q will be updated since you are explicitly referencing it by doing window.q.