cm0002@lemmy.world to Programmer Humor@programming.dev · 2 days agoInfallible Codefeddit.ukimagemessage-square54fedilinkarrow-up1275arrow-down118
arrow-up1257arrow-down1imageInfallible Codefeddit.ukcm0002@lemmy.world to Programmer Humor@programming.dev · 2 days agomessage-square54fedilink
minus-square_cnt0@sh.itjust.workslinkfedilinkarrow-up2·2 days agoThough, obviously I had to come up with some ridiculous solutions: bool IsEven(int i) => ((Func<string, bool>)(s => s[^1] == 48))($"{i:B}"); This one works without conditionals :) bool IsEven(int i) { try { int _ = (i & 1) / (i & 1); } catch (Exception) { return true; } return false; }
minus-squareLemminary@lemmy.worldlinkfedilinkarrow-up1·24 hours ago s[^1] Ohh wow, I’ve been learning it casually for years, and I didn’t know that existed in C#. I guess I should go back and hit the books some more.
Though, obviously I had to come up with some ridiculous solutions:
bool IsEven(int i) => ((Func<string, bool>)(s => s[^1] == 48))($"{i:B}");
This one works without conditionals :)
bool IsEven(int i) { try { int _ = (i & 1) / (i & 1); } catch (Exception) { return true; } return false; }
Ohh wow, I’ve been learning it casually for years, and I didn’t know that existed in C#. I guess I should go back and hit the books some more.