Friday, January 15, 2021

Can we have anti-lambdas please?

Aren’t you tired of flattening your code to avoid repeated computation:

val x = longComputation()
ls map { y =>
  (x, y)
}

when you’d rather write

ls map { y =>
  (
    longComputation(),
    y
  )
}

because it’s more compact, but if you had an anti-lambda you could write

ls map { y =>
  (
    <= y ( longComputation() ),
    y
  )
}

where the anti-lambda <= y ( ... ) cancels out the parameter y making longComputation() constant and evaluated only once (essentially making it part of the closure’s local environment).