我一直在舔我的脚趾到Rcpp,以加快我目前在R中执行的一些操作。我遇到的一个问题是修改通过引用函数传递的NumericVector。
特别是,下面的函数似乎不会改变传递给函数的NumericVector(x):
import {Component, forwardRef, Input, ViewChild, KeyValueDiffers} from '@angular/core';
import {
ControlValueAccessor, NG_VALUE_ACCESSOR, FormControl, NgModel,
NG_ASYNC_VALIDATORS
} from '@angular/forms';
import {Observable} from "rxjs/Rx";
@Component({
selector: 'input-email',
templateUrl: './email.component.html',
styleUrls: ['./email.component.css'],
providers: [
{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => InputEmailComponent), multi: true },
{ provide: NG_ASYNC_VALIDATORS, useExisting: forwardRef(() => InputEmailComponent), multi: true }
]
})
export class InputEmailComponent implements ControlValueAccessor {
value:String = null;
differ: any;
@ViewChild('f') f:NgModel;
@Input()
info:String = "Email";
@Input()
placeholder:String = "Email";
propagateChange:any = (val) => {};
constructor(private differs: KeyValueDiffers) {
this.differ = differs.find({}).create(null);
}
onChange(){
this.propagateChange(this.value);
}
/**
* Write a passed NgValue value to the element.
*/
writeValue(value) {
if (value && this.value != value) {
this.value = value;
//setTimeout(()=>{this.propagateChange(this.value);},0)
}
}
/**
* Set the function to be called
* when the control receives a change event.
* registers 'fn' that will be fired when changes are made
* this is how we emit the changes back to the form
*/
registerOnChange(fn) {
this.propagateChange = fn;
}
/**
* Set the function to be called
* when the control receives a touch event.
*/
registerOnTouched(fn) {}
/**
* Set the function to be called
* to validate if input has errors.
*/
validate(c: FormControl):any {
return new Promise((resolve, reject) => {
Observable.of(c)
.debounceTime(300)
.switchMap(val => {
return Observable.of(val.errors);
})
.subscribe(result => {
console.log('RESOLVING ASYNC VALIDATOR: ' + JSON.stringify(result, null, 2));
resolve(result);
});
});
}
}
另一方面,下面的代码确实:
void f1(NumericVector &x){
NumericVector y(1);
y[0] = 1;
x = y;
}
基于此,似乎您无法通过为其指定另一个NumericVector来修改通过引用函数传递的NumericVector。相反,你必须逐个元素地修改它。这是对的吗?
这不一定是一个问题,但似乎第一个函数应该起作用,所以我不想不必要地遍历向量的每个元素。