7 ms·
> So, really, it seems to me what we really want is a better way to express branching operations. There is a fairly wide-adopted and battle-proven pattern. It'
by valenterry 2y ago
> So, really, it seems to me what we really want is a better way to express branching operations.
There is a fairly wide-adopted and battle-proven pattern. It's called monads. Basically, you put a "context" over parts of your code and then some type of branching becomes implicit.
Good languages generalize it and allow their developers to create and use their own contexts. Other languages at least have special cases for very common cases. async/await is such an example.
- 9rx 2y ago> There is a fairly wide-adopted and battle-proven pattern If by fairly wide-adopted you mean it is adopted in a handful of language nobody uses, sure. > Other languages at least have special cases for very common cases. Where they usually screw it up. Look at the canonical example of a "monad" in Rust: let result = divide(3.0, 2.0); match result { Just(x) => println!("Answer: ", x), Nothing => println!("division failed; we'll get 'em next time."), } That is just "if" by another name!
- steveklabnik 2y agoWhere did you get this Rust code from? I’m curious.
- 9rx 2y agoI found the same code in several different places. Wikipedia was the place I ultimately copy/pasted it from.
- steveklabnik 2y agoHuh, I don't see this code on Wikipedia. I ask because it's very much not "canonical," Just and Nothing are Haskell terms, not Rust terms. You'd expect at least Some and None. Regardless, I do agree that in this specific circumstance, you're emulating an if. There's even a form in this case that emulates it even more closely: if Some(x) = divide(3.0, 2.0) { println!("Answer: ", x); } else { println!("division failed; we'll get 'em next time."); } But these simple cases don't show off where Option/Result truly shine.
- 9rx 2y agohttps://en.wikipedia.org/wiki/Monad_(functional_programming) https://en.wikipedia.org/wiki/Monad_(functional_programming)
- steveklabnik 2y agoAh, thanks, I was looking at the Rust page. I'll consider sending them an edit. Sent one in: https://en.wikipedia.org/w/index.php?title=Monad_(functional_programming)&oldid=1282620785 https://en.wikipedia.org/w/index.php?title=Monad_(functional... we'll see if it gets reverted. Honestly, I find this whole section kind of awkward. I fully agree with you that it doesn't really show off why this stuff works, and works well. Real-world code doesn't get written like this.
- School-Cotton 2y agoyou're missing a `let`
- steveklabnik 2y agodangit, thanks. can't edit now though...
- deleted 2y ago[deleted]
- valenterry 2y ago> If by fairly wide-adopted you mean it is adopted in a handful of language nobody uses, sure. Please precisely define your criteria so that I can end this discussion by giving you a concrete counter example. > That is just "if" by another name! It appears you haven't understand the difference. Here's a counter example: let first_result = divide(10, ???); let second_result = divide(20, ???); let final_result = first_result.or(second_result).unwrap_or(42); println!("Result: {}", final_result); Logic being, do two divisions and print the result of the first one, or if it failed, of the second one, or fall back to 42. We can go one with using lists of results, results of results and doing conditional calculations and so on and so on - and all of that without writing a single if. Now do that in go without using if. Won't look as nice for sure.
- 9rx 2y ago> Please precisely define your criteria You introduced the term. It is on you to define it. My stab in the dark to try and get us to an understanding may have failed, but that's where you would logically come in and explain yourself in better detail. > so that I can end this discussion by giving you a concrete counter example. A single counter example? I definitely would have never guessed that by wide-adoption you were thinking something singular. No wonder you've been so afraid to share your definition. > Logic being, do two divisions and print the result of the first one, or if it failed, of the second one, or fall back to 42. That too is just "if" with different syntax, and worse syntax – treading into Perl one-liner territory, but I concede that this particular example may not be a good basis on which to start from.
- valenterry 2y agoOkay then, how about "at least 3 languages with each at least a few thousand devs using them supports it" And my example is not "if". There is no if. Maybe there is "if" being used in the underlying functions? Again, eventually everything is machine code, but then every discussion about PLs is meaningless...
- 9rx 2y ago