【问题标题】:Erlang Select highest bidsErlang 选择最高出价
【发布时间】:2020-01-19 12:57:33
【问题描述】:

在下面的 select_highest_bids 函数中,使用 orddict:new() 代替 list 是否更好,如下所示?

OrderedBatches = lists:foldl(fun high_bid_filter/2, orddict:new(), Campaigns)

我们如何更改 high_bid_filter 以返回 orddict ?

select_highest_bids(Campaigns, BidRequest) ->
    lists:map(fun({_, C}) -> C end,
    lists:foldl(fun high_bid_filter/2, [], Campaigns),


high_bid_filter(Campaign = #campaign_response{bid = BR = #bid_response{ad_id = AdID, cpm_bid = Score}}, Campaigns) ->

    case lists:keyfind(AdID, 1, Campaigns) of
        false ->
            [{AdID, Campaign}|Campaigns];
        {AdID, _ = #campaign_response{bid = _ = #bid_response{cpm_bid = EB}}} when EB >= Score ->
            Campaigns;
        {AdID, _ = #campaign_response{bid = _ = #bid_response{cpm_bid = EB}}} when EB < Score ->
            lists:keystore(AdID, 1, Campaigns, {AdID, Campaign})
    end.

【问题讨论】:

    标签: list dictionary erlang


    【解决方案1】:

    这里是文件夹功能

    high_bid_filter(Campaign = #campaign_response{bid = #bid_response{ad_id = AdID, cpm_bid = Score}}, Campaigns) ->
      case orddict:take(AdID, Campaigns) of
        {V, _} when V >= Score -> 
          % find value but higher than Offer
          Campaigns;
        {V, Dict} ->
          % update higher value
          orddict:store(AdID, Campaign, Dict);
        error -> 
          % 
          orddict:store(AdID, Campaign, Campaigns)
      end.
    
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 2018-10-07
      • 1970-01-01
      相关资源
      最近更新 更多