I need to print out a value if it is above zero:
info({mail, BoxPid, Messages, MessageCount, MessageDropCount}, Req, S) ->
if MessageDropCount > 0 ->
io:format("dropped=~p~n", [MessageDropCount]);
true -> true
end,
...,
{loop, Req, S};
I do not like the true -> true
part of the if
expression. Is there a way to avoid it? Is there an idiom for that?
The If Then page at the ErlangCentral wiki suggests these patterns:
[io:format("dropped=~p~n", [MessageDropCount]) || MessageDropCount > 0]
MessageDropCount > 0 andalso io:format("dropped=~p~n", [MessageDropCount])
MessageDropCount =< 0 orelse io:format("dropped=~p~n", [MessageDropCount])
You ca use a function with pattern matching:
EDIT: a more general function could be the following: