注意:在发布这个问题之前,我发现有一个更好的方法来做我想要完成的事情(我觉得它很愚蠢):
IEnumerable<string> checkedItems = ProductTypesList.CheckedItems.Cast<string>();
filter = p => checkedItems.Contains(p.ProductType);
Run Code Online (Sandbox Code Playgroud)
好的,是的,我已经意识到这一点.但是,无论如何,我发布了这个问题,因为我仍然不明白为什么我(愚蠢地)试图做的事情不起作用.
我觉得这很容易.原来它让我很头疼.
基本思路:显示ProductType在a中检查其属性值的所有项目CheckedListBox.
实施:
private Func<Product, bool> GetProductTypeFilter() {
// if nothing is checked, display nothing
Func<Product, bool> filter = p => false;
foreach (string pt in ProductTypesList.CheckedItems.Cast<string>()) {
Func<Product, bool> prevFilter = filter;
filter = p => (prevFilter(p) || p.ProductType == pt);
}
return filter;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果在ProductTypesList(a CheckedListBox)中检查了"权益"和"ETF"项目.然后由于某种原因,以下代码仅返回"ETF"类型的产品:
var filter = GetProductTypeFilter();
IEnumerable<Product> filteredProducts = allProducts.Where(filter);
Run Code Online (Sandbox Code Playgroud)
我猜想它可能与一些自我引用的混乱有关filter,其本质上是本身 …
本网站提出以下声明:http: //hyperpolyglot.wikidot.com/lisp#ten-primitives
McCarthy introduced the ten primitives of lisp in 1960. All other pure lisp functions (i.e. all functions which don't do I/O or interact with the environment) can be implemented with these primitives. Thus, when implementing or porting lisp, these are the only functions which need to be implemented in a lower language. The way the non-primitives of lisp can be constructed from primitives is analogous to the way theorems can be proven from axioms in mathematics. …
在C#中,我一直在通用列表中执行FindAll,如下所示:
List<group.category> tlist = list.FindAll(p => p.parid == titem.catid);
Run Code Online (Sandbox Code Playgroud)
两个问题,这是执行这样的事情的适当方式,我如何将其转换为VB.Net
在Java中,是否有一种简单的方法将多个谓词(Guava Predicate)合并为一个?
目前,我有一些谓词列表:
Collection<Predicate<TypeA>> preds = ...;
Run Code Online (Sandbox Code Playgroud)
我有一些循环遍历谓词的代码,如果其中任何一个是假的,则返回false.是否有一个单行程完成同样的事情?
我只是读了一个人用一个构造函数和operator()一个谓词来调用一个类:
// Example
class Foo {
public:
Foo(Bar);
bool operator()(Baz);
private:
Bar bar;
};
Run Code Online (Sandbox Code Playgroud)
但是,我之前没有听过谓语在这种情况下被使用过.我会称这样的东西为仿函数.对我来说,谓词将来自形式逻辑领域.
这提出了以下问题:
Foo吗?bool与其他东西相比)是否与它有关?operator()存在const?我有以下代码:
public boolean isImageSrcExists(String imageSrc) {
int resultsNum = 0;
List<WebElement> blogImagesList = driver.findElements(blogImageLocator);
for (WebElement thisImage : blogImagesList) {
if (thisImage.getAttribute("style").contains(imageSrc)) {
resultsNum++;
}
}
if (resultsNum == 2) {
return true;
} else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
将它转换为使用Java 8 Stream的正确方法是什么?
当我试图使用时map(),我得到一个错误,因为getAttribute不是Function.
int a = (int) blogImagesList.stream()
.map(WebElement::getAttribute("style"))
.filter(s -> s.contains(imageSrc))
.count();
Run Code Online (Sandbox Code Playgroud) 我试图从a std::list中删除元素并保留一些已删除元素的统计信息.
为了做到这一点,我使用列表中的remove_if函数,我有一个谓词.我想用这个谓词来收集统计数据.以下是谓词的代码:
class TestPredicate
{
private:
int limit_;
public:
int sum;
int count;
TestPredicate(int limit) : limit_(limit), sum(0), count(0) {}
bool operator() (int value)
{
if (value >= limit_)
{
sum += value;
++count; // Part where I gather the stats
return true;
}
else
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
以下是算法的代码:
std::list < int > container;
container.push_back(11);
TestPredicate pred(10);
container.remove_if(pred)
assert(pred.count == 1);
Run Code Online (Sandbox Code Playgroud)
不幸的是,断言是错误的,因为谓词是按值传递的.有没有办法强制它通过引用传递?
我有一个整数流,我想找到两个数字,其总和等于另一个数字.所以我想出了以下解决方案:
BiPredicate<Integer, Integer> p = (price1, price2) -> price1.intValue() + price2.intValue() == moneyQty;
flavoursPrices.filter(p);
Run Code Online (Sandbox Code Playgroud)
但是过滤方法没有收到BiPredicate.为什么不?有什么替代方案?
有没有办法过滤共享扩展程序,以便仅在例如URL域是特定的域时显示?
例如,我想仅在用户共享Google链接时显示我的应用扩展程序:
http://www.google.com/?someQuery
如果我要过滤获取网址,那么下面的代码就足够了:
SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
).@count == $extensionItem.attachments.@count
).@count == 1
Run Code Online (Sandbox Code Playgroud)
在这种情况下,谓词访问$attachment的registeredTypeIdentifiers属性并对其进行评估.我希望能够将REGEX与内部的值匹配$attachment,如下所示:
SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" AND
ANY $attachment.value MATCHES "^http\:\/\/www\.google\.com\/"
).@count == $extensionItem.attachments.@count
).@count == 1
Run Code Online (Sandbox Code Playgroud)
...所以当我的应用程序不支持主机应用程序共享的URL时,我的扩展名将不可见.
PS:请注意,所使用的URL域只是一个示例.当然,真正的一个具有像这样使用的意义.
我有一个谓词,用于过滤同一实体对象的列表:
Predicate<DWHDeal> companyFilter = i -> i.getCompany().equals(company);
Run Code Online (Sandbox Code Playgroud)
我还必须在 DTO 列表上应用相同的过滤器,在完全相同的字段上使用完全相同的条件,其中 DTOS 是基于之前的实体构建的:
Predicate<DWHDealDTO> companyFilterDTO = i -> i.getCompany().equals(company);
Run Code Online (Sandbox Code Playgroud)
是否可以在不实例化两个不同谓词的情况下实现这一目标?如果可能的话,我想通过只制作一个Predicate.