C
C#12mo 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
chef drone builder
Can you change the methods a and c?
Heiholf
Heiholf12mo ago
No, they are given methods
chef drone builder
Then I think the only option is to wrap it in a normal method or a lambda.
Heiholf
Heiholf12mo ago
I think you are correct. But sadly a function or a lambda create more tokens as the first option.
chef drone builder
I actually also participate in this challenge so I‘m facing the same problems.
canton7
canton712mo 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
Heiholf12mo ago
I haven't counted directly but the second version would lead to a syntax change somewhere else which would overall reduce tokens
canton7
canton712mo 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
Heiholf12mo 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
ffmpeg -i me -f null -
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
Heiholf12mo 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).
ffmpeg -i me -f null -
so a brief description would be "lexical entity", we shall say
canton7
canton712mo ago
The description I read on the website explicitly excluded things like semicolons
Heiholf
Heiholf12mo 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
Accord12mo 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.