[erlang-questions] clarify: how to express this elegantly

Richard Carlsson richardc@REDACTED
Tue Dec 4 22:17:57 CET 2007


There are (at least) two variants.

Version 1:

    case is_plottable(Value) of
        true ->
            ?D, circuit_monitor:plot_node_value(..., Value);
        false ->
            throw(unplottable_value)
    end
    ...

is_plottable("state") -> true;
is_plottable("rx_bit_rate") -> true;
...
is_plottable("rtt") -> true;
is_plottable(_) -> false.


Version 2:

    Plot = fun () ->
            ?D, circuit_monitor:plot_node_value(..., Value)
           end,
    case Value of
        "state" -> Plot();
        "rx_bit_rate" -> Plot();
        ...
        "rtt" -> Plot()
    end

Also, it's probably better if you can transform the strings to atoms
when you read them, because a switch over atoms is much more efficient
than a switch over strings.

  /Richard


Matej Kosik wrote:
> Friends,
> 
> I wander, how can I express this:
> 
>     case Value of
>         "state" ->
>             ?D,
>             circuit_monitor:plot_node_value(CircuitMonitorPid, Socket,
>                 RequestId, CircuitId, FromDateTime, ToDateTime, Width, Height, Value
>             );
>         "rx_bit_rate" ->
>             ?D,
>             circuit_monitor:plot_node_value(CircuitMonitorPid, Socket,
>                 RequestId, CircuitId, FromDateTime, ToDateTime, Width, Height, Value
>             );
>         "tx_bit_rate" ->
>             ?D,
>             circuit_monitor:plot_node_value(CircuitMonitorPid, Socket,
>                 RequestId, CircuitId, FromDateTime, ToDateTime, Width, Height, Value
>             );
>         "ebno" ->
>             ?D,
>             circuit_monitor:plot_node_value(CircuitMonitorPid, Socket,
>                 RequestId, CircuitId, FromDateTime, ToDateTime, Width, Height, Value
>             );
>         "packetloss" ->
>             ?D,
>             node_pinger:plot_node_value(NodePingerPid, Socket,
>                 RequestId, CircuitId, FromDateTime, ToDateTime, Width, Height, Value
>             );
>         "rtt" ->
>             ?D,
>             node_pinger:plot_node_value(NodePingerPid, Socket,
>                 RequestId, CircuitId, FromDateTime, ToDateTime, Width, Height, Value
>             )
>     end
> 
> more elegantly. Via some kind of "variant patterns" such as:
> 
>     case Value of
>         "state" | "rx_bit_rate" | "tx_bit_rate" | "ebno" ->
>             ?D,
>             circuit_monitor:plot_node_value(CircuitMonitorPid, Socket,
>                 RequestId, CircuitId, FromDateTime, ToDateTime, Width, Height, Value
>             );
>         "packet_loss" | "round_trip_time" ->
>             ?D,
>             node_pinger:plot_node_value(NodePingerPid, Socket,
>                 RequestId, CircuitId, FromDateTime, ToDateTime, Width, Height, Value
>             )
>     end
> 
> but these do not exist :(
> These "variant patterns" could be introduced without any problems in special cases when neither
> variant contains unbound variable.
_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list