C
C#17mo ago
Heiholf

❔ Convert void to bool

I know that this question is totally weird and I would never try to use this in clean/maintainable code. However that is not my goal. I'm writing code for Sebastian Lague's Chess Bot Tournament and try to reduce the number of tokens in code (and cannot use unsafe environments). I have three functions like this:
void A() {}
bool B() => false || true; //return value is obviously not the same every time
void C() {}
void A() {}
bool B() => false || true; //return value is obviously not the same every time
void C() {}
And I want to execute them in order and keep the return value of B(). One way to accomplish is like that:
string Execute()
{
A();
bool res = B();
C();
return res ? "Result 1" : "Result 2";
}
string Execute()
{
A();
bool res = B();
C();
return res ? "Result 1" : "Result 2";
}
But it seems to be not the optimal way to minimize tokens. I would except to be a workaround like this:
string Execute2() => A().toFalse() || (B() && C().toTrue()) ? "Yes" : "No";
string Execute2() => A().toFalse() || (B() && C().toTrue()) ? "Yes" : "No";
However, I was unable to find a way to bodge the toTrue/toFalse statements and Google didn't help because it just shows questions of people not understanding what void is. One could obviously wrap A in a function, which invokes it and returns a bool but it generates to many "boilerplat" tokens. Is there any way to achieve this or is C# to securely designed to do this?
15 Replies
anita
anita17mo ago
Can you change the methods a and c?
Heiholf
HeiholfOP17mo ago
No, they are given methods
anita
anita17mo ago
Then I think the only option is to wrap it in a normal method or a lambda.
Heiholf
HeiholfOP17mo ago
I think you are correct. But sadly a function or a lambda create more tokens as the first option.
anita
anita17mo ago
I actually also participate in this challenge so I‘m facing the same problems.
canton7
canton717mo ago
Reading the definition of a token... Do those two versions of the code have different numbers of tokens?
All names (variables, functions, etc.) are counted as a single token, regardless of length. This means that both lines of code: bool a = true; and bool myObscenelyLongVariableName = true; count the same. Additionally, the following things do not count towards the limit: white space, new lines, comments, access modifiers, commas, and semicolons.
Newlines, semicolons, whitespace don't count. Just variable and function names I guess res counts But wouldn't your toFalse count?
Heiholf
HeiholfOP17mo ago
I haven't counted directly but the second version would lead to a syntax change somewhere else which would overall reduce tokens
canton7
canton717mo ago
I guess you might be able to do something like:
try
{
A();
return B();
}
finally
{
C();
}
try
{
A();
return B();
}
finally
{
C();
}
No idea whether try and finally count
Heiholf
HeiholfOP17mo ago
It would probably count, maybe even as 2 or 3 tokens but I can say for certain because I dont know of a way to accomplish the functionality Tha's a very clever idea, according to the token counter it is equivalent to the first method but there might be room for optimization
Omnissiah
Omnissiah17mo ago
i was thinking like
List<object> fs = new() { (object)A, (object)B, (object)C };
bool bool_result;
fs.ForEach(f => { if (f is Action a) a(); else bool_result = ((Func<bool>)f)(); });
List<object> fs = new() { (object)A, (object)B, (object)C };
bool bool_result;
fs.ForEach(f => { if (f is Action a) a(); else bool_result = ((Func<bool>)f)(); });
but maybe i'm uselessly complicating things i don't really know what token means maybe this
bool bool_result = false;
await Task.WhenAll(new[] {
new Func<Task>(async () => jvoid())(),
new Func<Task>(async () => { bool_result = rbool(); })(),
new Func<Task>(async () => jvoid())()
});
return bool_result;
bool bool_result = false;
await Task.WhenAll(new[] {
new Func<Task>(async () => jvoid())(),
new Func<Task>(async () => { bool_result = rbool(); })(),
new Func<Task>(async () => jvoid())()
});
return bool_result;
slightly less verbose
bool b = false;
return (bool)(Action.Combine(A, () => () => b = B(), C).DynamicInvoke() ?? b);
bool b = false;
return (bool)(Action.Combine(A, () => () => b = B(), C).DynamicInvoke() ?? b);
also i don't get what this doesn't work return (bool)Action.Combine(new[] { ((Action)A).DynamicInvoke, ((Delegate)B).DynamicInvoke, ((Action)C).DynamicInvoke }).DynamicInvoke(); but this works
return new[] { ((Action)A).DynamicInvoke, ((Delegate)B).DynamicInvoke, ((Action)C).DynamicInvoke }.Select(_ => _()).OfType<bool>().Single();
return new[] { ((Action)A).DynamicInvoke, ((Delegate)B).DynamicInvoke, ((Action)C).DynamicInvoke }.Select(_ => _()).OfType<bool>().Single();
Heiholf
HeiholfOP17mo ago
This is definitely the closest way to "convert void to bools" but it sadly is too verbose because a token is a keyword, a opening/closing brace/bracket/parenthesis, dot, comma, semicolon, type name, variable name or any "atomic" element of code (some of these are excluded by the rules of the contest).
Omnissiah
Omnissiah17mo ago
so a brief description would be "lexical entity", we shall say
canton7
canton717mo ago
The description I read on the website explicitly excluded things like semicolons
Heiholf
HeiholfOP17mo ago
That's correct in general a token is anything described above but the contest excludes: SyntaxKind.PrivateKeyword, SyntaxKind.PublicKeyword, SyntaxKind.SemicolonToken, SyntaxKind.CommaToken, SyntaxKind.CloseBraceToken, SyntaxKind.CloseBracketToken, SyntaxKind.CloseParenToken
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server