3 ms·
There’s no mutating happening here, for example: if cond: X = “yes” else: X = “no” X is only ever assigned once, it’s actually still purely fu
by turtletontine 1y ago
There’s no mutating happening here, for example:
if cond:
X = “yes”
else:
X = “no”
X is only ever assigned once, it’s actually still purely functional. And in Rust or Lisp or other expression languages, you can do stuff like this:
let X = if cond { “yes” } else { “no” };
That’s a lot nicer than a trinary operator!
- nielsbot 1y agoSwift does let you declare an immutable variable without assigning a value to it immediately. As long as you assign a value to that variable once and only once on every code path before the variable is read: let x: Int if cond { x = 1 } else { x = 2 } // read x here
- lock1 1y agoIMO, both ternary operator form & Rust/Haskell/Zig syntax works pretty well. Both if expression syntax can be easily composed and read left-to-right, unlike Python's `<true-branch> if <cond> else <false-branch>`.