Bend icon indicating copy to clipboard operation
Bend copied to clipboard

(Request) If expressions

Open developedby opened this issue 1 year ago • 1 comments

At the moment, all pattern matching syntax happens only at the statement level. This makes writing some stuff a pain.

For example, if we want to write a max function with a lambda, that's not possible.

I propose the addition of python-like if expressions: lambda a, b: a if a > b else b

This is ok, but opens the can of worms of what statements should have expression versions, which i'd like to be 0.

Alternatives:

  • Current way (users have to define a separate function)
  • Scoped functions. We can restrict them to always be combinators and implicitly pass captured variables, which would avoid duplication problems (we'd catch it during runtime).
def FnOuter(a):
  def FnInner(b):
     return f(a, b)
  return FnInner(2)

developedby avatar May 20 '24 23:05 developedby

I'd like to propose two seemingly unconsidered alternatives, each of which would fix the expression-statement issue:

  • A C-like ternary operator Imperative syntax: condition ? is_true : is_false Functional syntax: (? condition is_true is_false)
  • An ifelse function. Imperative syntax: ifelse(condition, is_true, is_false) Functional syntax: (ifelse condition is_true is_false)

The C-like option would be less Python-like, but would still be familiar to most developers and would be more concise. On the other hand, the ifelse option would be more Python-like, but would run the risk of potentially breaking programs that have an ifelse in their namespace.

qwergle avatar Jun 25 '24 18:06 qwergle