These are chat archives for evhub/coconut

20th
Feb 2017
Mark S I Taylor
@realms-of-chaos
Feb 20 2017 11:10

Once again sorry if this is a naive question or if I've missed something in the documentation (some of which is still over my head at this point). Is it possible to do a 'conditional assignment' with Coconut? I've also been playing about with Elm and found it very useful in that language. For example I can do something like this:

case value:
    match 1:
        (a, b, c) = (1, 2, 3)
    match 2:
        (a, b, c) = (3, 2, 1)
    match _:
        (a, b, c) = (0, 0, 0)

but I'd like to reduce it to something more like this:

(a, b, c) = case value:
                match 1:
                    (1, 2, 3)
                match 2:
                    (3, 2, 1)
                match _:
                    (0, 0, 0)

...which generates a CoconutParseError. Is there a nice way do do this in Coconut without having to type (a, b, c) = in every branch of a case or if .. elif .. else statement? Not so bad when there are only three branches and three values to assign, but on a larger scale it's a different matter. Thanks.

Mark S I Taylor
@realms-of-chaos
Feb 20 2017 11:18
P.S. I've looked into Python Conditional expressions as per https://www.python.org/dev/peps/pep-0308/ but unless I'm missing something they only seem to allow for one 'True' and one 'False' branch
Mark S I Taylor
@realms-of-chaos
Feb 20 2017 11:30
Also, to clarify, I'm trying to do it without having to define a separate function, which I understand can be done quite easily
Mark S I Taylor
@realms-of-chaos
Feb 20 2017 12:11

I've just seen #190 and the conversation about it earlier here. So I guess that answers my question for the time being. I really like the suggestion that case statements (maybe even if statements too) could be expressions. Maybe this could be distinguished syntactically from the normal uses of such statements by enclosing the conditional block inside some sort of opening and closing delimiters, such as parentheses or some variation i.e. (if ...) or (case ...) or something like that. So my previous example might look like:

(a, b, c) = (
    case value:
        match 1:
            (1, 2, 3)
        match 2:
            (3, 2, 1)
        match _:
            (0, 0, 0)
    )

for example. (There may be reasons I don't know about why this is a terrible/impractical/impossible suggestion as I'm really not that proficient with Python/Coconut yet).

Mark S I Taylor
@realms-of-chaos
Feb 20 2017 12:32
...And I have close to zero idea of how the compiler works in practice so feel free to let me know if I'm being an idiot!