predicate

Is it possible to cache a value evaluated in a lambda expression?

匆匆过客 提交于 2019-12-21 02:56:10
问题 In the ContainsIngredients method in the following code, is it possible to cache the p.Ingredients value instead of explicitly referencing it several times? This is a fairly trivial example that I just cooked up for illustrative purposes, but the code I'm working on references values deep inside p eg. p.InnerObject.ExpensiveMethod().Value edit: I'm using the PredicateBuilder from http://www.albahari.com/nutshell/predicatebuilder.html public class IngredientBag { private readonly Dictionary

Guava - How to remove from a list, based on a predicate, keeping track of what was removed?

南笙酒味 提交于 2019-12-20 18:31:42
问题 I have an ArrayList to be filtered, and various Guava Predicate s to filter it with. This list will have only 50-100 elements. I was planning on Iterables.removeIf using each predicate in turn. It is perhaps not maximally efficient but never mind (at least removeIf has some optimization for RandomAccess lists) For debugging, I want to concisely log what each predicate did. e.g. Pred0 removed [a, c, g] Pred1 removed [] Pred2 removed [b, f] There are some obvious hack solutions but what would

Can you pass an additional parameter to a predicate?

时光毁灭记忆、已成空白 提交于 2019-12-20 10:33:10
问题 I'm trying to filter a vector so it contains only a specific value. e.g. Make sure the vector only contains elements of the value "abc." Right now, I'm trying to achieve this with remove_copy_if . Is there any way to pass an additional parameter to a predicate when using one of std's algorithms? std::vector<std::string> first, second; first.push_back("abc"); first.push_back("abc"); first.push_back("def"); first.push_back("abd"); first.push_back("cde"); first.push_back("def"); std::remove_copy

Why doesn't Java 8's Predicate<T> extend Function<T, Boolean>

丶灬走出姿态 提交于 2019-12-20 10:29:42
问题 If I wrote the Predicate interface, I'd want to encode in the interface the fact that it's just a function that returns a primitive boolean , like this: @FunctionalInterface public interface Predicate<T> extends Function<T, Boolean> { boolean test(T t); @Override default Boolean apply(T t) { return Boolean.valueOf(test(t)); } } I was wondering, is there a compelling reason Java 8 API designers chose to keep the Predicate completely separate from Function ? Is there some evidence that they

How to convert an Expression<Func<T, bool>> to a Predicate<T>

狂风中的少年 提交于 2019-12-20 08:24:31
问题 I have a method that accepts an Expression<Func<T, bool>> as a parameter. I would like to use it as a predicate in the List.Find() method, but I can't seem to convert it to a Predicate which List takes. Do you know a simple way to do this? public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new() { var list = GetList<T>(); var predicate = [what goes here to convert expression?]; return list.Find(predicate); } Update Combining answers from tvanfosson and 280Z28, I am

Why C++ associative containers predicate not transparent by default?

僤鯓⒐⒋嵵緔 提交于 2019-12-20 02:30:10
问题 Since C++14 we have std::less<void> that is transparent and more usefull in most cases, so is there reasons why, for example, std::set still has std::less<Key> as a predicate by default, not an std::less<void> except historical reasons. Useful cases: std::set<std::string>::find with std::string_view , etc. 回答1: It would break current working code to do so. Imagine I have struct my_type { int id; int bar; }; namespace std { template<> struct less<my_type> { bool operator()(my_type const& lhs,

Why C++ associative containers predicate not transparent by default?

旧时模样 提交于 2019-12-20 02:30:03
问题 Since C++14 we have std::less<void> that is transparent and more usefull in most cases, so is there reasons why, for example, std::set still has std::less<Key> as a predicate by default, not an std::less<void> except historical reasons. Useful cases: std::set<std::string>::find with std::string_view , etc. 回答1: It would break current working code to do so. Imagine I have struct my_type { int id; int bar; }; namespace std { template<> struct less<my_type> { bool operator()(my_type const& lhs,

Combining Predicates

二次信任 提交于 2019-12-19 02:27:28
问题 Is there any way that you can combine predicates? Lets say I have something like this: 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,

Prolog: How to tell if a predicate is deterministic or not

回眸只為那壹抹淺笑 提交于 2019-12-18 15:54:31
问题 So from what I understand about deterministic predicates: Deterministic predicate = 1 solution Non-deterministic predicate = multiple solutions Are there any type of rules as to how you can detect if the predicate is one or the other? Like looking at the search tree, etc. 回答1: There is no clear, generally accepted consensus about these notions. However, they are usually based rather on the observed answers and not based on the number of solutions. In certain contexts the notions are very

C#, Linq2SQL: Creating a predicate to find elements within a number of ranges

天大地大妈咪最大 提交于 2019-12-18 13:24:38
问题 Lets say I have something called Stuff in my database, with a property called Id. From the user I get a sequence of selected Range objects (or rather I create them from their input) with the Ids they want. A stripped down version of that struct looks like this: public struct Range<T> : IEquatable<Range<T>>, IEqualityComparer<Range<T>> { public T A; public T B; public Range(T a, T b) { A = a; B = b; } ... } So one could for example have gotten: var selectedRange = new List<Range<int>> { new