Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative construct for pattern clause: CASE #2

Closed
Jiansen opened this issue Jul 6, 2016 · 0 comments
Closed

Alternative construct for pattern clause: CASE #2

Jiansen opened this issue Jul 6, 2016 · 0 comments
Labels

Comments

@Jiansen
Copy link
Owner

Jiansen commented Jul 6, 2016

A pattern clause is a tuple (pattern, statements, ...extra_args)

Because tuple is not a build-in data type in JavaScript, we use array instead. For example, we may try to match a value against 3 patterns in the following order.

    const m = match(i)(
      [1, (x,y,z)=>{ return "Match "+x+" "+y+" "+z; }, 2,3]
      [2, "TWO"],
      [_, ()=>"_ matches "+i]
    )

Both an array and a tuple groups an ordered sequence of data. In a typed language, array usually refers to a group of data having the same type, whereas tuple doesn't have such a restriction. In JavaScript an array can have data of mixed types; therefore there is no need for the tuple type.

For those who are always thinking about types and not comfortable to see an array of mixed typed data, the following constructs might be more pleasant to your eyes.

    const m = match(i)(
      CASE(1, (x,y,z)=>{ return "Match "+x+" "+y+" "+z; }, 2,3),
      CASE(2, "TWO"),
      CASE(_, ()=>"_ matches "+i)
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant