predicate

How do I create a set with std::pair thats sorted based on the ::second pair member using bind

不羁岁月 提交于 2019-12-05 13:54:50
I know I could use the following: template <typename Pair> struct ComparePairThroughSecond : public std::unary_function<Pair, bool> { bool operator ()(const Pair& p1, const Pair& p2) const { return p1.second < p2.second; } }; std::set<std::pair<int, long>, ComparePairThroughSecond> somevar; but wondered if it could be done with boost::bind How about the following one. I'm using boost::function to 'erase' the actual type of the comparator. The comparator is created using boost:bind itself. typedef std::pair<int, int> IntPair; typedef boost::function<bool (const IntPair &, const IntPair &)>

Problem with `\\+` in Prolog queries with variables

假如想象 提交于 2019-12-05 12:29:43
I'm reading "Seven languages in seven weeks" atm, and I'm stumped over some Prolog query that I don't understand the 'no' response to. The friends.pl file looks like this: likes(wallace, cheese). likes(grommit, cheese). likes(wendolene, sheep). friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z). I can do some trivial queries on it, such as: | ?- ['friends']. compiling /home/marc/btlang-code/code/prolog/friends.pl for byte code... /home/marc/btlang-code/code/prolog/friends.pl compiled, 12 lines read - 994 bytes written, 8 ms yes | ?- friend(wallace,grommit). yes | ?- friend(wallace,wendolene).

prevent window.onhashchange from executing when hash is set via JavaScript

泪湿孤枕 提交于 2019-12-05 11:28:07
I use the window.onhashchange function to execute code when the User changes the hash of the page: window.onhashchange = function() { /* do something */ }; In some functions I also set the hash via JavaScript: window.location.hash = "#abc"; I want to prevent the onhashchange event from firing when I set the hash via JavaScript. What I have tried so far: var currently_setting_hash = false; window.onhashchange = function() { if (currently_setting_hash) return; //... } currently_setting_hash = true; window.location.hash = "#abc"; currently_setting_hash = false; That didn't work because the event

How to implement a not_all_equal/1 predicate

﹥>﹥吖頭↗ 提交于 2019-12-05 10:04:24
How would one implement a not_all_equal/1 predicate, which succeeds if the given list contains at least 2 different elements and fails otherwise? Here is my attempt (a not very pure one): not_all_equal(L) :- ( member(H1, L), member(H2, L), H1 \= H2 -> true ; list_to_set(L, S), not_all_equal_(S) ). not_all_equal_([H|T]) :- ( member(H1, T), dif(H, H1) ; not_all_equal_(T) ). This however does not always have the best behaviour: ?- not_all_equal([A,B,C]), A = a, B = b. A = a, B = b ; A = a, B = b, dif(a, C) ; A = a, B = b, dif(b, C) ; false. In this example, only the first answer should come out,

Java split stream by predicate into stream of streams

北慕城南 提交于 2019-12-05 08:30:02
I have hundreds of large (6GB) gziped log files that I'm reading using GZIPInputStream s that I wish to parse. Suppose each one has the format: Start of log entry 1 ...some log details ...some log details ...some log details Start of log entry 2 ...some log details ...some log details ...some log details Start of log entry 3 ...some log details ...some log details ...some log details I'm streaming the gziped file contents line by line through BufferedReader.lines() . The stream looks like: [ "Start of log entry 1", " ...some log details", " ...some log details", " ...some log details", "Start

Adding a parameter to a FindAll for a Generic List in C#

若如初见. 提交于 2019-12-05 08:27:06
I have a list of objects that I want to filter by an integer parameter List<testObject> objectList = new List<testObject>(); // populate objectList with testObjects objectList.FindAll(GroupLevel0); private static bool GroupLevel0(testObject item) { return item._groupLevel == 0; } private class testObject { public string _FieldSQL = null; public int _groupLevel; } What I'm looking to do is to make GroupLevel0 take in an integer as a parameter instead of hardcoding to 0. I'm working in .NET 2.0 so lambda expressions are a no-go. Is it even possible to pass a parameter into a predicate? Thank you

C# predicate list passed to Linq Where clause

南楼画角 提交于 2019-12-05 03:07:05
I have a long Linq Where clause that I would like to populate with a predicate list. List<Expression<Func<Note, bool>>> filters = new List<Expression<Func<Note, bool>>>(); filters.Add(p => p.Title != null && p.Title.ToLower().Contains(searchString)); filters.Add(p => p.Notes != null && p.Notes.ToLower().Contains(searchString)); filters.Add(GlobalSearchUser((List < User > users = new List<User>() { p.user1, p.user2, p.user3, p.user4 }), searchString)); notes = dataAccess.GetList<Note>(pn => pn.ProjectVersionID == projectVersionID, filterExtensions.ToArray()) .Where(filters.ToArray()).Take(10)

Java join collections using functor

[亡魂溺海] 提交于 2019-12-05 03:01:58
问题 2 collections are given with the same number of elements, say List<String> . What are elegant ways in JAVA to apply a functor on each 2 elements of collections with corresponding indexes? Say, one example could be: List<String> = { "APPLE", "PEAR" }; List<String> = { "BANANA", "ORANGE" }; A predicate that joins string together will result in the following List<String> : List<String> = { "APPLEBANANA", "PEARORANGE" }; 回答1: Akin to the functors found in Apache Commons Collections, I have

Pass std algos predicates by reference in C++

流过昼夜 提交于 2019-12-05 02:43:14
I am trying to remove elements from a std::list and keep some stats of deleted elements. In order to do so, I use the remove_if function from the list, and I have a predicate. I would like to use this predicate to gather statistics. Here is the code for the predicate: 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; } }; And here is the code for the algo: std::list < int

Sorting names with numbers correctly

萝らか妹 提交于 2019-12-05 02:41:03
问题 For sorting item names, I want to support numbers correctly. i.e. this: 1 Hamlet 2 Ophelia ... 10 Laertes instead of 1 Hamlet 10 Laertes 2 Ophelia ... Does anyone know of a comparison functor that already supports that? (i.e. a predicate that can be passed to std::sort ) I basically have two patterns to support: Leading number (as above), and number at end, similar to explorer: Dolly Dolly (2) Dolly (3) (I guess I could work that out: compare by character, and treat numeric values differently