我得到list_nth / 2函数的function_clause

时间:2018-07-04 19:00:50

标签: erlang

我在这段代码上有问题,这真的很奇怪,也许有人可以告诉我发生了什么事

public function scopeCloseTo(Builder $query, $latitude, $longitude)
{
    return $query->whereRaw("
       ST_Distance_Sphere(
            point(longitude, latitude),
            point(?, ?)
        ) * .000621371192 < delivery_max_range
    ", [
        $longitude,
        $latitude,
    ]);
}

在194行:项目=列表:nth(NextIndex,SuggestedUsers)在日志中抛出错误,我不知道为什么?

suggested_system_actor_data_for_hookup(RealActorId, RealActorName, RealActorLang, SuggestedUsers = [_|_]) ->
  case length(SuggestedUsers) > 0 of
    true ->
      SuggestedUserIndexRedisKey = "real_actor." ++ helper:convert_value_to_list(RealActorId) ++ "." ++
        helper:convert_value_to_list(RealActorLang) ++ ".hookup.suggested_user_index",
      NextIndex = next_index(SuggestedUserIndexRedisKey, SuggestedUsers),

      case NextIndex of
        not_found -> not_found;
        NextIndex ->
          case is_list(SuggestedUsers) of
            true ->
              Item = lists:nth(NextIndex, SuggestedUsers), % <--- throws error %
              SystemActorId = helper:convert_value_to_list(ej:get({"user_id"}, Item)),
              SystemActorSiteId = helper:convert_value_to_list(ej:get({"site_id"}, Item)),

              case helper:has_redis_key_last_visit_timestamp(RealActorId, SystemActorId) of
                {ok, no} ->
                  ?MODULE:send_visit_and_sleep(RealActorName, RealActorId, SystemActorId),
                  {SystemActorId, SystemActorSiteId};
                {ok, _LastVisitTimestamp} -> {SystemActorId, SystemActorSiteId}
              end;
            false ->
              helper:log_error("SuggestedUsers is not a list = " ++ helper:convert_value_to_list(SuggestedUsers), ?LINE, ?MODULE),
            not_found
          end
      end;
    false -> not_found
  end;
suggested_system_actor_data_for_hookup(_RealActorId, _RealActorName, _RealActorLang, _SuggestedUsers = []) ->
  not_found.

1 个答案:

答案 0 :(得分:3)

您的代码段中有许多无用的代码。由于您在函数头中使用了模式匹配SuggestedUsers = [_|_],因此您知道RecommendationsUsers是一个非空列表,因此length(SuggestedUsers) > 0始终为true,与is_list(SuggestedUsers)相同。

出现此错误,只有两种可能的原因:

  • NextIndex不是整数> 0
  • NextIndex大于长度(SuggestedUsers)

错误报告显示NextIndex = length(SuggestedUsers)+ 1

我认为您可以通过以下方式简化功能:

suggested_system_actor_data_for_hookup(RealActorId, RealActorName, RealActorLang, SuggestedUsers = [_|_]) ->
  SuggestedUserIndexRedisKey = "real_actor." ++ helper:convert_value_to_list(RealActorId) ++ "." ++
    helper:convert_value_to_list(RealActorLang) ++ ".hookup.suggested_user_index",
  NextIndex = next_index(SuggestedUserIndexRedisKey, SuggestedUsers),

  case (NextIndex > 0) and (NextIndex =< length(SuggestedUsers)) of
  % an atom is bigger than any integer, so the test covers the case NextIndex == not_found 
    false -> not_found;
    true ->
      Item = lists:nth(NextIndex, SuggestedUsers),
      SystemActorId = helper:convert_value_to_list(ej:get({"user_id"}, Item)),
      SystemActorSiteId = helper:convert_value_to_list(ej:get({"site_id"}, Item)),
      {ok,Answer} = helper:has_redis_key_last_visit_timestamp(RealActorId, SystemActorId),
      maybe_send_visit_and_sleep(Answer,RealActorName, RealActorId, SystemActorId);
      {SystemActorId, SystemActorSiteId}
    end;
suggested_system_actor_data_for_hookup(_RealActorId, _RealActorName, _RealActorLang, _SuggestedUsers = []) ->
  not_found.

maybe_send_visit_and_sleep(no,RealActorName, RealActorId, SystemActorId) ->
  send_visit_and_sleep(RealActorName, RealActorId, SystemActorId);
maybe_send_visit_and_sleep(_,_,_,_) ->
  ok.