Hacker News new | past | comments | ask | show | jobs | submit login

You can create true "early return" behavior by writing a lazy computation builder [0]. It feels esoteric but taking the time to craft a domain specific computation builder can drastically simplify the rest of your code base. I don't know if there's a more modern (more ergonomic) way to do this since this has been around since ~2012 or so.

Alternatively you can take the more functional approach and rewrite your function logic to use `if x then (value) else` throughout instead of `return (value)` or `yield (value)`. Early return isn't really a thing in f# but if-else is.

[0] https://fsharpforfunandprofit.com/posts/computation-expressi...




most of the time early return can be replaced by function calls. what bites is that f# has no way to force TCO so sometimes it's safer to write a loop with a mutable flag.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: