❔ 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:
And I want to execute them in order and keep the return value of B()
.
One way to accomplish is like that:
But it seems to be not the optimal way to minimize tokens. I would except to be a workaround like this:
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
Can you change the methods a and c?
No, they are given methods
Then I think the only option is to wrap it in a normal method or a lambda.
I think you are correct. But sadly a function or a lambda create more tokens as the first option.
I actually also participate in this challenge so I‘m facing the same problems.
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?I haven't counted directly but the second version would lead to a syntax change somewhere else which would overall reduce tokens
I guess you might be able to do something like:
No idea whether
try
and finally
countIt 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
i was thinking like
but maybe i'm uselessly complicating things
i don't really know what token means
maybe this
slightly less verbose
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
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).
so a brief description would be "lexical entity", we shall say
The description I read on the website explicitly excluded things like semicolons
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
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.