On Thursday, 23 July 2015 at 21:27:17 UTC, Ziad Hatahet wrote:
I think it is actually kinda pretty:
What about:
int median(int a, int b, int c) {
return (a<b) ? (b<c) ? b : (a<c) ? c : a : (a<c) ? a :
(b<c) ? c : b;
}
vs.
def median(a: Int, b: Int, c: Int) =
if (a < b) {
if (b < c) b
else if (a < c) c
else a
}
else if (a < c) a
else if (b < c) c
else b
Not really a spaces-to-spaces comparison...
to be honest, I'd probably just write that as:
int median(int a, int b, int c) {
if (a < b) {
if (b < c) return b;
else if (a < c) return c;
else return a;
}
else if (a < c) return a;
else if (b < c) return c;
else return b;
}
You don't need it to be an expression since it is a function, you
can simply write return statements (which I kinda like since then
it is obvious that that value is a terminating condition and not
just the middle of some other calculation).
But if we were using a ternary, some newlines can help with it:
return
(a < b) ? (
(b < c) ? b
: (a < c) ? c
: a
)
: (a < c) ? a
: (b < c) ? c
: b;
Indeed, there I just took your if/else thing and swapped out the
else keyword for the : symbol, then replaced if(cond) with (cond)
?, then changed out the {} for (). It still works the same way.
Is the compiler always able to always optimize out the function
call by inlining it, as would be the case with
a scope?
It should be.