473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Predicate as functions composition

Hi,

I have written the following code:

// start code
using System;
using System.Collections.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.WriteLine();

Console.WriteLine(System.Array.FindLastIndex(aTmp,
MyPredicate.Zero));
}
}
}
// end code
Now I'd like to perform a FindLastIndex but with MyPredicate.Zero
negate, something like:

Console.WriteLine(System.Array.FindLastIndex(aTmp, !MyPredicate.Zero));

How may I compound (better if inplace) the logical not and
MyPredicate.Zero to get a valid predicate?

TIA.
Marco.
Nov 17 '05 #1
2 1897
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.WriteLine(System.Array.FindLastIndex(aTmp, i => i == 0));

And when you want to change it, you just do this:

Console.WriteLine(System.Array.FindLastIndex(aTmp, i => i != 0));

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com


"Marco Segurini" <ma***********@virgilio.it> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
Hi,

I have written the following code:

// start code
using System;
using System.Collections.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.WriteLine();

Console.WriteLine(System.Array.FindLastIndex(aTmp,
MyPredicate.Zero));
}
}
}
// end code
Now I'd like to perform a FindLastIndex but with MyPredicate.Zero negate,
something like:

Console.WriteLine(System.Array.FindLastIndex(aTmp, !MyPredicate.Zero));

How may I compound (better if inplace) the logical not and
MyPredicate.Zero to get a valid predicate?

TIA.
Marco.

Nov 17 '05 #2
The best I can come up with is something like:

static class MyPredicate
{
public static bool Zero(int val)
{
return val == 0;
}
}

class PredicateNegator<T>
{
Predicate<T> _pred;

public PredicateNegator(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.WriteLine();

Console.WriteLine(System.Array.FindLastIndex(aTmp,
MyPredicate.Zero));
Console.WriteLine(System.Array.FindLastIndex(aTmp, new
PredicateNegator<int>(MyPredicate.Zero).Negate));

Console.ReadLine();
}
}

It's not very elegant - in particlualr it's annoying to have to include the
"<int>" in the "new PredicateNegator<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**************@tk2msftngp13.phx.gbl...
Hi,

I have written the following code:

// start code
using System;
using System.Collections.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.WriteLine();

Console.WriteLine(System.Array.FindLastIndex(aTmp,
MyPredicate.Zero));
}
}
}
// end code
Now I'd like to perform a FindLastIndex but with MyPredicate.Zero negate,
something like:

Console.WriteLine(System.Array.FindLastIndex(aTmp, !MyPredicate.Zero));

How may I compound (better if inplace) the logical not and
MyPredicate.Zero to get a valid predicate?

TIA.
Marco.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
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...
53
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...
4
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...
5
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...
5
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...
4
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...
0
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...
2
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...
8
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.