标签: predicate

string.Contains作为谓词而不是函数调用?

我在SO上找到了这个代码示例(不记得从哪里:/),它允许我在启动我的应用程序时检查行代码参数:

if (e.Args.Length == 0 || e.Args.Any("-show".Contains))
{
  //show interface...
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法理解它是如何"-show".Contains工作的.如果与(经典)有任何区别x => x.Contains('"-show")(除了明显的打字增益).

工作就像一个魅力,但我想明白为什么,我觉得一些大的东西是hapening.

c# linq predicate

15
推荐指数
1
解决办法
1125
查看次数

核心数据谓词日期比较

我试图获取匹配用户selectedDate的实体中的所有对象(它是一个NSDate).核心数据代码很好,但我的谓词不断返回0结果,数据库中的日期与用户选择的相同.

应该如何将selectedDate与使用谓词的实体的日期进行比较?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(eDate = %@)", selectedDate];
Run Code Online (Sandbox Code Playgroud)

cocoa predicate objective-c nsdate

14
推荐指数
2
解决办法
2万
查看次数

为什么`Predicate <T>`与`Func <T,bool>`不匹配?

我尝试在C#中编译以下代码:

public static T FirstEffective(IEnumerable<T> list) 
{
    Predicate<T> pred = x => x != null;
    return Enumerable.FirstOrDefault(list, pred);
}
Run Code Online (Sandbox Code Playgroud)

编译器(Mono/.NET 4.0)给出以下错误:

File.cs(139,47) The best overloaded method match for `System.Linq.Enumerable.FirstOrDefault<T>(this System.Collections.Generic.IEnumerable<T>,System.Func<T,bool>)' has some invalid arguments
/usr/lib/mono/4.0/System.Core.dll (Location of the symbol related to previous error)
File.cs(139,47): error CS1503: Argument `#2' cannot convert `System.Predicate<T>' expression to type `System.Func<T,bool>'
Run Code Online (Sandbox Code Playgroud)

这是相当奇怪的,因为a Predicate<T>实际上是一个函数,它接受一个参数作为输入T并返回一个bool(T甚至是"协变"因此T允许的特化).代表们是否不考虑"Liskov替代原则"来推导出Predicate<T>相当于Func<T,bool>?据我所知,这个等价问题应该是可判定的.

c# delegates predicate function

14
推荐指数
2
解决办法
1462
查看次数

Scheme和Clojure没有原子类型谓词 - 这是设计的吗?

常见的LISP和Emacs LISP具有原子类型谓词.Scheme和Clojure没有它.http://hyperpolyglot.wikidot.com/lisp

是否存在设计原因 - 或者它不是包含在API中的基本功能?

lisp scheme elisp predicate clojure

13
推荐指数
4
解决办法
2034
查看次数

XPath/XSLT嵌套谓词:如何获取外部谓词的上下文?

似乎这个问题之前没有在stackoverflow上讨论过,除了使用嵌套的XPath Predicates ...精炼在哪里提供了不涉及嵌套谓词的解决方案.

所以我试着写出我想要得到的过度简化的样本:

输入:

<root>
    <shortOfSupply>
        <food animal="doggie"/>
        <food animal="horse"/>
    </shortOfSupply>
    <animalsDictionary>
        <cage name="A" animal="kittie"/>
        <cage name="B" animal="dog"/>
        <cage name="C" animal="cow"/>
        <cage name="D" animal="zebra"/>
    </animals>
</root>
Run Code Online (Sandbox Code Playgroud)

输出:

<root>
    <hungryAnimals>
        <cage name="B"/>
        <cage name="D"/>
    </hungryAnimals>
</root>
Run Code Online (Sandbox Code Playgroud)

或者,如果没有交叉点,

<root>
    <everythingIsFine/>
</root>
Run Code Online (Sandbox Code Playgroud)

我想使用嵌套谓词来获取它:

<xsl:template match="cage">
    <cage>
        <xsl:attribute name="name">
            <xsl:value-of select="@name"/>
        </xsl:attribute>
    </cage>
</xsl:template>

<xsl:template match="/root/animalsDictionary">
    <xsl:choose>
        <!--                                                             in <food>     in <cage>       -->
        <xsl:when test="cage[/root/shortOfSupply/food[ext:isEqualAnimals(./@animal, ?????/@animal)]]">
            <hungryAnimals>
                <xsl:apply-templates select="cage[/root/shortOfSupply/food[ext:isEqualAnimals(@animal, ?????/@animal)]]"/>
            </hungryAnimals>
        </xsl:when>
        <xsl:otherwise>
            <everythingIsFine/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

那么我该?????怎么写呢呢?

我知道我可以使用一个更多的模板和变量/参数的广泛使用来重写整个样式表,但它甚至使这个样式表变得更加复杂,更不用说真实问题的真实样式表了. …

xslt xpath nested predicate

13
推荐指数
1
解决办法
7451
查看次数

JPA:谓词和表达式都在QueryCriteria where子句中

我有一个情况,在我的where子句中我有单个谓词和表达式.两者都需要在where子句中进行AND运算:

Expression<String> col1 = tableEntity.get("col1");
Expression<String> regExpr = criteriaBuilder.literal("\\.\\d+$");
Expression<Boolean> regExprLike = criteriaBuilder.function("regexp_like", Boolean.class, col, regExpr);

Expression<TableEntity> col2= tableEntity.get("col2");
Predicate predicateNull = criteriaBuilder.isNull(col2);

createQuery.where(cb.and(predicateNull));
createQuery.where(regExprLike);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我无法执行以下操作:createQuery.where(predicateNull,regExprLike);

我尝试使用CriteriaBuilder的isTrue()方法:

Predicate predicateNull = criteriaBuilder.isNull(col2);
Predicate predicateTrue = criteriaBuilder.isTrue(regExprLike);
createQuery.where(predicateNull, predicateTrue);
Run Code Online (Sandbox Code Playgroud)

但它没有帮助.

CriteriaQuery要么在where子句中允许谓词或表达式,而不是两者都允许.任何想法如何在QueryCriteria的where子句中使用谓词和表达式?

2014年10月10日更新: 根据Chris的建议,我尝试使用:

createQuery.where(predicateNull, regExprLike);
Run Code Online (Sandbox Code Playgroud)

但我的查询失败,但有异常:

Caused by: org.jboss.arquillian.test.spi.ArquillianProxyException: org.hibernate.hql.internal.ast.QuerySyntaxException : unexpected AST node: ( near line 1, column 311 [select coalesce(substring(generatedAlias0.col1,0,(locate(regexp_substr(generatedAlias0.col1, :param0),
generatedAlias0.col1)-1)), generatedAlias0.col1), generatedAlias0.col1 
from com.temp.TableEntity as generatedAlias0 
where (generatedAlias0.col2 is null ) and ( regexp_like(generatedAlias0.col1, :param1))] [Proxied because : …
Run Code Online (Sandbox Code Playgroud)

java expression jpa criteria predicate

13
推荐指数
1
解决办法
5981
查看次数

“谓词不应由于函数调用而修改其状态”是什么意思?

我在网上阅读有关C ++的内容,并遇到了以下说法:

谓词不应由于函数调用而修改其状态。

我不明白“状态”在这里是什么意思。有人可以举例说明吗?

c++ stl predicate c++11

13
推荐指数
2
解决办法
227
查看次数

快速在Prolog中运行

我的公司有一个在Prolog运行的项目,我想澄清一些关于如何学习它的事情.我知道Prolog与众不同.它不应该像任何其他语言一样学习.

话虽如此,考虑到我还没有把手放在任何Prolog书上,有没有书或在线资源,我可以在哪里学习Prolog我们学习C/C++的方式?我的意思是,只要是在C/C++的操作,你只需要知道程序的结构,比如main { },loops,conditions,branches,很少functions,你可以用它来启动在C/C编写的基本程序++.

就这样我可以学习Prolog吗?是否有任何书只是让我知道如何在Prolog中编程?(基础知识,循环,如何实现条件,程序结构,什么是谓词?如何使用它?如何定义它?等等......).

logic predicate prolog

12
推荐指数
3
解决办法
2099
查看次数

如何匹配XPath(lxml)中元素的内容?

我想用lxml使用XPath表达式解析HTML.我的问题是匹配标签的内容:

比如给出了

<a href="http://something">Example</a>
Run Code Online (Sandbox Code Playgroud)

element我可以匹配href属性

.//a[@href='http://something']
Run Code Online (Sandbox Code Playgroud)

但给定的表达

.//a[.='Example']
Run Code Online (Sandbox Code Playgroud)

甚至

.//a[contains(.,'Example')]
Run Code Online (Sandbox Code Playgroud)

lxml抛出'invalid node predicate'异常.

我究竟做错了什么?

编辑:

示例代码:

from lxml import etree
from cStringIO import StringIO

html = '<a href="http://something">Example</a>'
parser = etree.HTMLParser()
tree   = etree.parse(StringIO(html), parser)

print tree.find(".//a[text()='Example']").tag
Run Code Online (Sandbox Code Playgroud)

预期产量为'a'.我得到'SyntaxError:无效的节点谓词'

python xpath lxml predicate

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

返回Java流中的第一个结果匹配谓词或所有不匹配的结果

我有一个Validator提供isValid(Thing)方法的接口,返回ValidationResult包含a boolean和原因消息的方法.

我想创建一个ValidatorAggregator这个接口的实现,它在多个Validators上执行OR (如果有的话Validator返回一个肯定的结果,那么结果是正的).如果任何验证器成功,我想短路并返回其结果.如果没有验证器成功,我想返回所有失败消息.

我可以使用流简洁地执行此操作findFirst().orElse(...)但是如果findFirst返回空,则使用此模式会丢失所有中间结果:

public ValidationResult isValid(final Thing thing) {
    return validators.stream()
      .map(v -> validator.isValid(thing))
      .filter(ValidationResult::isValid)
      .findFirst()
      .orElseGet(() -> new ValidationResult(false, "All validators failed'));
}
Run Code Online (Sandbox Code Playgroud)

有没有办法使用流捕获失败的结果,或者实际上比下面更简洁?

public ValidationResult isValid(final Thing thing) {
    final Set<ValidationResult> failedResults = new HashSet<>();
    for (Validator validator : validators) {
        final ValidationResult result = validator.isValid(thing);
        if (result.isValid()) {
            return result;
        }
        failedResults.add(result);
    }
    return new ValidationResult(false, "No successful …
Run Code Online (Sandbox Code Playgroud)

java aggregate predicate java-8 java-stream

12
推荐指数
1
解决办法
137
查看次数