Build an dynamic if statement? - c#

How can i build an dynamic if statement that also includes <, >, ==, <=, >=
I want to build an dynamic if statement that is not looking like this:
if (seconds < choosedSeconds)
{
}
else if (seconds > choosedSeconds)
{
}
else if(seconds >= choosedSeconds)
{
}
else if(seconds == choosedSeconds)
{
}
This is what i want it to look like
if(seconds myOperator choosedSeconds) // or minutes, hours and so on
{
}
I just want to have that in one statement.
Do i have to build a struct for that?
An example would be nice.

You can work with delegates and lambda expressions
void MyMethod (Func<int, int, bool> comparison)
{
int seconds = ...;
int chosenSeconds = ...;
if (comparison(seconds, chosenSeconds)) {
...
}
}
You can call it like this
MyMethod((a, b) => a <= b);
or
MyMethod((a, b) => a == b);
Any comparison will work as long as the expression is a Boolean expression:
MyMethod((a, b) => a % b == 0);
MyMethod((a, b) => array[a] == 100 * b + 7);

interface IMyConditionEvaluator
{
bool EvaluateCondition(int x, int y);
}
…
IMyConditionEvaluator e = new SomeSpecificConditionEvaluator();
…
if (e.EvaluateCondition(seconds, choosedSeconds))
{
…
}
Now go ahead and create as many classes implementing IMyConditionEvaluator as you wish.

You could go with predicates, i.e:
public bool IsExactlyOneSecond(TimeSpan timeSpan)
{
return timeSpan.TotalSeconds == 1.0;
}
public bool IsMoreThanOneSecond(TimeSpan timeSpan)
{
return timeSpan.TotalSeconds > 1.0;
}
Then you probably have some method taking the predicate as input:
private void Process(TimeSpan timeSpan, Predicate<TimeSpan> test)
{
if (test(timeSpan))
{
// Do something
}
}
And you use it like this:
Process(timeSpan, IsExactlyOneSecond);
Or
Process(timeSpan, IsMoreThanOneSecond);

Maybe you need something like this? This generic method compares two values using the specified comparison type.
public enum ComparisonType
{
Equal,
Less,
Greater,
LessOrEqual,
GreaterOrEqual
}
public static bool Compare<T>(T a, ComparisonType compType, T b)
where T : IComparable<T>
{
switch (compType)
{
case ComparisonType.Equal:
return a.CompareTo(b) == 0;
case ComparisonType.Less:
return a.CompareTo(b) < 0;
case ComparisonType.Greater:
return a.CompareTo(b) > 0;
case ComparisonType.LessOrEqual:
return a.CompareTo(b) <= 0;
case ComparisonType.GreaterOrEqual:
return a.CompareTo(b) >= 0;
}
throw new ApplicationException();
}
Usage example:
if (Compare(seconds, ComparisonType.LessOrEqual, choosenSeconds))
{
// seconds <= choosenSeconds here
}

Take a look at
Expression Trees

Related

How to iterate only distinct string values by custom substring equality

Similar to this question, I'm trying to iterate only distinct values of sub-string of given strings, for example:
List<string> keys = new List<string>()
{
"foo_boo_1",
"foo_boo_2,
"foo_boo_3,
"boo_boo_1"
}
The output for the selected distinct values should be (select arbitrary the first sub-string's distinct value):
foo_boo_1 (the first one)
boo_boo_1
I've tried to implement this solution using the IEqualityComparer with:
public class MyEqualityComparer : IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
int xIndex = x.LastIndexOf("_");
int yIndex = y.LastIndexOf("_");
if (xIndex > 0 && yIndex > 0)
return x.Substring(0, xIndex) == y.Substring(0, yIndex);
else
return false;
}
public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
foreach (var key in myList.Distinct(new MyEqualityComparer()))
{
Console.WriteLine(key)
}
But the resulted output is:
foo_boo_1
foo_boo_2
foo_boo_3
boo_boo_1
Using the IEqualityComparer How do I remove the sub-string distinct values (foo_boo_2 and foo_boo_3)?
*Please note that the "real" keys are a lot longer, something like "1_0_8-B153_GF_6_2", therefore I must use the LastIndexOf.
Your current implementation has some flaws:
Both Equals and GetHashCode must never throw exception (you have to check for null)
If Equals returns true for x and y then GetHashCode(x) == GetHashCode(y). Counter example is "abc_1" and "abc_2".
The 2nd error can well cause Distinct return incorrect results (Distinct first compute hash).
Correct code can be something like this
public class MyEqualityComparer : IEqualityComparer<string> {
public bool Equals(string x, string y) {
if (ReferenceEquals(x, y))
return true;
else if ((null == x) || (null == y))
return false;
int xIndex = x.LastIndexOf('_');
int yIndex = y.LastIndexOf('_');
if (xIndex >= 0)
return (yIndex >= 0)
? x.Substring(0, xIndex) == y.Substring(0, yIndex)
: false;
else if (yIndex >= 0)
return false;
else
return x == y;
}
public int GetHashCode(string obj) {
if (null == obj)
return 0;
int index = obj.LastIndexOf('_');
return index < 0
? obj.GetHashCode()
: obj.Substring(0, index).GetHashCode();
}
}
Now you are ready to use it with Distinct:
foreach (var key in myList.Distinct(new MyEqualityComparer())) {
Console.WriteLine(key)
}
Your GetHashCode method in your equality comparer is returning the hash code for the entire string, just make it hash the substring, for example:
public int GetHashCode(string obj)
{
var index = obj.LastIndexOf("_");
return obj.Substring(0, index).GetHashCode();
}
For a more succinct solution that avoids using a custom IEqualityComparer<>, you could utilise GroupBy. For example:
var keys = new List<string>()
{
"foo_boo_1",
"foo_boo_2",
"foo_boo_3",
"boo_boo_1"
};
var distinct = keys
.Select(k => new
{
original = k,
truncated = k.Contains("_") ? k.Substring(0, k.LastIndexOf("_")) : k
})
.GroupBy(k => k.truncated)
.Select(g => g.First().original);
This outputs:
foo_boo_1
boo_boo_1

C# - Passing functions (with arguments) as arguments of a function

I need to create a function, that will take another functions (always different quantity of them). Can someone help me please?
Function DoThisFunction will have different types and quantity of parameters.
there can be different number of condition functions.
I'll try to show it here:
bool MyFunction(condition1(args), condition2(args), condition3(args), ... , DoThisFunction(args))
{
...
if (condition1(int x) == true && condition2(int x, string C) == 5)
{
DoThisFunction(par1, par2, par3 ...);
return true;
}
}
bool condition1(int x)
{
if (x>5)
return true;
else
return false;
}
int condition2(int x, string C)
{
....
return par1;
}
etc...
Then I need to call:
bool z = MyFunction(condition1(int x)==true, condition2(int x, string C)==5, DoThisFunction(par1, anotherArguments ...))
I would like to suggest another approach for your code.
Maybe, you can keep a separated list with all the functions you need to verify, and run each method inside a very simple loop (foreach), in this case:
the code will be very friendly (easy to understand)
better maintainability
you can review less code and add more functionality (for instance, you may inject some code and just add another Func<> into your List<>)
Please, take a look at the following example:
static class Program
{
private static void Main(string[] args)
{
var assertions = new List<Func<object[], bool>>
{
Assertion1,
Assertion2,
Assertion3
};
var yourResult = Assert(assertions, 1, "1", true);
Console.WriteLine(yourResult); // returns "True" in this case
Console.ReadLine();
}
private static bool Assert(IEnumerable<Func<object[], bool>> assertions, params object[] args)
{
// the same as
// return assertions.Aggregate(true, (current, assertion) => current & assertion(args));
var result = true;
foreach (var assertion in assertions)
result = result & assertion(args);
return result;
}
private static bool Assertion1(params object[] args)
{
return Convert.ToInt32(args[0]) == 1;
}
private static bool Assertion2(params object[] args)
{
return Convert.ToInt32(args[0]) == Convert.ToInt32(args[1]);
}
private static bool Assertion3(params object[] args)
{
return Convert.ToBoolean(args[2]);
}
}
This solution seems to generic for your problem.
For checking preconditions before executing methods have a look at Code Contracts
You can use functor like the following:
private bool MyFunction(Func<int, bool> condition1, Func<int,string,int> condition2, Func<int,string,int, int> doThisFunction, int x, string str)
{
if (condition1(x) && condition2(x, str) == 5)
return doThisFunction(x, str, x) == 10;
return false;
}
Then call this function in your code like the below:
MyFunction(x => x > 5 ? true : false, (x, C) => C.Length == x * 5 ? 5 : C.Length,
(x, str, y) =>
{
if (x + y > str.Length)
return 5;
else if (x * y > 5)
return 10;
else
return 15;
}, 10, "Csharp");

What happens when ==, CompareTo(), and Equals() do not agree?

I have a program I wrote some years back to find "good" binary operators for bytes; byte A is left multiplied by byte B to yield byte C. The operator is defined as 256x256 byte matrix. A stripped down version of the class implementation is below.
Equals() is true IFF all 65536 bytes in the array are the same.
CompareTo() compares the Linearity of the operator into a continuum of more linear (bad for cryto) to less linear (good for crypto).
So it is possible for two instances, A and B, that both of the following are true:
A.Equals(B) = false
(A.ComparesTo(B) == 0) = true
My question is less: Is this a good idea? I know the answer is No, but given the large computational cost of measuring linearity and the narrow nature of my problem this design works. Also code similar to:
if (localMinimumOperator < globalMinimumOperator)
{
localMinimumOperator = globalMinimumOperator;
}
is easier for me to read.
My question is: What are the consequences of this divergence among: ==, CompareTo()== 0, and Equals()? or alternately:
Is there list of which LINQ extensions methods describing which extension use which interface (IEquatable or IComparable)?
Something more concise than this MSDN article on Enumerable?
For example:
IEnumerable<BinaryOperation> distinct = orgList.Distinct();
calls Equals(BinaryOperator) as per: Enumerable.Distinct<TSource> Method as does Contains(). I understand that Sort() and OrderBy() use calls to CompareTo().
But what about FindFirst() and BinarySearch()?
My example class:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Jww05
{
public class BinaryOperation : IEquatable<BinaryOperation>, IComparable<BinaryOperation>
{
#region ClassMembers
public List<List<byte>> TruthTable
{
get
{
// I don't like giving out the underlying list if I help it
var retVal = new List<List<byte>>(OperatorDefintion);
return retVal;
}
}
// private data store for TruthTable
private List<List<byte>> OperatorDefintion { get; set; }
public BinaryOperation()
{
// initial state is the Identity operator
OperatorDefintion = new List<List<byte>>();
for (int i = 0; i < 256; i++)
{
var curRow = new List<byte>();
for (int j = 0; j < 256; j++)
{
curRow.Add((byte)(i + j));
}
OperatorDefintion.Add(curRow);
}
}
private long MeasureOperatorLinearity()
{
var diagonalOffsets = new byte[] { 255, 0, 1 };
/*
* Code that measures linearity in the original code used the Fast Walsh Hadamard Transform.
* That should go here, but it is removed because the FWHT is clutter for the purposes of this question.
*
* Since I needed a stub for this, I decided to exacerbate the differnece
* between CompareTo() == 0 and Equals()
* by returning an arbitrary int in lieu of the long CPU intensive Fast Walsh Hadamard Transform.
*
* If the matrices are identical on an element-by-element basis, then the Faux Linearity will be the the same.
* If the faux linearity (sum of terms on the main diagonal and corners) are the same, the underlying matrices could be different on an element-by-element basis.
*/
long fauxLinearityMeasure = 0;
for (var currRow = 0; currRow < OperatorDefintion.Count(); ++currRow)
{
fauxLinearityMeasure *= 5;
fauxLinearityMeasure = diagonalOffsets.Select(diagonalOffset => (byte)(currRow + diagonalOffset))
.Aggregate(fauxLinearityMeasure, (current, offestedIndex) => current + (OperatorDefintion[offestedIndex][currRow]));
}
return (int)fauxLinearityMeasure;
}
#endregion ClassMembers
#region ComparisonOperations
public int CompareTo(BinaryOperation other)
{
long otherLinearity = other.MeasureOperatorLinearity();
long thisLinearity = MeasureOperatorLinearity();
long linearityDiff = thisLinearity - otherLinearity;
// case the differnece of the linarity measures into {-1, 0, 1}
return (0 < linearityDiff) ? 1
: (0 > linearityDiff) ? -1
: 0;
}
public static bool operator >(BinaryOperation lhs, BinaryOperation rhs)
{
if (ReferenceEquals(null, lhs) ||
ReferenceEquals(null, rhs))
{
return false;
}
return (0 < lhs.CompareTo(rhs));
}
public static bool operator <(BinaryOperation lhs, BinaryOperation rhs)
{
if (ReferenceEquals(null, lhs) ||
ReferenceEquals(null, rhs))
{
return false;
}
return (0 > lhs.CompareTo(rhs));
}
public static bool operator <=(BinaryOperation lhs, BinaryOperation rhs)
{
if (ReferenceEquals(null, lhs) ||
ReferenceEquals(null, rhs))
{
return false;
}
// equals is cheap
if (lhs.Equals(rhs))
{
return true;
}
return (0 > lhs.CompareTo(rhs));
}
public static bool operator >=(BinaryOperation lhs, BinaryOperation rhs)
{
if (ReferenceEquals(null, lhs) ||
ReferenceEquals(null, rhs))
{
return false;
}
// equals is cheap
if (lhs.Equals(rhs))
{
return true;
}
return (0 < lhs.CompareTo(rhs));
}
#endregion ComparisonOperations
#region EqualityOperators
public bool Equals(BinaryOperation other)
{
if (ReferenceEquals(null, other))
{
return false;
}
var otherTruthTable = other.TruthTable;
var thisTruthTable = TruthTable;
var isEquals = true;
for (int currRow = 0; currRow < thisTruthTable.Count(); ++currRow)
{
isEquals = isEquals && thisTruthTable[currRow].SequenceEqual(otherTruthTable[currRow]);
}
return isEquals;
}
public override bool Equals(object obj)
{
return Equals(obj as BinaryOperation);
}
public override int GetHashCode()
{
return OperatorDefintion.SelectMany(currRow => currRow)
.Aggregate(1, (current, currByte) => current * 5 + currByte);
}
public static bool operator ==(BinaryOperation lhs, BinaryOperation rhs)
{
if (ReferenceEquals(null, lhs) ||
ReferenceEquals(null, rhs))
{
return false;
}
return (0 == lhs.CompareTo(rhs));
}
public static bool operator !=(BinaryOperation lhs, BinaryOperation rhs)
{
if (ReferenceEquals(null, lhs) ||
ReferenceEquals(null, rhs))
{
return false;
}
return (0 != lhs.CompareTo(rhs));
}
#endregion EqualityOperators
}
}
What are the consequences of this divergence among: ==, CompareTo()== 0, and Equals()?
Someone looking at your code in the future will truly hate you.
or alternately: Is there list of which linq extensions methods describing which extension use which interface (IEquitable or IComparable)?
I think that you've found most of it by yourself. A good rule of thumb is that usually there is nothing surprising in what interface is used by which LINQ function (no surprises is one of features of good design - unlike yours). For example: it's quite obvious that to sort elements it is necessary to know in which particular order should elements go, equality/inequality alone are not sufficient for this. BinarySearch also needs to know "which way to go" during search - if element is larger than current it re-curses into upper part of sorted array, if smaller it goes into lower. Again: obviously it needs IComparable. For Distinct Equals and GetHashCode suffice - sorting is not needed to determine a set of unique elements. And so on.

How to determine if three ints are all equal

Hi say I have three ints: value1, value2 and value3.
How do I best determine if they are all the same?
I tried:
return value1 == value2 == value3
But this said:
Operator '==' cannot be applied to operands of type 'bool' and 'int'.
So I guess it compares the first two which returns a boolean which it tries to compare to the third.
I could go:
return value1 == value2 && value2 == value3;
But this seems to be getting untidy.
Anybody have a good suggestion?
The second seems just fine to me.
As the list gets longer, that could get unwieldy. In which case I'd write an extension method along the lines of AllSame.
bool AllSame(this IEnumerable<int> list)
{
bool first = true;
int comparand = 0;
foreach (int i in list) {
if (first) comparand = i;
else if (i != comparand) return false;
first = false;
}
return true;
}
or use the params keyword:
bool AllSame(params int[] list)
{
return (list as IEnumerable<int>).AllSame();
}
Then you can just write:
if (AllSame(value1, value2, value3, value4, value5)) ...
That seems fine to me. The only comment I have is that you should introduce an 'explaining variable' for the equation. Besides explaining the calculation, the return now provides a nice place for a breakpoint or a tracepoint when inspecting the result.
bool allThreeAreEqual = value1 == value2 && value2 == value3;
return allThreeAreEqual;
I modified my original answer to include a method that is more general purpose and that does not rely on LINQ or extension methods. I think it's safe to assume this method would be more performant based on the fact that it doesn't have to enumerate the entire list to determine uniqueness when there are values that are different early on in the list.
class Program
{
static void Main(string[] args)
{
int value1 = 1, value2 = 2, value3 = 1;
Console.WriteLine(AllAreEqual<int>(value1, value2, value3));
Console.Write("V2: 1 value ");
Console.WriteLine(AllAreEqual_V2<int>(1));
Console.Write("V2: no value ");
Console.WriteLine(AllAreEqual_V2<int>());
Console.Write("V2: 3 values, same ");
Console.WriteLine(AllAreEqual_V2<int>(1, 1, 1));
Console.Write("V2: 3 values, different ");
Console.WriteLine(AllAreEqual_V2<int>(1, 1, 2));
Console.Write("V2: 2 values, same ");
Console.WriteLine(AllAreEqual_V2<int>(1, 1));
Console.Write("V2: 2 values, different ");
Console.WriteLine(AllAreEqual_V2<int>(1, 2));
Console.ReadKey();
}
static bool AllAreEqual<T>(params T[] args)
{
return args.Distinct().ToArray().Length == 1;
}
static bool AllAreEqual_V2<T>(params T[] args)
{
if (args.Length == 0 || args.Length == 1)
{
return true;
}
if (args.Length == 2)
{
return args[0].Equals(args[1]);
}
T first = args[0];
for (int index = 1; index < args.Length; index++)
{
if (!first.Equals(args[index]))
{
return false;
}
}
return true;
}
}
If you are just looking for elegance (Considering your already have a solution that has nothing wrong with it ) , you could go with good'ol LINQ. This can handle three or more.
class Program
{
static void Main(string[] args)
{
List<int> mylist = new List<int>();
mylist.Add(1);
mylist.Add(1);
mylist.Add(1);
mylist.Add(1);
bool allElementsAreEqual = mylist.All( x => ( x == mylist.First() ));
}
}
You can do it like this also
bool AllSame(int a, int b, int c, int comparand) {
return board[a] == comparand && board[b] == comparand && board[c] == comparand;
}
int nOneInput = 5;
int nTwoInput = 5;
int nThreeInput = 5;
if ((nOneInput + nTwoInput + nThreeInput ) / 3 == nOneInput )
{
// all 3 sides are equal when...
// the sum of all 3 divided by 3 equals one of the values
}
Using LINQ it would be best to us Any(). Why Any() instead of All() is because All() will test the predicate on all the items of the collection while Any() will exit as soon as and item match the predicate.
Here i use a reverse check. I am looking for any item that will be different than the item "a". So as soon it find one different we know they are not all equals so it exit and return true. So it will only test item "b", "c" and "d".
// all values to compare
var a = 4;
var b = 4;
var c = 4;
var d = 8;
var e = 6;
var f = 4;
// return if any of the following is different and negate to get a true
var areSame = (!new[] { b, c, d, e, f}.Any(i => i != a));
If you override equals you can create generic extensions from this that is reusable and can work for multiple type as long as they implement IEqualityComparer<T>
This is what I would do
if((value1 == value2) && (value1 == value3) && (value2 == value3))
{
//Whatever you want here
}
The accepted answer works fine, but I needed to support different types too,
so this is the generic version
public static bool AllSame<T>(params T[] items)
{
var first = true;
T comparand = default;
foreach (var i in items)
{
if (first) comparand = i;
else if (!i.Equals(comparand)) return false;
first = false;
}
return true;
}
usage:
if (AllSame(true, false, true)) // false
if (AllSame(5, 5, 5)) // true
if (AllSame(record1 , record2, record3))

Easy way to compare values of more than 3 variables?

I want to check whether these variables have same values.
EXAMPLE:
int a = 5;
int b = 5;
int c = 5;
int d = 5;
int e = 5;
. . .
int k = 5;
if(a==b && b==c && c==d && d==e && .... && j==k)
{
//this is Complex way and not well understandable.
}
Any easy way to Compare all are same?
LIKE in below example
if(a==b==c==d==e.....j==k)
{
//Understandable but not work
}
how about something like this:
if (Array.TrueForAll<int>(new int[] {a, b, c, d, e, f, g, h, i, j, k },
val => (a == val))) {
// do something
}
With this many variables, would it make sense to move them into an array?
You could then test to see if they are all equal using Linq expressions like myarray.Distinct().Count() == 1; or perhaps myarray.All(r => r == 5);
You could create a var args method to do that:
bool intsEqual (params int[] ints) {
for (int i = 0; i < ints.Length - 1; i++) {
if (ints[i] != ints[i+1]) {
return False;
}
}
return True;
}
Then just call it with all your ints as parameters:
if (intsEqual(a, b, c, d, e, f, g, h, i, j, k)) {
doSomeStuff();
}
Just a thought, but if you can calculate the standard deviation of the entire list, and it is equal to zero, you would have your answer.
Here's an answer on the site that addresses this that may help with that: Standard deviation of generic list?
Interesting problem. Good luck with it.
I agree that the easiest way is to place them all into a list and then use the following to compare. This is in essence looping through and comparing to the first value, but this is a little cleaner.
var match = counts.All(x => x == counts[0])
How about
int common = a;
if (a==common && b==common && c==common && d==common && .... && k==common)
You could write a helper method like this:
public static bool AllEqual<T> (params T[] values) where T : IEquatable<T>
{
if (values.Length < 2) return true;
for (int i = 1; i < values.Length; i++)
if (!values[i].Equals (values[0])) return false;
return true;
}
This will be subtly different to the == operator in special cases, though:
AllEqual (double.NaN, double.NaN).Dump(); // True
(double.NaN == double.NaN).Dump(); // False
It doesn't work because a==b evaluates to a boolean which can't be compared to an integer, c. What you have seems to be the best way.
You might consider putting the values in an array and using a for() loop. It isn't really any simpler, but it might help if the number of values changed.
You could use a variable argument helper function to perform the comparison pretty easily.
static bool CompareLongList(params int[] args)
{
if (args.Length > 1)
{
int value = args[0];
for (int i = 1; i < args.Length; ++i)
{
if (value != args[i])
return false;
}
}
return true;
}
Then you would just use the function as follows
if(CompareLongList(a,b,c,d,e,f,j,h,i,j,k))
{
// True Code
}
I know it's an old question I came across but I was wondering what's wrong with:
if (a == (b & c & d & e & f & g & h & i & j & k))
{
}
Compare the same elements in array:
same = len(uniq([1,2,3,4])) == 1
Use Linq Query.
var greatestInt = new List() { a,b,c,d,e,f}.Max();
Bitwise and is a possible way to check multiple variables for same value.
Such a helper method could of course also check for equality instead of using the '&' operator. Helper method accepting a params array of variables seems to be the right method here. We could adjust this method to accept a generic argument also, but there are only a few data types which support boolean logical operators anyways in C# (and other languages).
Testing with a high value of Int32 to check validity of this:
void Main()
{
int a = 1234567891;
int b = 1234567891;
int c = 1234567891;
int d = 1234567891;
int e = 1234567891;
int f = 1234567891;
int g = 1234567891;
int h = 1234567891;
int i = 1234567891;
int j = 1234567891;
int k = 1234567891;
bool areAllSameValue = IntUtils.AreAllVariablesSameValue(a,b,c,d,e,f,g,h,i,j,k);
areAllSameValue.Dump(); //Linqpad method - dump this code into Linqpad to test
}
public class IntUtils
{
public static bool AreAllVariablesSameValue(params int[] values)
{
if (values == null || !values.Any())
{
return false;
}
int bitWiseAndValue = values[0];
foreach (var value in values)
{
bitWiseAndValue &= value;
}
return bitWiseAndValue == values[0];
}
}
This spots also if one of the values got a different sign (negative number).

Categories

Resources