标签: generic-function

创建泛型函数的委托

我正在写一些单元测试,我有很多表单的功能

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)

.net lambda delegates generic-function

3
推荐指数
1
解决办法
363
查看次数

Common Lisp:从泛型函数中提取方法

有没有办法从 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)

methods common-lisp clos generic-function mop

3
推荐指数
1
解决办法
358
查看次数

了解 Common Lisp 中的泛型函数?

在这个答案中,用户给出了一个非常清晰的示例,说明类和方法如何协同工作。

我将在这里重印该示例:


(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)

我的问题是,使用相同的示例:

  1. 创建通用函数会增加什么价值?
  2. 为什么泛型函数有用?它们是否像其他面向对象语言中提供结构的实例一样?

似乎通用函数是在后台隐式创建的(不是 100% 确定)。我注意到,当我使用这个示例时,如果我创建一个具有与该方法的第一个实例不同的参数结构的方法,我会得到一个generic function error.

methods function common-lisp clos generic-function

3
推荐指数
1
解决办法
565
查看次数

为什么单独实例化的Func <T,bool>谓词不会转换为带有Entity Framework的SQL?

我有一个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子句,它会返回所有内容,然后在代码中对其进行过滤.

sql linq entity-framework generic-function ef-code-first

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

R:从列表对象创建自定义输出

我有一个存储不同数据类型和对象的列表:

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)

r generic-function

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

在泛型函数方法中不能使用变量吗?(CLOS/LISP)

我正在学习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)

在上面的示例中,您可以看到方法本身从未实际使用过xy变量.所有这些例子都不使用变量,这是巧合吗?他们可以用吗?

此外,它写在维基百科上:

方法与类分开定义,并且它们对类槽没有特殊访问权(例如"this","self"或"protected").

好吧,所以方法没有"这个",因为它们不属于一个类.但是为什么泛型函数方法有接收器呢?接收器在类中不是类似于'this'吗?

lisp common-lisp clos generic-function

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

如何根据传递给函数的数据类型分派 Python 2 函数?

我想根据传递给“调度”函数(例如使用)的参数的数据类型来调度 Python 函数(例如使用dict 方法isinstance())。是否有实施替代方案?最简单的方法是什么?

python generic-function single-dispatch python-2.7

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

如何在 AWSglue 中使用从一个胶水脚本到另一个胶水脚本的函数

我有一个 AWSglue pyspark 脚本,例如 scriptA.py。在此脚本中,我定义了一些通用函数,例如 readSourceData()

def readSourceData(参数1, 参数2):

//函数逻辑

现在我想在我的第二个胶水 pyspark 脚本 scriptB.py 中使用这个通用函数。

我有很多这样的通用函数。如何导入这些函数并在其他脚本中使用它们?

generic-function amazon-web-services pyspark aws-glue aws-glue-spark

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

理解 CLOS:后续方法和主要方法

在 hunchentoot 源代码中,有一个:after名为initalize-instance 的def 方法。

这个特定的示例是整个项目中调用的少数:after方法之一。initalize-instance

我使用 CLOS 的经验告诉我, 需要一个主要方法:before:after并且:around可以使用方法。

但在 hunchentoot 包中,我看不到他们如何创建主要方法以使这些:after方法起作用。

我缺少什么?

sbcl common-lisp clos generic-function method-combination

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