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

That's a neat pattern in F#, I like it!

In Erlang's case (pun intended!) I'd probably go for a function though:

    total({expense, N}, Total) -> Total - N;
    total({invoice, N}, Total) -> Total - N;
    total({loss, N},    Total) -> Total - N;
    total({credit, N},  Total) -> Total + N.



That still repeats the `Total - N` 3 times, though. As patrec said, it's possible to work around this with guards in this specific case, but in general this kind of duplication is indeed common in Erlang code. You can avoid it many cases by correctly factoring the code, but it's not always possible and then it's a bit irritating. Not too much, though. It's basically the difference between fall-through and non-fall-through `switch` statement: OCaml/F# allow fall-through (as an option, not by default, thankfully), Erlang/Elixir/LFE don't.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: