我正在写一些单元测试,我有很多表单的功能
public void SomeTestHelperMethod<TKey, TValue>(TKey key, TValue value)
Run Code Online (Sandbox Code Playgroud)
我正在反复调用这样的各种论点
SomeTestHelperMethod<int, int>(0, 1);
SomeTestHelperMethod<int, object>(1, new Nullable<double>(16.5));
SomeTestHelperMethod<int, string>(2, "The quick brown fox jumped over the lazy dog.");
SomeTestHelperMethod<object, int>(new NullReferenceException(), 15);
SomeTestHelperMethod<object, object>(StringComparison.Ordinal, new Version());
SomeTestHelperMethod<object, string>((ushort)3, string.Empty);
SomeTestHelperMethod<string, int>(string.Empty, 195);
SomeTestHelperMethod<string, object>("A string", this);
SomeTestHelperMethod<string, string>("Another string", "Another string");
Run Code Online (Sandbox Code Playgroud)
我想做的是编写一个带有Action委托的函数,并可以使用所有不同的参数调用委托.有什么办法吗?
回答:
感谢MichaelCG,这就是我最终做的事情:
private void CallWithKeyAndValue(string methodName)
{
MethodInfo method = typeof(ObservableDictionaryTest).GetMethod(methodName);
foreach (KeyValuePair<object, object> kvp in ourKeyValueSet)
{
MethodInfo genericMethod = method.MakeGenericMethod(kvp.Key.GetType(), kvp.Value.GetType());
genericMethod.Invoke(this, new[] { kvp.Key, kvp.Value }); …Run Code Online (Sandbox Code Playgroud) 有没有办法从 Common Lisp 的泛型函数中提取方法列表?
例如:
(defmethod say ((self string)) ; method-0
(format t "Got string: ~a~%" self))
(defmethod say ((self integer)) ; method-1
(format t "Got integer: ~a~%" self))
(defmethod say ((self symbol)) ; method-2
(format t "Got symbol: ~a~%" self))
(extract-methods-from-generic 'say) ; -> (method-0-obj method-1-obj method-2-obj)
Run Code Online (Sandbox Code Playgroud)
更具体地说,我的目标是 ECL,所以如果这可以通过 C API 完成 - 没关系。
我需要这个来做下一个技巧:
(defgeneric merged-generic ())
(loop for method
in (extract-methods-from-generic 'some-generic-0)
do (add-method merged-generic method))
(loop for method
in (extract-methods-from-generic 'some-generic-1)
do (add-method merged-generic method))
Run Code Online (Sandbox Code Playgroud) 在这个答案中,用户给出了一个非常清晰的示例,说明类和方法如何协同工作。
我将在这里重印该示例:
(defclass human () ())
(defclass dog () ())
(defmethod greet ((thing human))
(print "Hi human!"))
(defmethod greet ((thing dog))
(print "Wolf-wolf dog!"))
(defparameter Anna (make-instance 'human))
(defparameter Rex (make-instance 'dog))
(greet Anna) ;; => "Hi human"
(greet Rex) ;; => "Wolf-wolf dog!"
Run Code Online (Sandbox Code Playgroud)
我的问题是,使用相同的示例:
似乎通用函数是在后台隐式创建的(不是 100% 确定)。我注意到,当我使用这个示例时,如果我创建一个具有与该方法的第一个实例不同的参数结构的方法,我会得到一个generic function error.
我有一个EF Code First Db上下文,我用它来查询数据库.我在Func<Product, bool>从我的Aggregate Repository 传入查询时发现了一些性能问题,并且进一步调查结果发现查询没有被转换为SQL查询.
经过一番挖掘,我发现了以下内容.
var results = _context.Products
.Where(p => p.ProductCode.Contains("AAA"))
.Where(p => p.CategoryId == 1)
.ToList();
Run Code Online (Sandbox Code Playgroud)
这完全符合预期.它使用Where子句生成一些参数化SQL.
================================================== ================
var results2 = _context.Products
.Where(p => p.ProductCode.Contains("AAA") && p.CategoryId == 1)
.ToList();
Run Code Online (Sandbox Code Playgroud)
这也按预期工作.它生成与上面相同的sql
================================================== ================
Func<Product, bool> pred = (p => p.ProductCode.Contains("AAA") && p.CategoryId == 1);
var results3 = _context.Products.Where(pred).ToList();
Run Code Online (Sandbox Code Playgroud)
这打破了.它不会在SQL中生成where子句,它会返回所有内容,然后在代码中对其进行过滤.
我有一个存储不同数据类型和对象的列表:
header <- "This is a header."
a <- 10
b <- 20
c <- 30
w <- 1:10
x <- 21:30
y <- 51:60
z <- 0:9
mylist <- list(header = header,
const = list(a = a, b = b, c = c),
data = data.frame(w,x,y,z))
Run Code Online (Sandbox Code Playgroud)
现在我希望R以下列格式显示此列表:
This is a header.
Values: a: 10 b: 20 c: 30
Data: w x y z
1 1 21 51 0
2 2 22 52 1
3 3 23 53 2
4 …Run Code Online (Sandbox Code Playgroud) 我正在学习CLOS中的泛型函数.
由于我在教科书和网上找到的例子类型,我变得非常困惑.这些示例总是使用多个调度的事实.根据参数类型,执行不同的计算.但是,为什么参数本身从未在示例中使用过?
维基百科的示例代码
; declare the common argument structure prototype
(defgeneric f (x y))
; define an implementation for (f integer t), where t matches all types
(defmethod f ((x integer) y) 1)
(f 1 2.0) => 1
; define an implementation for (f integer real)
(defmethod f ((x integer) (y real)) 2)
(f 1 2.0) => 2 ; dispatch changed at runtime
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,您可以看到方法本身从未实际使用过x或y变量.所有这些例子都不使用变量,这是巧合吗?他们可以用吗?
此外,它写在维基百科上:
方法与类分开定义,并且它们对类槽没有特殊访问权(例如"this","self"或"protected").
好吧,所以方法没有"这个",因为它们不属于一个类.但是为什么泛型函数方法有接收器呢?接收器在类中不是类似于'this'吗?
我想根据传递给“调度”函数(例如使用)的参数的数据类型来调度 Python 函数(例如使用dict 方法isinstance())。是否有实施替代方案?最简单的方法是什么?
我有一个 AWSglue pyspark 脚本,例如 scriptA.py。在此脚本中,我定义了一些通用函数,例如 readSourceData()
def readSourceData(参数1, 参数2):
//函数逻辑
现在我想在我的第二个胶水 pyspark 脚本 scriptB.py 中使用这个通用函数。
我有很多这样的通用函数。如何导入这些函数并在其他脚本中使用它们?
generic-function amazon-web-services pyspark aws-glue aws-glue-spark
在 hunchentoot 源代码中,有一个:after名为initalize-instance 的def 方法。
这个特定的示例是整个项目中调用的少数:after方法之一。initalize-instance
我使用 CLOS 的经验告诉我, 需要一个主要方法:before,:after并且:around可以使用方法。
但在 hunchentoot 包中,我看不到他们如何创建主要方法以使这些:after方法起作用。
我缺少什么?