小编Jac*_*mos的帖子

Julia - 将关键字参数传递给另一个函数?

假设我有一系列具有不同名称的关键字参数的函数

foo(x, y; a=1, b=2) = (a + b) / (x + y)
bar(x, y; c=3, d=4) = (x - y) * (c - d)
Run Code Online (Sandbox Code Playgroud)

假设我有第三个函数,它将函数作为参数。在调用第三个函数时,我希望能够将任何关键字参数传递给前两个函数之一。

master(x, y; fun::Function=foo, args...) = fun(x, y, args...)
Run Code Online (Sandbox Code Playgroud)

尝试master使用关键字参数调用函数时出现了我的问题。

julia> master(pi, e, fun=bar)
-0.423310825130748

julia> master(pi, e, fun=bar, c=4)
ERROR: MethodError: `bar` has no method matching bar(::Irrational{:?}, ::Irrational{:e}, ::Tuple{Symbol,Int64})
Closest candidates are:
  bar(::Any, ::Any)
Run Code Online (Sandbox Code Playgroud)

有没有办法传递关键字参数而不必反复检查参数名称?

如果问题不清楚,请告诉我,我很乐意澄清。我寻找了其他问题,但我看到的解决方案通常展示了如何获取名称-值对,而不是如何将它们传递给其他带有关键字参数的函数

arguments function parameter-passing julia

5
推荐指数
1
解决办法
2074
查看次数

犰狳向量类的 RcppArmadillo 样本

我们一直在使用samplefrom 函数RcppArmadilloNumericVector对象进行随机采样。然而,我们注意到不可能在犰狳类型(vecuvec)上使用相同的函数。我们已经查看了文件中的函数定义sample.h,它看起来像一个模板化函数,应该能够使用这些类型,但是我们无法弄清楚如何使其与犰狳类一起使用,而不需要做很多工作与库之间的转换NumericVector或类型。IntegerVectorRcpp

例如,我们将此函数编写在名为try.cpp.

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>

using namespace arma;
using namespace Rcpp;

// [[Rcpp::export]]
arma::uvec sample_index(const int &size){
    arma::uvec sequence = linspace<uvec>(0, size-1, size);
    arma::uvec out = sample(sequence, size, false);
    return out;
}
Run Code Online (Sandbox Code Playgroud)

运行上面的代码会产生以下错误:

src/try.cpp|11 col 22 error| no matching function for call to 'sample' [cpp/gcc]      

~/Library/R/3.3/library/Rcpp/include/Rcpp/sugar/functions/sample.h|401 col 1 error| note: candidate function not viable: no known conversion from …
Run Code Online (Sandbox Code Playgroud)

c++ r armadillo rcpp

2
推荐指数
1
解决办法
1378
查看次数

标签 统计

arguments ×1

armadillo ×1

c++ ×1

function ×1

julia ×1

parameter-passing ×1

r ×1

rcpp ×1