Python Forum
Are braces rather than indentation ever wanted?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are braces rather than indentation ever wanted?
#21
(May-01-2017, 03:38 PM)Ofnuts Wrote: don't use switch/case statements
That'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
Reply


Messages In This Thread
RE: Are braces rather than indentation ever wanted? - by nilamo - May-02-2017, 05:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output set items without the curly braces ? jimthecanadian 3 2,998 May-11-2019, 07:02 AM
Last Post: perfringo
  search and replace dots inside curly braces kchinnam 1 2,728 May-01-2018, 06:53 PM
Last Post: Larz60+
  No curly braces in python? RedSkeleton007 9 9,351 Jul-28-2017, 11:01 AM
Last Post: tony1812
  JSON API / no braces marucho21 1 3,963 Feb-01-2017, 07:35 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020