通用语言服务程序
这是我的问题:我想从 csv 文件读取列标题,并从这些标题创建一个返回 plist 的函数,其中标题是属性名称:
假设我们有一个 csv 文件,其列标题为“名称”、“日期”、“事件”。我想从这 3 个字符串创建一个如下函数:
(defun read-csv-line (x y z)
(list :Name x :Date y :Event z))
Run Code Online (Sandbox Code Playgroud)
这样我就可以继续阅读以下几行:
(read-csv-line "Bob" "1/1/1985" "Birthday")
Run Code Online (Sandbox Code Playgroud)
我认为宏可以解决这个问题:
(defmacro convert-to-plist (x y z)
`(defun read-csv-line (a b c)
(list :,(intern x) a :,(intern y) b :,(intern z) c)))
Run Code Online (Sandbox Code Playgroud)
但这种语法不被认为是正确的。
那你说我怎么能做到呢?当然,如果参数数量可变的话会更好......
提前致谢!你的,h
我有许多服务类,其call方法在参数上有变化。
notify我想在每个方法的末尾调用一个函数call。我不想修改这些服务类,但我愿意修改基类。
我正在玩,ActiveSupport::Callbacks但它不能达到不修改服务类的目的。
require 'active_support'
class Base
include ActiveSupport::Callbacks
define_callbacks :notifier
set_callback :notifier, :after do |object|
notify()
end
def notify
puts "notified successfully"
end
end
class NewPost < Base
def call
puts "Creating new post on WordPress"
# run_callbacks :notifier do
# puts "notifying....."
# end
end
end
class EditPost < Base
def call
puts "Editing the post on WordPress"
# run_callbacks :notifier do
# puts "notified successfully"
# end
end
end
person …Run Code Online (Sandbox Code Playgroud) class Foo
belongs_to :bar
Run Code Online (Sandbox Code Playgroud)
有什么好方法可以找出哪个类是belongs_to关系?
在这种情况下,它是Bar。
我刚刚遇到了 gcc 和 clang 的编译错误,所以我认为这段代码是不可能的:
template < typename T >
struct Type {
using type = T;
};
template < int size = 1024 >
struct Foo {};
constexpr auto test_ = [] (const int size) {
return Type<Foo<size>>;
};
Run Code Online (Sandbox Code Playgroud)
编译错误:
test.cpp:12:19: error: non-type template argument is not a constant expression
return Type<Foo<size>>;
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
问题是为什么?size是一个常量值,应该能够适合作为模板参数不是吗?我已经使用了一些静态常量值作为模板参数,但似乎不支持这种情况。
我正在使用 PySpark 处理一些通话数据。正如您所看到的,我GetInfoFromCalls通过使用元类动态地向类添加了一些内部类。下面的代码位于for_test所有节点中都存在的包中:
class StatusField(object):
"""
some alias.
"""
failed = "failed"
succeed = "succeed"
status = "status"
getNothingDefaultValue = "-999999"
class Result(object):
"""
Result that store result and some info about it.
"""
def __init__(self, result, status, message=None):
self.result = result
self.status = status
self.message = message
structureList = [
("user_mobile", str, None),
("real_name", str, None),
("channel_attr", str, None),
("channel_src", str, None),
("task_data", dict, None),
("bill_info", list, "task_data"),
("account_info", list, "task_data"),
("payment_info", list, "task_data"),
("call_info", …Run Code Online (Sandbox Code Playgroud) 我正在使用以下函数从 Julia AST 中去除行号:
function filter_lineno(ex::Expr)
filter!(ex.args) do e
isa(e, LineNumberNode) && return false
if isa(e, Expr)
(e::Expr).head === :line && return false
filter_lineno(e::Expr)
end
return true
end
return ex
end
Run Code Online (Sandbox Code Playgroud)
但是当代码中有宏时,这似乎无法正常工作。这是一个失败的例子:
expr = Meta.parse("begin run(``) end")
filter_lineno(expr)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
function filter_lineno(ex::Expr)
filter!(ex.args) do e
isa(e, LineNumberNode) && return false
if isa(e, Expr)
(e::Expr).head === :line && return false
filter_lineno(e::Expr)
end
return true
end
return ex
end
Run Code Online (Sandbox Code Playgroud)
处理文档字符串时的另一个示例:
expr = Meta.parse("begin \"Here is the doc\"\nmodule X end end")
filter_lineno(expr)
Run Code Online (Sandbox Code Playgroud)
产生以下结果:
quote …Run Code Online (Sandbox Code Playgroud) 我想做这样的事情:
template <uint64_t N>
struct a {
static constexpr T1 v1 = {};
static constexpr T2 v2 = {};
static constexpr auto v3 = (N % 2 == 1 ? v1 : v2);
};
Run Code Online (Sandbox Code Playgroud)
但是我不能将 (? :) 用于不同类型。我怎么能做到这一点?
出于性能原因,我需要与用户定义函数一样快的梯度和 Hessians(例如,ForwardDiff 库使我的代码显着变慢)。然后我尝试使用@generated宏进行元编程,用一个简单的函数进行测试
using Calculus
hand_defined_derivative(x) = 2x - sin(x)
symbolic_primal = :( x^2 + cos(x) )
symbolic_derivative = differentiate(symbolic_primal,:x)
@generated functional_derivative(x) = symbolic_derivative
Run Code Online (Sandbox Code Playgroud)
这正是我想要的:
rand_x = rand(10000);
exact_values = hand_defined_derivative.(rand_x)
test_values = functional_derivative.(rand_x)
isequal(exact_values,test_values) # >> true
@btime hand_defined_derivative.(rand_x); # >> 73.358 ?s (5 allocations: 78.27 KiB)
@btime functional_derivative.(rand_x); # >> 73.456 ?s (5 allocations: 78.27 KiB)
Run Code Online (Sandbox Code Playgroud)
我现在需要将其推广到具有更多参数的函数。明显的推断是:
symbolic_primal = :( x^2 + cos(x) + y^2 )
symbolic_gradient = differentiate(symbolic_primal,[:x,:y])
Run Code Online (Sandbox Code Playgroud)
该symbolic_gradient预期(就如同在一维的情况下),但我相信它会在@generated宏不应对多种尺寸的行为:
@generated functional_gradient(x,y) = symbolic_gradient …Run Code Online (Sandbox Code Playgroud) 最近我有一个在Scala 3中编写宏的经验。我用于inline简单的函数和引用代码,scala.quoted用于更复杂的操作。
似乎这两个功能都用编译时生成的代码替换了一些运行时代码,但inline有一些限制。
它们之间有什么区别,为什么inline不能到处使用引用代码代替?
我正在尝试编写一个 rust (meta-) 函数,将一些输入类型映射到一些不相关的输出类型。
来自 C++ 背景,我通常会这样写:
template<typename T>
struct f;
template<>
struct f<int> { using type = double; };
using input_type = int;
using output_type = f<input_type>::type;
Run Code Online (Sandbox Code Playgroud)
我天真地尝试在 Rust 中编写相同的内容如下:
macro_rules! f {
($in: ty) => {
match $in {
i32 => f32,
}
}
}
type OutputType = f!(i32);
Run Code Online (Sandbox Code Playgroud)
但是,好吧,这无法编译,因为宏显然没有返回类型。
$ rustc typedef.rs
error: expected type, found keyword `match`
--> typedef.rs:3:5
|
3 | match $in {
| ^^^^^ expected type
...
9 | type OutputType = f!(i32); …Run Code Online (Sandbox Code Playgroud) metaprogramming ×10
c++ ×2
julia ×2
macros ×2
ruby ×2
activerecord ×1
c++11 ×1
c++14 ×1
callback ×1
common-lisp ×1
metaclass ×1
pickle ×1
plist ×1
python-3.x ×1
recursion ×1
rust ×1
scala ×1
scala-macros ×1
templates ×1