Cipherd@lemmy.ml to Programmer Humor@programming.dev · 13 hours agofunctionslemmy.mlimagemessage-square44fedilinkarrow-up1360file-textcross-posted to: programmerhumor@lemmy.ml
arrow-up1360imagefunctionslemmy.mlCipherd@lemmy.ml to Programmer Humor@programming.dev · 13 hours agomessage-square44fedilinkfile-textcross-posted to: programmerhumor@lemmy.ml
minus-squarecalcopiritus@lemmy.worldlinkfedilinkarrow-up6·10 hours agoThis can also be a side product for code blocks being expressions instead of statements. In rust for example they are, so it’s not rare to see functions like: fn add_one(x: i32) -> i32 { x+1 } This lets you do amazing things like: let x = if y < 0.0 { 0.0 } else { y } which is the same as x = y < 0.0 ? 0.0 : y But is much better for more complex logic. So you can forget about chaining 3-4 ternary operations in a single line.
minus-squareSlurpingPus@lemmy.worldlinkfedilinkarrow-up2·1 hour agoLisp programmers seeing these ‘amazing things’: But yeah, every time I’m trying to do a ternary in Lua, I miss being able to just throw in an if.
This can also be a side product for code blocks being expressions instead of statements.
In rust for example they are, so it’s not rare to see functions like:
fn add_one(x: i32) -> i32 { x+1 }This lets you do amazing things like:
let x = if y < 0.0 { 0.0 } else { y }which is the same as
x = y < 0.0 ? 0.0 : yBut is much better for more complex logic. So you can forget about chaining 3-4 ternary operations in a single line.
Lisp programmers seeing these ‘amazing things’:
But yeah, every time I’m trying to do a ternary in Lua, I miss being able to just throw in an
if.