使用/导入的Julia元编程

时间:2018-09-12 11:18:17

标签: julia metaprogramming

我想基于参数expr评估一个导入Module的表达式。到目前为止,我想到了:

julia> expr = :(Base.Threads)

julia> @eval using $expr
ERROR: TypeError: import or using: expected Symbol, got Expr
Stacktrace:
 [1] eval(::Module, ::Expr) at ./sysimg.jl:23

一种可能性是直接使用Expr构造函数,如下所示:

julia> expr = [:Base, :Threads]
2-element Array{Symbol,1}:
 :Base   
 :Threads

julia> eval(Expr(:using, expr...))

但是还有其他可能更直接的方法而无需构造Expr吗?

1 个答案:

答案 0 :(得分:5)

将宏名称后的每个以空格分隔的字符组视为一个单独的参数。相反,您应该只在括号之间编写表达式。

Dataset<String> onlyRows = logData.filter((FilterFunction<String>) s -> s.contains("<row"));
    Dataset<Row> firstSplit = onlyRows.selectExpr("split(value, 'DisplayName=\"')[1] as User_Id", "split(value, ' Reputation=\"')[1] as Reputation");

    Dataset<Row> repid = firstSplit.selectExpr("split(User_Id, '\" LastAccessDate=')[0] as User", "CAST(split(Reputation, '\" CreationDate')[0] as INT) as Reputation").orderBy(desc("Reputation"));

    repid.show(10);