- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
...或者,我如何通过它们实现的接口(interface)过滤一系列类?
假设我有一个继承自 Foo 的对象序列,即 seq<#Foo>
。 .换句话说,我的序列将包含 Foo 的四个不同子类中的一个或多个。
每个子类实现不同的独立接口(interface),与其他子类实现的接口(interface)不共享任何内容。
现在我需要将此序列过滤为仅实现特定接口(interface)的项目。
C# 版本很简单:
void MergeFoosIntoList<T>(IEnumerable<Foo> allFoos, IList<T> dest)
where T : class
{
foreach (var foo in allFoos)
{
var castFoo = foo as T;
if (castFoo != null)
{
dest.Add(castFoo);
}
}
}
let mergeFoosIntoList (foos:seq<#Foo>) (dest:IList<'a>) =
System.Linq.Enumerable.OfType<'a>(foos)
|> Seq.iter dest.Add
let mergeFoosIntoList (foos:seq<#Foo>) (dest:IList<'a>) =
foos
|> Seq.choose (function | :? 'a as x -> Some(x) | _ -> None)
|> Seq.iter dest.Add
:? 'a
- 告诉我:
This runtime coercion or type test from type 'b to 'a involves an indeterminate type based on information prior to this program point. Runtime type tests are not allowed on some types. Further type annotations are needed.
'a
之间没有关系和
#Foo
除了 Foo 的一个或多个子类实现该接口(interface)。此外,可以作为
'a
传入的不同接口(interface)之间没有关系。除了它们都是由 Foo 的子类实现的。
最佳答案
你可以这样做:
let foos = candidates |> Seq.filter (fun x -> x :? Foo) |> Seq.cast<Foo>
关于F# 等价于 Enumerable.OfType<'a>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2521254/
是: x -= y; 相当于: x = x - y; 最佳答案 不,它们并不等同于您表达它们的方式。 short x = 0, y = 0; x -= y; // This compiles f
这个问题在这里已经有了答案: What is the rationale for all comparisons returning false for IEEE754 NaN values? (1
我在哪里可以找到与 Python maketrans 和 translate 等效的 C# 代码?谢谢! 最佳答案 这应该带你到那里的大部分方式: public class MakeTrans {
我正在 SwiftUI 中构建一个应用程序其中包含很多组件,包括 Text包含长字符串的 View 。 当我在 iPhone 11 上启动该应用程序时,一切正常,但当我在较小的设备(例如 iPhone
这个问题已经有答案了: What is the equivalent lambda expression for System.out::println (2 个回答) Function pointe
我最近在使用 postgres,我必须做一些计算。然而我一直没能模仿Excel的HOUR()函数,我看了official information但这对我帮助不大。 函数接收一个小数,并得到小数的时、分
如果在 cython 中定义了一个指针 vector ,那么与 python 中的 enumerate 类似的函数或过程是什么,用于遍历指针数组中元素的索引和值在 C 声明类型函数内的循环中? 测试.
要选择: select user_id, max(case when value > 0 then timestamp else 0 end) as max_timestamp_whe
如果没有例子,这个问题很难问,所以这里是: #include struct O { }; struct C { template void function1(void (C::*call
我得到了这个结构的实现: struct NodoQ { Etype elem; NodoQ *sig; }; 下面是这段代码吗, typedef NodoQ *PtrNodoQ; PtrNod
我有一些宏需要访问当前类的类型,目前我通过违反 DRY 的模式解决了这个问题: struct ThisScruct{ int a; double b; //example st
我想知道 TensorFlow 的 softmax_cross_entropy_with_logits 是否有等效的 PyTorch 损失函数? 最佳答案 is there an equivalent
我找到了一个 trie 的 java 实现,并希望在 J2ME 中有一个类似的实现。这是代码。 节点类 import java.util.Collections; import java.util.L
我刚刚学习了 GraphQL,我想找到用户 id=2 OR 用户 id=3 现在我将如何进行 GraphQL 查询,我正在使用以下查询获取整个集合 { users() {
假设我有两个 Web 服务:A 和 B。两者都在 Apache 上运行。我希望它们可以从我的主机的不同端口访问:A 来自端口 88,B 来自端口 89。 我可以手动完成(首先创建图像,然后使用“doc
我一直在 excel 中使用一个非常简单的数组公式来处理一些数据集,但是它们变得太大并且在我更新计算时完全破坏了我的计算机性能。 excel表格和MySQL数据库布局如下: +-Timestamp-+
我有一个类,其实例要通过不同于它们携带的数据值的标识来区分。在我的代码中,我打算使用 == 来表示两个实例在它们的数据方面是等价的,并且 is 表示两个变量引用同一个实例,也就是说,他们是相同的。根据
我正在 Windows 中使用 WinSock 2.0 开发代理服务器。如果我想在阻塞模型中开发它,select() 是等待客户端或远程服务器从中接收数据的方法。是否有任何适用的方法可以使用 I/O
我正在将我制作的 Android 应用移植到 iOS。 Android 有一个 Yield() 函数可以将线程从运行中移到线程队列的后面(?)。这很有用,这样该线程就不会占用过多的 CPU 并使其他一
这是否保证始终为真: std::numeric_limits::max() == INT_MAX C++ 标准对此有何规定?我在标准中找不到任何明确说明这一点的引用资料,但我一直在阅读这些内容应该是等
我是一名优秀的程序员,十分优秀!