如何使用Array.Find将以下内容转换为VB.NET谓词?
Private Function FindCulture(ByVal Code As String) As Globalization.CultureInfo
'
Dim AllCultures As Globalization.CultureInfo() = Globalization.CultureInfo.GetCultures(Globalization.CultureTypes.AllCultures)
'
For Each Culture As Globalization.CultureInfo In AllCultures
If Culture.TwoLetterISOLanguageName = Code Then
Return Culture
End If
Next
'
Return Nothing
'
End Function
Run Code Online (Sandbox Code Playgroud) 有没有什么方法可以组合谓词?
让我们说我有这样的事情:
class MatchBeginning : public binary_function<CStdString, CStdString, bool>
{ public:
bool operator()(const CStdString &inputOne, const CStdString &inputTwo) const
{ return inputOne.substr(0, inputTwo.length()).compare(inputTwo) == 0; }
};
int main(int argc, char* argv[])
{
CStdString myString("foo -b ar -t az");
vector<CStdString> tokens;
// splits the string every time it encounters a "-"
split(myString, tokens, "-", true, true);
vector<CStdString>::iterator searchResult = find_if(tokens.begin(), tokens.end(), not1(bind2nd(MatchBeginning(), "-")));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这有效,但现在我想做一些事情:
searchResult = find_if(tokens.begin(), tokens.end(), bind2nd(MatchBeginning(), "-b") || not1(bind2nd(MatchBeginning(), "-")));
Run Code Online (Sandbox Code Playgroud)
所以我想找到第一个以"-b"开头的字符串或第一个不以" - "开头的字符串.但是,这给了我一个错误(二进制'||'未定义).
有没有办法做到这一点?
我无法弄清楚如何根据我在运行时传入的值使用List上的"查找".如果您看到我的下面的代码,我希望能够在List中找到它的Path参数等于X的CustomClass,其中X将在运行时定义.
任何想法如何在列表上进行这样的查找?或者,如果不编写迭代器并手动执行查找,这是不可能的?在这种情况下,或许有一个关键的集合,我应该用它来代替?
private List<CustomClass> files;
public void someMethod()
{
Uri u= new Uri(www.test.com);
CustomClass cc = this.files.find( matchesUri(u) ); // WON'T LET ME DO THIS
}
private static bool matchesUri(List<CustomClass> cc, Uri _u)
{
return cc.Path == _u; }
public class CustomClass
{
private Uri path;
public Uri Path
{
get { return this.path; }
set { this.path = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
PS.我必须承认,我并没有完全遵循doco中的谓词内容,网址为http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx
在RDF中,语句用S,P和O表示; 在OWL中,owl:ObjectProperty表示谓词逻辑.
(S) (P) (O)
I like dog
<owl:Class rdf:about="Person" />
<owl:NamedIndividual rdf:about="I">
<rdf:type rdf:resource="Person"/>
<like rdf:resource="Dog"/>
</owl:NamedIndividual>
<owl:Class rdf:about="Pet" />
<owl:NamedIndividual rdf:about="Dog">
<rdf:type rdf:resource="Pet"/>
</owl:NamedIndividual>
<owl:ObjectProperty rdf:about="like">
<rdfs:domain>
<owl:Restriction>
<owl:onProperty rdf:resource="like"/>
<owl:someValuesFrom rdf:resource="Person"/>
</owl:Restriction>
</rdfs:domain>
<rdfs:range>
<owl:Restriction>
<owl:onProperty rdf:resource="like"/>
<owl:someValuesFrom rdf:resource="Pet"/>
</owl:Restriction>
</rdfs:range>
</owl:ObjectProperty>
Run Code Online (Sandbox Code Playgroud)
但如何描述我喜欢狗的"程度"呢?如何为谓词提供属性或值?我得到的一个解决方案是将一个(S,P,O)语句扩展为3个语句.例如,
(S) (P) (O)
Person isSrcOf LikeRelation
Pet isTargetOf LikeRelation
LikeRelation hasValue [0~100]
Run Code Online (Sandbox Code Playgroud)
它应该工作,但显然它会让本体大3倍:(
我很感激任何建议!
我有一个包含该类的div元素的集合media-gallery-item.
我想选择元素编号x.
只选择所有项目时,我得到5个结果
$x("//div[@id='content-area']//div[@class='media-gallery-item']")
Run Code Online (Sandbox Code Playgroud)
现在我希望能够选择项目编号2,但我无法弄清楚如何将两者正确组合:
$x("//div[@id='content-area']//div[@class='media-gallery-item'][2]")
$x("//div[@id='content-area']//div[@class='media-gallery-item'
and position() = 2]")
Run Code Online (Sandbox Code Playgroud)
我知道这些并没有多大意义,因为它不是一个实际的AND,但更像是:"首先按照这个过滤,然后选择第二个匹配".这样做的目的是继续选择事物(例如在第二个元素中,获取元素的href属性a)
您好我有2个包含相同对象的列表.我想通过使用谓词执行任何操作,如intercesct,union,distinct,因为我无法使用equals来进行比较.
例:
class Car{
public String id;
public String color;
public int hashcode(){
//id field is used for hashcode
}
public boolean equals(){
//id field is used for equals
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有两个汽车清单.我需要在这个列表中找到重复项,但不能仅通过颜色找到id.
List<Car> carList1 = new ArrayList(){ new Car(1,blue), new Car(2,green)};
List<Car> carList2 = new ArrayList(){ new Car(1,silver), new Car(4,green)};
Run Code Online (Sandbox Code Playgroud)
我需要从carList1找到第二个对象(新车(2,绿色))
列出类似的东西
Collection.intersect(carList1,carList2,comparator).
Run Code Online (Sandbox Code Playgroud)
在C#中我会用它来LINQ.
有可能以Predicate<T> to Expression<Func<T, bool>>某种方式转换?
我想使用我的ICollectionView的过滤器使用下一个IQueryable函数:
public static System.Linq.IQueryable<TSource> Where<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<System.Func<TSource, bool>> predicate)
Run Code Online (Sandbox Code Playgroud)
谢谢
我已经对这个问题进行了几个小时的故障排除,到目前为止没有成功.我已经在这里阅读了所有可能与我的问题有关的问题.
我有两个实体(A,B)通过像这样的一对多关系以树状结构相互连接:A <--- >> B.
有一个由NSFetchedResultsController支持的UITableViewController,用于显示与实体A的选定对象相关的实体B的所有对象.我正在使用谓词:
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"B" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"controlDate" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
NSMutableArray *subpredicates = [NSMutableArray array];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"isRelatedTo = %@", selectedObjectOfA];
[subpredicates addObject:predicate1];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"syncStatus != %d", ObjectDeleted];
[subpredicates addObject:predicate2];
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
[fetchRequest setPredicate:predicate];
NSFetchedResultsController …Run Code Online (Sandbox Code Playgroud) 此代码有效:
#include <iostream>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
int main(){
priority_queue<int,vector<int>,greater<int> > pq;
pq.push(1);
cout<<pq.top()<<endl;
}
Run Code Online (Sandbox Code Playgroud)
但是,此代码无法编译:
#include <iostream>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
int main(){
priority_queue<int,vector<int>,greater<int>() > pq;
pq.push(1);
cout<<pq.top()<<endl;
}
Run Code Online (Sandbox Code Playgroud)
为什么?
我所了解的是,它greater<int>() 是一个函数对象,并且priority_queue接受二进制谓词作为第三个参数,并且谓词是一种特殊的函子类型。但是这对大括号如何使这一点有所不同。
我正在为xpath 1转换器创建一个CSS选择器,而我最挣扎的一个细节就是找到An+B微语法的简洁替代品.
由于我是为xpath 1实现它并且以尽可能通用的方式实现它,因此我生成的谓词必须遵守一些约束:
我(相信我)不能使用position(),因为这取决于可能在前的谓词.
所有*-of-type伪类等价物都将由宿主语言中的外部函数生成,因为我认为没有办法根据xpath 1中前一个上下文节点的名称构建谓词.
例如,硬编码li:first-of-type为
//li[count(preceding-sibling::li) = 0]
Run Code Online (Sandbox Code Playgroud)
很好.但动态编码*:first-of-type就像
//*[count(preceding-sibling::*[name()=name(.)]) = 0]
<!-- this is probably silly anyway -->
Run Code Online (Sandbox Code Playgroud)
我相信在xpath 1中是不可能的.
所以,这是我提出的"模板"谓词nth-of-type(An+B),宿主语言将填充缺少的值:
<!--
$A represents the step (A) I will insert
$Am represents "mod $A" I will insert if $A != 0
$B represents the offset (B) I will insert
$E represents the element name I will insert
$O represents the operator I will …Run Code Online (Sandbox Code Playgroud)