4 ms·
I've never found it much more painful to write: for i, c in range col { if c.isReady && convertToX(c).color == "BLUE": return True } (which is n
by solidasparagus 6y ago
I've never found it much more painful to write:
for i, c in range col {
if c.isReady && convertToX(c).color == "BLUE":
return True
}
(which is not to say I don't miss the map/filter syntax - although I don't miss the bugs related to lazy execution in stuff like LINQ or Spark)
- erik_seaberg 6y agoThat’s actually pretty good. Most people would use three more lines to create a temporary and break after setting it, and three to nine more depending on how many steps require idiomatic Go error handling.
- andykx 6y agoWould most people actually do that? That example seems like pretty idiomatic Go. I imagine junior developers would write that.
- erik_seaberg 6y agoI would typically expect anyBlue := false for _, c in range col { ready, err := c.IsReady() if err != nil { return nil, fmt.Errorf("isReady failed: %v", err) } if !ready { continue } x, err := ConvertToX(c) if err != nil { return nil, fmt.Errorf("convertToX failed: %v", err) } color, err := x.GetColor() if err != nil { return nil, fmt.Errorf("getColor failed: %v", err) } if color == BLUE { anyBlue = true break } } which badly obscures what’s going on but demands no cleverness at all (except that some might forget the “break” and still get the right answer).
- tmerr 6y agoIf IsReady, ConvertToX, and GetColor can return errors then this isn't that far off. But then you're not making an apples to apples comparison. You would have to extend your functional example so that it also handles errors. Unless it already does, and I am missing something.
- erik_seaberg 6y agoWhat I wrote should be pretty close to valid Scala, which handles exceptions. I can imagine wanting to skip an isReady failure, which looks like col.filter(c => Try(c.isReady).getOrElse(false)) .map(convertToX).any(_.color == BLUE)
- tmerr 6y agoI see. Then in part the comparison is showing off exception handling, not FP. Rust is a good example of functional lang without exceptions (panics aside). In Rust there are many cases where I could use FP but opt not to because it can make error handling more awkward. For example to collect the result of a map operation to a vector, and if there is an error along the way you want to abort the entire operation. In functional this means collecting to a Result<Vec<T>>. Not sure if I'm just tired today but I can think of how to do it in 5 seconds with a loop. I suspect functional solution would be uglier.
- steveklabnik 6y agoRust’s collect will do this for you. It’s even an example in the documentation.
- tmerr 6y agoThanks! I learned something new.:) https://doc.rust-lang.org/stable/rust-by-example/error/iter_result.html#fail-the-entire-operation-with-collect https://doc.rust-lang.org/stable/rust-by-example/error/iter_... This is easy to miss if you are mainly reading reference docs. I probably read the getting started guide at a time before there was a section on it. My other comment in this thread comes to mind https://news.ycombinator.com/item?id=24300057 https://news.ycombinator.com/item?id=24300057