Suggestion: if-comprehensions

Foo for thought: in the language we’re building, one is going to be are able to write:

if aOpt is Some(a) and bOpt is Some(b) and a.foo() == b.bar() then 
   // Use a & b...
else ...

Some other cool examples:

fun add(x, y) =
  if x is Some(xv) and y is Some(yv)
  then Some(xv + yv)
  else None

fun nonZero(list) =
  list is Nil or
    list is x :: xs and x != 0 and nonZero(xs)

fun findFirst(list, p) =
  if list is
    Nil then None
    x :: xs and
      p(x) then Some(x)
      else findFirst(xs, p)

fun zipWith(f, xs, ys) =
  if xs is Cons(x, xs)
    and ys is Cons(y, ys)
    and zipWith(f, xs, ys) is Some(tail)
    then Some(Cons(f(x, y), tail))
  else if xs is Nil
    and ys is Nil
    then Some(Nil)
  else None

fun mapPartition(f, xs) = if xs is
  Nil then (Nil, Nil)
  x :: xs and mapPartition(f, xs) is (l, r) and f(x) is
    Left(v) then (v :: l, r)
    Right(v) then (l, v :: r)

if x <=
  31 then "invisible"
  57 and x >= 48 then "digit"
  90 and x >= 65 then "uppercase"
  122 and x >= 97 then "lowercase"
else "symbol"
4 Likes