Syntactic sugar for this pattern?

Sorry for the vague title, but I don’t even know how to describe it in a few words. The following pseudo code describes the problem.

new_res = some_fn()
res = some_condition(res, new_res) ? new_res : res

In short, I need to determine if I should set a variable to the new result or not, based on some condition.

Of course, the above code pattern works, but it seems to contain unnecessary repetition. Both res and new_res appear 3 times. Is there a better way to deal with this situation, other than writing a macro?

Thanks.

1 Like

An alternative formulation:

new_res = some_fn()
if some_condition(res, new_res) res = new_res end

# or
some_condition(res, new_res) && ( res = new_res )

These may be more readable and emphasize the updating semantics for res.

1 Like

Thanks, this is better.