Updating elements of a record

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Updating elements of a record

Donald Steven
I would like to pass a list of some fields of a record to a function to
update those fields, leaving the other fields as they were.  The list
will be different for each function call.  For example, one call might
want to change fields 1, 2 and 7, another call only field 8, another
call fields 3 and 4.  Of course, each field will have a unique name. 
Each call to updateStatus (let's call it) would look like 
updateStatus(Status, List_of_Fields_to_Update) -> code.

--------------------------------

I can't figure put how to do this economically, without a long
succession of tests like:

extract fieldnames from list...

case field1name of
         true -> update field1;
         false -> ok % do nothing
end,

repeat for each field...


--------------------------------

Any suggestions?

Many thanks.

Don





_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Updating elements of a record

Nathaniel Waisbrot
On Tue, Jan 22, 2019, at 3:10 PM, Donald Steven wrote:
I would like to pass a list of some fields of a record to a function to 
update those fields, leaving the other fields as they were.  The list 
will be different for each function call.  For example, one call might 
want to change fields 1, 2 and 7, another call only field 8, another 
call fields 3 and 4.  Of course, each field will have a unique name.  
Each call to updateStatus (let's call it) would look like  
updateStatus(Status, List_of_Fields_to_Update) -> code.

--------------------------------

I can't figure put how to do this economically, without a long 
succession of tests like:

extract fieldnames from list...

case field1name of
        true -> update field1;
        false -> ok % do nothing
end,

repeat for each field...


--------------------------------

Any suggestions?


Do you need it to be a record? This sounds like a use-case for maps:

update_fields(Data, []) ->
  Data;
update_fields(Data, [Field|Rest]) ->
  Value = maps:get(Field, Data),
  NewValue = update_value(Value),
  NewData = maps:put(Field, NewValue, Data),
  update_fields(NewData, Rest).


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Updating elements of a record

Roger Lipscombe-2
On Tue, Jan 22, 2019, at 3:10 PM, Donald Steven wrote:
> I would like to pass a list of some fields of a record to a function to
> update those fields, leaving the other fields as they were.  The list
> will be different for each function call.  For example, one call might
> want to change fields 1, 2 and 7, another call only field 8, another
> call fields 3 and 4.  Of course, each field will have a unique name.
> Each call to updateStatus (let's call it) would look like
> updateStatus(Status, List_of_Fields_to_Update) -> code.
>

Records are just tuples. Use setelement with #rec.field:

-module(rec).
-export([new/0, update/3, test/0]).

-record(rec, {a, b, c}).

new() ->
    #rec{}.

update(R, Val, [F | Fields]) ->
    R2 = setelement(F, R, Val),
    update(R2, Val, Fields);
update(R, _Val, []) ->
    R.

test() ->
    R = rec:new(),
    R2 = rec:update(R, 2, [#rec.a, #rec.c]),
    R3 = rec:update(R2, 1, [#rec.b]),
    R3.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Updating elements of a record

Donald Steven
Thanks Roger.  I see how this works, but the issue is as much how to get
it to do:

test([{a, Val},{c, Val}]), leaving, for example, field b untouched.

On 1/22/2019 3.57 PM, Roger Lipscombe wrote:

> On Tue, Jan 22, 2019, at 3:10 PM, Donald Steven wrote:
>> I would like to pass a list of some fields of a record to a function to
>> update those fields, leaving the other fields as they were.  The list
>> will be different for each function call.  For example, one call might
>> want to change fields 1, 2 and 7, another call only field 8, another
>> call fields 3 and 4.  Of course, each field will have a unique name.
>> Each call to updateStatus (let's call it) would look like
>> updateStatus(Status, List_of_Fields_to_Update) -> code.
>>
> Records are just tuples. Use setelement with #rec.field:
>
> -module(rec).
> -export([new/0, update/3, test/0]).
>
> -record(rec, {a, b, c}).
>
> new() ->
>      #rec{}.
>
> update(R, Val, [F | Fields]) ->
>      R2 = setelement(F, R, Val),
>      update(R2, Val, Fields);
> update(R, _Val, []) ->
>      R.
>
> test() ->
>      R = rec:new(),
>      R2 = rec:update(R, 2, [#rec.a, #rec.c]),
>      R3 = rec:update(R2, 1, [#rec.b]),
>      R3.
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Updating elements of a record

Donald Steven
I should have said:

test(Rec, [{a, Val},{c, Val}])

On 1/22/2019 5.48 PM, Donald Steven wrote:

> Thanks Roger.  I see how this works, but the issue is as much how to
> get it to do:
>
> test([{a, Val},{c, Val}]), leaving, for example, field b untouched.
>
> On 1/22/2019 3.57 PM, Roger Lipscombe wrote:
>> On Tue, Jan 22, 2019, at 3:10 PM, Donald Steven wrote:
>>> I would like to pass a list of some fields of a record to a function to
>>> update those fields, leaving the other fields as they were. The list
>>> will be different for each function call.  For example, one call might
>>> want to change fields 1, 2 and 7, another call only field 8, another
>>> call fields 3 and 4.  Of course, each field will have a unique name.
>>> Each call to updateStatus (let's call it) would look like
>>> updateStatus(Status, List_of_Fields_to_Update) -> code.
>>>
>> Records are just tuples. Use setelement with #rec.field:
>>
>> -module(rec).
>> -export([new/0, update/3, test/0]).
>>
>> -record(rec, {a, b, c}).
>>
>> new() ->
>>      #rec{}.
>>
>> update(R, Val, [F | Fields]) ->
>>      R2 = setelement(F, R, Val),
>>      update(R2, Val, Fields);
>> update(R, _Val, []) ->
>>      R.
>>
>> test() ->
>>      R = rec:new(),
>>      R2 = rec:update(R, 2, [#rec.a, #rec.c]),
>>      R3 = rec:update(R2, 1, [#rec.b]),
>>      R3.
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Updating elements of a record

Raimo Niskanen-11
On Tue, Jan 22, 2019 at 06:04:13PM -0500, Donald Steven wrote:
> I should have said:
>
> test(Rec, [{a, Val},{c, Val}])
>
> On 1/22/2019 5.48 PM, Donald Steven wrote:
> > Thanks Roger.  I see how this works, but the issue is as much how to
> > get it to do:
> >
> > test([{a, Val},{c, Val}]), leaving, for example, field b untouched.

setelement/3 does exactly that; copies the other fields

> >
> > On 1/22/2019 3.57 PM, Roger Lipscombe wrote:
> >> On Tue, Jan 22, 2019, at 3:10 PM, Donald Steven wrote:
> >>> I would like to pass a list of some fields of a record to a function to
> >>> update those fields, leaving the other fields as they were. The list
> >>> will be different for each function call.  For example, one call might
> >>> want to change fields 1, 2 and 7, another call only field 8, another
> >>> call fields 3 and 4.  Of course, each field will have a unique name.
> >>> Each call to updateStatus (let's call it) would look like
> >>> updateStatus(Status, List_of_Fields_to_Update) -> code.
> >>>
> >> Records are just tuples. Use setelement with #rec.field:
> >>
> >> -module(rec).
> >> -export([new/0, update/3, test/0]).
> >>
> >> -record(rec, {a, b, c}).
> >>
> >> new() ->
> >>      #rec{}.
> >>
> >> update(R, Val, [F | Fields]) ->
> >>      R2 = setelement(F, R, Val),
> >>      update(R2, Val, Fields);
> >> update(R, _Val, []) ->
> >>      R.
> >>
> >> test() ->
> >>      R = rec:new(),
> >>      R2 = rec:update(R, 2, [#rec.a, #rec.c]),
> >>      R3 = rec:update(R2, 1, [#rec.b]),
> >>      R3.
> >> _______________________________________________
> >> erlang-questions mailing list
> >> [hidden email]
> >> http://erlang.org/mailman/listinfo/erlang-questions
> >
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Updating elements of a record

Donald Steven
Thanks everyone!

On 1/23/2019 11.07 AM, Raimo Niskanen wrote:

> On Tue, Jan 22, 2019 at 06:04:13PM -0500, Donald Steven wrote:
>> I should have said:
>>
>> test(Rec, [{a, Val},{c, Val}])
>>
>> On 1/22/2019 5.48 PM, Donald Steven wrote:
>>> Thanks Roger.  I see how this works, but the issue is as much how to
>>> get it to do:
>>>
>>> test([{a, Val},{c, Val}]), leaving, for example, field b untouched.
> setelement/3 does exactly that; copies the other fields
>
>>> On 1/22/2019 3.57 PM, Roger Lipscombe wrote:
>>>> On Tue, Jan 22, 2019, at 3:10 PM, Donald Steven wrote:
>>>>> I would like to pass a list of some fields of a record to a function to
>>>>> update those fields, leaving the other fields as they were. The list
>>>>> will be different for each function call.  For example, one call might
>>>>> want to change fields 1, 2 and 7, another call only field 8, another
>>>>> call fields 3 and 4.  Of course, each field will have a unique name.
>>>>> Each call to updateStatus (let's call it) would look like
>>>>> updateStatus(Status, List_of_Fields_to_Update) -> code.
>>>>>
>>>> Records are just tuples. Use setelement with #rec.field:
>>>>
>>>> -module(rec).
>>>> -export([new/0, update/3, test/0]).
>>>>
>>>> -record(rec, {a, b, c}).
>>>>
>>>> new() ->
>>>>       #rec{}.
>>>>
>>>> update(R, Val, [F | Fields]) ->
>>>>       R2 = setelement(F, R, Val),
>>>>       update(R2, Val, Fields);
>>>> update(R, _Val, []) ->
>>>>       R.
>>>>
>>>> test() ->
>>>>       R = rec:new(),
>>>>       R2 = rec:update(R, 2, [#rec.a, #rec.c]),
>>>>       R3 = rec:update(R2, 1, [#rec.b]),
>>>>       R3.
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> [hidden email]
>>>> http://erlang.org/mailman/listinfo/erlang-questions
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions