[erlang-questions] Ternary Like Operation Idiom in Erlang

Richard O'Keefe ok@REDACTED
Thu Dec 3 01:16:32 CET 2009


On Dec 3, 2009, at 10:13 AM, Jarrod Roberson wrote:

> 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";

Why not
	s.indexOf('=') < 0 ? s : s + "=true"
?
>
>
> here is what I have come up with in Erlang.

    case lists:member($=, String)
      of true  -> String
       ; false -> String ++ "=true"
    end

Logically the same as the (improved) Java version.




More information about the erlang-questions mailing list