<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 23, 2015 at 11:06 AM, YuanZhiqian <span dir="ltr"><<a href="mailto:on-your-mark@hotmail.com" target="_blank">on-your-mark@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Sorry to bother you again... I am totally a green hand in Erlang :(</blockquote></div><div class="gmail_extra"><br></div>Suppose for the moment you are willing to use a map to store all your records. Then we can simplify the code quite a lot. List-representations are best used if you often process all of the the elements in the list, or you can use any element of the list, particularly the first one. If you are pin-pointing a specific entry, the map is a far better data structure. First a module definition:</div><div class="gmail_extra"><br><div class="gmail_extra">-module(z).</div><div class="gmail_extra">-export([cal_win_notice/2]).</div><div class="gmail_extra"><br></div><div class="gmail_extra">Next, lets hack up a couple of records to match what is going on in your code:</div><div class="gmail_extra"><br></div><div class="gmail_extra">-record(state, {</div><div class="gmail_extra">        '...',</div><div class="gmail_extra">        companies,</div><div class="gmail_extra">        campaigns</div><div class="gmail_extra">}).</div><div class="gmail_extra"><br></div><div class="gmail_extra">-record(company_info, {</div><div class="gmail_extra">        id,</div><div class="gmail_extra">        budget,</div><div class="gmail_extra">        consumption,</div><div class="gmail_extra">        campaigns</div><div class="gmail_extra">}).</div><div class="gmail_extra"><br></div><div class="gmail_extra">The mapply/3 function here tries to apply F to the map value given by the key ID. If no such ID exists, it leaves the map alone.</div><div class="gmail_extra"><br></div><div class="gmail_extra">mapply(F, ID, Map) -></div><div class="gmail_extra">    case maps:get(ID, Map, undefined) of</div><div class="gmail_extra">        undefined -> Map;</div><div class="gmail_extra">        T -> Map#{ ID := F(T) }</div><div class="gmail_extra">    end.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Now, we can use this function. Make an F which does what we want. Then apply it. We can check if any change were made to the map by matching on the unchanged map. Otherwise, the map has been changed, and we can update the map with the new one.</div><div class="gmail_extra"><br></div><div class="gmail_extra">cal_win_notice({CoID, _Ca, _AdGrp, Price, _Cur}, #state { companies = Cos } = State) -></div><div class="gmail_extra">    F = fun(#company_info{ consumption = Cons } = X) -> X#company_info{ consumption = Cons + Price } end,</div><div class="gmail_extra">    case mapply(F, CoID, Cos) of</div><div class="gmail_extra">        Cos -> {not_found, State}; %% Unchanged map</div><div class="gmail_extra">        Changed -> {ok, State#state { companies = Changed }}</div><div class="gmail_extra">    end.</div><div><br></div><div>Want to make two changes?</div><div><br></div><div>G = fun(#company_info { consumption = Cons } = X) -> X#company_info { consumption = Cons - Price } end,</div><div><br></div><div>compose(F, G) -></div><div>    fun(X) -> G(F(X)) end.</div><div><br></div><div>then mapply(compose(F, G), CoID, Cos) ...</div><div><br></div><div><br></div><br clear="all"><div><br></div>-- <br><div class="gmail_signature">J.</div>
</div></div>