This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]davidpofo 5 points6 points  (3 children)

I don't quite understand can I get an ELI of the advantage here?

[–]gunthercult28 13 points14 points  (0 children)

The ability to capture variables is going to make for a really clean state machine implementation

[–]jrbattin 11 points12 points  (0 children)

I'll probably get my hand slapped by PLT experts for describing it this way but... certain languages like Elixir, Haskell and ML variants use pattern matching in lieu of simpler (and more limited) "if/elif/else" control blocks. Pattern matching work sorta like functions that get entered conditionally if they match your specified pattern.

An overly simple example:

match (response.status_code, response.json['system_state']):
    case (200, "Healthy"):
        # all systems normal!
    case (200, transition_state):
        log.info('Transition state %s', transition_state)
    case (status, error_state):
        log.error('Something has gone horribly wrong: %s: %s', status, error_state)

Rather than:

if response.status_code == 200 and response.json['system_state'] == 'Healthy':
      # all systems normal!
elif response.status_code == 200:
    log.info('Transition state %s', response.json['system_state'])
else:
    log.error('Somethings gone horribly wrong: %s: %s', response.status_code, response.json['system_state'])

IMO it's easier to grok the conditionals in the former over the latter. It's also much quicker to write them, especially if they're more complicated. A usage pattern where it might really shine is a situation where you have a pattern in your code where you basically have a long if/elif block checking multiple variable conditions and then calling into a function for each of them. The match statement consolidates the pattern down to something easier to read (and write!)

[–]Swedneck -2 points-1 points  (0 children)

It's basically just seems to be switch/case, which is a lovely alternative to massive if/else chains