Deduct "New List" with "new Value" only for some "specific Tag", from Old List, using "List Comprehension".

Oliver Korpilla Oliver.Korpilla@REDACTED
Sun Jul 19 10:19:01 CEST 2020


Hello,

wouldn't it be better to either add more function heads to the required function to process them or a case switch?

inner_response({charging_characteristics, V}) -> ... do something specific... ;
inner_response({qos, V}) -> ... do something for QOS ... ;
inner_response({_, V}) -> ... handle unexpected tag ... .

response(<<MsgId, Request/binary>>) ->
... call inner_response/1 with {Tag,Value} pair ... .

If you do it this way, you can condense all your information element transformations into one inner_response function with many heads.

Or do a case expression to the same effect:

...
case {T,V} of
  {charging_characteristics, _} -> ... do something with V ... ;
  ...
  _                             -> ... catch all ... 
end,
...

This will condense your code but has one implication: 

You do not guarantee that the right information elements are present for each message.

However, that can be put into a separate function if desired.

Oliver

 

Gesendet: Sonntag, 19. Juli 2020 um 09:59 Uhr
Von: "Papa Tana" <papa.tana101@REDACTED>
An: "Oliver Korpilla" <Oliver.Korpilla@REDACTED>
Cc: "erlang-questions" <erlang-questions@REDACTED>
Betreff: Re: Re: Deduct "New List" with "new Value" only for some "specific Tag", from Old List, using "List Comprehension".
Hi,

Thanks for your answer.

> Sorry, I didn't see you required list comprehension. Don't know why you have
> this requirement, though - many other ways to do it.

This T list is just a dummy example.

I have this requirement because in real life, I have 125 types of list
"REQ" compound of {Tag, Value}.
I need to process some tags inside them, transform, and answer back
with a response "RESP".

create_response(<<1, REQ/binary>>) ->
Length = [ integer_to_list(V,16) || {T,V} <- REQ , T == length ],
Charging_Characteristics = [ V || {T,V} <- REQ , T ==
charging_characteristics ],
Protocols_Configurations_Payload = [ V || {T,V} <- REQ , T ==
protocols_configurations ],
QoS_Payload = [ V || {T,V} <- REQ , T == qos ],....

.......
create_response(<<125, REQ/binary>>) ->
SelectionMode = [ V || {T,V} <- REQ , T == selection_mode ],
TearDownIndication = [ V || {T,V} <- REQ , T == teardown_indication ],
User_Location_Information_Payload = [ V || {T,V} <- REQ , T ==
user_location_information ],..........

So, I have to change;
- new Length depending on Old Length input,
- new Charging_Characteristics depending on Input of Charging_Characteristics,
- new Protocols_Configurations_Payload depending on Old
Protocols_Configurations_Payload Input,
- QoS_Payload depending on QoS_Payload input,
- etc...

After trying to create a lot of anonymous function to process each
desired Tag inside 125 different type of request, I was thinking that
List comprehension using some filter would be succinct and easy to
maintain.

Best Regards,


More information about the erlang-questions mailing list