Hi,
I have written the following code:
// start code
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace MyPredicates
{
static class MyPredicate
{
public static bool Zero(int val)
{
return val == 0;
}
}
class Program
{
static void Main(string[] args)
{
int[] aTmp = new int[] { 0, 0, 33, 0, 0, 22, 0, 0, 12};
foreach(int i in aTmp)
Console.Write(i .ToString() + ',');
Console.WriteLi ne();
Console.WriteLi ne(System.Array .FindLastIndex( aTmp,
MyPredicate.Zer o));
}
}
}
// end code
Now I'd like to perform a FindLastIndex but with MyPredicate.Zer o
negate, something like:
Console.WriteLi ne(System.Array .FindLastIndex( aTmp, !MyPredicate.Ze ro));
How may I compound (better if inplace) the logical not and
MyPredicate.Zer o to get a valid predicate?
TIA.
Marco. 2 1917
Marco,
You can't really do something like that now in C#. The reason is you
need to pass in a Predicate<int> to the method, and you can't get the result
of the previous predicate into the one you really need (which is
Predicate<bool> ).
In C# 3.0, it will be much easier to do, since you will write the
function like this:
Console.WriteLi ne(System.Array .FindLastIndex( aTmp, i => i == 0));
And when you want to change it, you just do this:
Console.WriteLi ne(System.Array .FindLastIndex( aTmp, i => i != 0));
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Marco Segurini" <ma***********@ virgilio.it> wrote in message
news:OB******** ******@tk2msftn gp13.phx.gbl... Hi,
I have written the following code:
// start code using System; using System.Collecti ons.Generic; using System.Text;
namespace MyPredicates {
static class MyPredicate { public static bool Zero(int val) { return val == 0; } }
class Program { static void Main(string[] args) { int[] aTmp = new int[] { 0, 0, 33, 0, 0, 22, 0, 0, 12}; foreach(int i in aTmp) Console.Write(i .ToString() + ','); Console.WriteLi ne();
Console.WriteLi ne(System.Array .FindLastIndex( aTmp, MyPredicate.Zer o)); } } } // end code
Now I'd like to perform a FindLastIndex but with MyPredicate.Zer o negate, something like:
Console.WriteLi ne(System.Array .FindLastIndex( aTmp, !MyPredicate.Ze ro));
How may I compound (better if inplace) the logical not and MyPredicate.Zer o to get a valid predicate?
TIA. Marco.
The best I can come up with is something like:
static class MyPredicate
{
public static bool Zero(int val)
{
return val == 0;
}
}
class PredicateNegato r<T>
{
Predicate<T> _pred;
public PredicateNegato r(Predicate<T> pred)
{
_pred = pred;
}
public bool Negate(T val)
{
return !_pred(val);
}
}
class Program
{
static void Main(string[] args)
{
int[] aTmp = new int[] { 0, 0, 33, 0, 0, 22, 0, 0, 12 };
foreach (int i in aTmp)
Console.Write(i .ToString() + ',');
Console.WriteLi ne();
Console.WriteLi ne(System.Array .FindLastIndex( aTmp,
MyPredicate.Zer o));
Console.WriteLi ne(System.Array .FindLastIndex( aTmp, new
PredicateNegato r<int>(MyPredic ate.Zero).Negat e));
Console.ReadLin e();
}
}
It's not very elegant - in particlualr it's annoying to have to include the
"<int>" in the "new PredicateNegato r<int>" (there may be a way round this
but I'm very new to generics) - but it works.
Chris Jobson
"Marco Segurini" <ma***********@ virgilio.it> wrote in message
news:OB******** ******@tk2msftn gp13.phx.gbl... Hi,
I have written the following code:
// start code using System; using System.Collecti ons.Generic; using System.Text;
namespace MyPredicates {
static class MyPredicate { public static bool Zero(int val) { return val == 0; } }
class Program { static void Main(string[] args) { int[] aTmp = new int[] { 0, 0, 33, 0, 0, 22, 0, 0, 12}; foreach(int i in aTmp) Console.Write(i .ToString() + ','); Console.WriteLi ne();
Console.WriteLi ne(System.Array .FindLastIndex( aTmp, MyPredicate.Zer o)); } } } // end code
Now I'd like to perform a FindLastIndex but with MyPredicate.Zer o negate, something like:
Console.WriteLi ne(System.Array .FindLastIndex( aTmp, !MyPredicate.Ze ro));
How may I compound (better if inplace) the logical not and MyPredicate.Zer o to get a valid predicate?
TIA. Marco. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Uwe Mayer |
last post by:
Hi,
I know the python community is not very receptive towards extending the
python syntax. Nevertheless I'd like to make a suggestion and hear your pro
and cons.
I want so suggest a concatenation operator like in mathematics: °
such that:
a(b(c(d))) <=> a°b°c(d)
|
by: Oliver Fromme |
last post by:
Hi,
I'm trying to write a Python function that parses
an expression and builds a function tree from it
(recursively).
During parsing, lambda functions for the the terms
and sub-expressions are constructed on the fly.
Now my problem is lazy evaluation. Or at least I
think it is. :-)
|
by: hall |
last post by:
I accidently overloaded a static member function that I use as predicate
in the std::sort() for a vector and ended up with a compiler error. Is
this kind of overload not allowed for predicates and if so, why not?
Shouldn the compiler be able to tell which of he overloaded functions to
use?
The second A::comp() is the one I accidently added and gives the error
message (in Borland C++Builder 6)
Unit1.cpp E2285 Could not find a match for
|
by: matthias_k |
last post by:
Hi,
I need to sort elements of a std::list using a function predicate,
something like:
bool predicate( const& M m1, const& M m2 ) {
return m1.somedata < m2.somedata;
}
I tried to call std::list::sort with such a predicate but that doesn't work:
|
by: Last Timer |
last post by:
I have these interview questions and could use some help in polishing
my knowledge:
a) what are put back functions?
b) is containment better than private inheritence?
(please contrast it with composition)
c) can we have flat out separate classes with friend functions?
Thanks for your help
| |
by: Frederik Vanderhaegen |
last post by:
Hi,
Can anyone explain me the difference between aggregation and composition?
I know that they both are "whole-part" relationships and that composition
parts are destroyed when the composition whole is destroyed.
Under a "whole-part" relationship I understand the following:
the whole can't exists without the parts, but can the parts exist without
the hole?
f.e.: a car can't exist without an engine
private engine _Engine
|
by: adebaene |
last post by:
Hello all,
Has everyone tried to use the functions taking a Predicate in Generics
container in C++/CLI?
Say I have a List<MyClass^>^ my_array, and I want to call RemoveAll on
it. How would you do this?
The most natural approach would be :
|
by: =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?= |
last post by:
Hello guys,
I was wondering if it was possible to reference a boolean predicate in
a variable. Basically I want to do with the boolean predicate what you
would do with any other variable; I want it to apply to many places
without having to update all those different locations if the
predicate needs to change.
This is not a full stylesheet, just snippets of the idea
|
by: puzzlecracker |
last post by:
The statement is taken from FAQ . What about non-virtual
functions? Can they be overriden? I still don't see a good
justification to prefer private inheritance over composition. In
fact, I have never seen it in a commercial code. If someone did,
please share the use-case and decisions behind it.
Thanks
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |