Ternary Like Operation Idiom in Erlang

Jarrod Roberson <>
Wed Dec 2 22:13:15 CET 2009


In multicast dns TXT records you have name value pairs that can look
like "key=value", "key=" and "key".
"key=" means key with no values
"key" implies "key=true"
I need to convert any thing that looks like "key" to "key=true" to
normalize my data I am converting to dictionaries using re:split/2.

here is how I would do this in Java.

String s = "xxx";
return s.matches("=") ? s : s + "=true";

here is what I have come up with in Erlang.

fix(T,{match,_}) -> T;
fix(T,nomatch) -> string:concat(T,"=true").

where I call it like
S = "key".
fix(S,re:run(S,"=")).

Ignoring that the function name "fix" needs to be more descriptive.
Is this the best way to do something like this? Or is there some fancy
one line way of doing this?


More information about the erlang-questions mailing list