Live data from Hacker News

The Ultimate Conditional Syntax

dl.acm.org

21–30 of 49 posts

Re: The Ultimate Conditional Syntax

#21
post #2

Elixir already has this - "with". https://www.openmymind.net/Elixirs-With-Statement/ E.g.: with true

I don't think that this is `with` (which is really a series of nested `case` statements with exactly two paths per case). case is_email_address?(email) do true -> case String.length(code) == 6 do true -> case EmailConfirmations.get_email_confirmation(email) do %EmailConfirmation{} -> case EmailAddresses.get_email_address(email) do nil -> case Users.create_user(data) do {:ok, user} -> case EmailAddresses.create_email_…

Just check out the paper's Motivaton section (2).

In ML you can't write something like this:

    if e is
      ...
      Lit(value)
        and Map.find_opt(value) is Some(result)
        then Some(result)
      ...
where the `...` may include many cases and may contain other Lit cases, so that you would need to refactor the whole expression.

Haskell's pattern guards can do this, but they can't "split" control-flow in the middle of a case, as in:

      Lit(value)
        and Map.find_opt(value) is Some(result)
        and computation(result) is
          Left(a)  then ...
          Right(b) then ...
but these all fall out completely naturally in the UCS.

Also, exhaustiveness Just Works without the need of any type annotation. The system is actually type system agnostic.

Re: The Ultimate Conditional Syntax

#22
post #4

I don't understand how it's better than traditional pattern matching.

With traditional matching there are up to five different things: - if x then y else z. This is roughly like a match with a true and false case but it depends on the language how much that is - match e with { p -> e }. This is the classic pattern match case - if let p = e then x. This is roughly equivalent to (match e with p -> x | _ -> ()) - match e with { p when e -> e }. This is matching with a guard but I’ve count…

Also check out my other answer here: https://news.ycombinator.com/item?id=41901573

Re: The Ultimate Conditional Syntax

#23
post #21

Earlier quoted context omitted.

I don't think that this is `with` (which is really a series of nested `case` statements with exactly two paths per case). case is_email_address?(email) do true -> case String.length(code) == 6 do true -> case EmailConfirmations.get_email_confirmation(email) do %EmailConfirmation{} -> case EmailAddresses.get_email_address(email) do nil -> case Users.create_user(data) do {:ok, user} -> case EmailAddresses.create_email_…

Just check out the paper's Motivaton section (2). In ML you can't write something like this: if e is ... Lit(value) and Map.find_opt(value) is Some(result) then Some(result) ... where the `...` may include many cases and may contain other Lit cases, so that you would need to refactor the whole expression. Haskell's pattern guards can do this, but they can't "split" control-flow in the middle of a case, as in: Lit(val…

PS: there's another point being made on Reddit about cond's right-shift problem: https://www.reddit.com/r/ProgrammingLanguages/comments/1g127...

Re: The Ultimate Conditional Syntax

#24
Kudos for publishing in OOPSLA, but alas I'm skeptical. This seems to add visual clutter/mental overhead as one needs to parse a decision tree rather than having a "flat" sequence of patterns with the optional when-condition.

Also,

> all the branches in the corresponding pattern matching expression would need to destructure the pair, even when only one of its components is needed

Can't one just bind the unneeded component to _ ? Isn't that actually a good thing, as it clearly self-documents we won't need that value on that branch?

Re: The Ultimate Conditional Syntax

#27
This is really cool

It reminds me a lot of the kind of flow you get in mathematics sometimes. I guess theorem prover languages may be keen to adopt it.

I'd be less keen in a language with side effects, unless the language has a way to restrict it to pure functions

Re: The Ultimate Conditional Syntax

#28
post #2

Elixir already has this - "with". https://www.openmymind.net/Elixirs-With-Statement/ E.g.: with true

I don't think that this is `with` (which is really a series of nested `case` statements with exactly two paths per case). case is_email_address?(email) do true -> case String.length(code) == 6 do true -> case EmailConfirmations.get_email_confirmation(email) do %EmailConfirmation{} -> case EmailAddresses.get_email_address(email) do nil -> case Users.create_user(data) do {:ok, user} -> case EmailAddresses.create_email_…

My eyes! They burn!

Re: The Ultimate Conditional Syntax

#29
> Below, the code on the left is equivalent to the more verbose ML expression on the right:

Wait a sec ... ML has the pipe of Elixir? Damn, I didn't know that!

I like the brevity, but I don't like the (at least visual) need to indent or whitespace focus. I guess I am too much a sucker for S-expressions now. But a similar structure could probably be written as a macro in a lisp, avoiding any whitespace focus or even dependency on it.

Re: The Ultimate Conditional Syntax

#30

This is the ultimate conditional syntax: if (x === 1) console.log(“it’s 1”) The “if” bothers me though, be nice not to have it.

    (x === 1) && console.log("it's 1")
Note that you've saved exactly no keystrokes, although this is composable into expressions (if you are using some Javascript-derivative; in ML it would typically already be using the if-form).
Post reply on HN