May-02-2017, 05:01 AM
(May-01-2017, 03:38 PM)Ofnuts Wrote: [ -> ]don't use switch/case statementsThat's funny, I just recently watched a video about HolyC's switch statement. https://www.youtube.com/watch?v=tBQ69ZnuMbc (nsfw... just like everything related to TempleOS)
The short version is that he added start/end clauses to switch statements, to add additional pattern matching for groups of cases. Additionally, it's worth noting that in HolyC, if you don't set a variable, it's sent to stdout, so each branch of the switch is echoing output (that might just be for strings, or single quoted strings, I'm not sure).
I64 i; for (i=0;i<10;i++) { switch (i) { case 0: 'Zero\n'; break; case 1: 'One\n'; break; case 2: 'Two\n'; break; start: '['; case 3: 'Three'; break; case 4: 'Four'; break; case 5: 'Five'; break; case 6: 'Six'; break; end: ']\n'; break; case 7: 'Seven\n'; break; case 8: 'Eight\n'; break; case 9: 'Nine\n'; break; } }Which is roughly the same as:
def wrapped(num): return '[{0}]'.format(num) switch = { 0: 'Zero', 1: 'One', 2: 'Two', 3: lambda: wrapped('Three'), 4: lambda: wrapped('Four'), 5: lambda: wrapped('Five'), 6: lambda: wrapped('Six'), 7: 'Seven', 8: 'Eight', 9: 'Nine' } for i in range(10): val = switch[i] # the same as a `if` in this example while callable(val): val = val() print(val)
Output:Zero
One
Two
[Three]
[Four]
[Five]
[Six]
Seven
Eight
Nine