<div dir="ltr">On Mon, Nov 23, 2015 at 4: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><div class="gmail_extra"><div class="gmail_quote"><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">


<div><div dir="ltr"><div><br></div><div><div>-record(company_info, {</div><div>        company_id,</div><div>        budget,</div><div>        consumption,</div><div>        compaign_ids</div><div>    }).</div></div><div><br></div><div><div>cal_win_notice({Co_id, Ca, Adgrp, Price, Cur}, </div><div>    #state{company_list = Co_list, campaign_list= Ca_list} = State) -></div><div>    case lists:any(fun(#company_info{company_id = A}) -> A == Co_id end, </div><div>            Co_list) of</div><div>        true -> </div><div><b><font color="#ac193d">            New_co_list = lists:map(fun(R) -> </font></b></div><div><b><font color="#ac193d">                        case R#company_info.company_id of</font></b></div><div><b><font color="#ac193d">                            Co_id -></font></b></div><div><b><font color="#ac193d">                                R#company_info{consumption = R#company_info.consumption + Price};</font></b></div><div><b><font color="#ac193d">                            _ -></font></b></div><div><b><font color="#ac193d">                                R</font></b></div><div><b><font color="#ac193d">                        end</font></b></div><div><b><font color="#ac193d">                    end,</font></b></div><div><b><font color="#ac193d">                Co_list),</font></b></div><div><b><font color="#ac193d">            {ok, State#state{company_list = New_co_list}};</font></b></div><div>        false -></div><div>            {not_found, State} </div><div>    end.</div></div></div></div></blockquote><div><br></div><div>Something like this (not compiled or tested): </div></div><div><br></div><div><font face="monospace, monospace">cal_win_notice({Co_id, _, _, Price, _}, </font><font face="monospace, monospace">#state{company_list=Co_list}=State</font><span style="font-family:monospace,monospace">) -></span></div><div><span style="font-family:monospace,monospace">    case lists:mapfoldl(fun (#company_info{company_id=CID, consumption=C} = E, _)</span></div><div><span style="font-family:monospace,monospace">                          when CID == Co_id -></span></div><div><span style="font-family:monospace,monospace">                                {E#company_info{consumption=C + price}, true};</span></div><div><span style="font-family:monospace,monospace">                            (E, Acc) -></span></div><div><span style="font-family:monospace,monospace">                                {E, Acc}</span></div><div><span style="font-family:monospace,monospace">                        end, </span><span style="font-family:monospace,monospace">false, Co_list) of</span></div><div><span style="font-family:monospace,monospace">     {_, false} -> {not_found, State};</span></div><div><span style="font-family:monospace,monospace">     {NL, true} -> {ok, </span><font face="monospace, monospace">State#state{company_list=NL}}</font></div><div><font face="monospace, monospace">    end.</font></div><div><br></div><div>This only makes a single pass through the list. If you need to update more than one field, you do it in the same update. For instance, if you need to add $1000 to the budget, as well (ignoring whether it makes sense in this function) replace line 4 with:</div><div><br></div><div><div><span style="font-family:monospace,monospace">                                {E#company_info{consumption=C + price,</span></div><div><span style="font-family:monospace,monospace">                                                budget=B + 1000}, true};</span></div></div><div><span style="font-family:monospace,monospace"><br></span></div><div class="gmail_extra">(you'll also have to pattern-match B with the budget field in line 2.)</div><div class="gmail_extra"><br></div>-- <br><div class="gmail_signature">Rich<br></div>
</div></div>