Skip to content

Commit 7a39149

Browse files
committed
refactor(quote_context): collapse paginate loop state into a map
The private paginate/9 loop accumulated two state arguments (acc, seen). Merging them into a single state map drops the arity to 8 and clears the Credo FunctionArity failure.
1 parent 82e7c63 commit 7a39149

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

lib/longbridge/quote_context.ex

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -450,26 +450,29 @@ defmodule Longbridge.QuoteContext do
450450
batch_size = min(Keyword.get(opts, :batch_size, 1000), 1000)
451451
trade_session = Keyword.get(opts, :trade_session, :NORMAL_TRADE)
452452

453-
paginate(pid, symbol, period, count, adjust_type, batch_size, trade_session, [], MapSet.new())
453+
paginate(pid, symbol, period, count, adjust_type, batch_size, trade_session, %{
454+
acc: [],
455+
seen: MapSet.new()
456+
})
454457
end
455458

456-
defp paginate(_pid, _symbol, _period, remaining, _adjust, _batch, _session, acc, _seen)
459+
defp paginate(_pid, _symbol, _period, remaining, _adjust, _batch, _session, state)
457460
when remaining <= 0 do
458-
{:ok, sort_by_ts(acc)}
461+
{:ok, sort_by_ts(state.acc)}
459462
end
460463

461-
defp paginate(pid, symbol, period, remaining, adjust, batch, session, acc, seen) do
464+
defp paginate(pid, symbol, period, remaining, adjust, batch, session, state) do
462465
fetch_count = min(batch, remaining)
463466

464467
with {:ok, resp} <- candlesticks(pid, symbol, period, fetch_count, adjust, session),
465468
candles when candles != [] <- resp.candlesticks do
466-
new_candles = Enum.reject(candles, &MapSet.member?(seen, &1.timestamp))
469+
new_candles = Enum.reject(candles, &MapSet.member?(state.seen, &1.timestamp))
467470

468471
if new_candles == [] do
469-
{:ok, sort_by_ts(acc)}
472+
{:ok, sort_by_ts(state.acc)}
470473
else
471-
new_seen = Enum.reduce(new_candles, seen, &MapSet.put(&2, &1.timestamp))
472-
new_acc = new_candles ++ acc
474+
new_seen = Enum.reduce(new_candles, state.seen, &MapSet.put(&2, &1.timestamp))
475+
new_acc = new_candles ++ state.acc
473476
fetched = length(new_candles)
474477

475478
if fetched < fetch_count do
@@ -483,14 +486,13 @@ defmodule Longbridge.QuoteContext do
483486
adjust,
484487
batch,
485488
session,
486-
new_acc,
487-
new_seen
489+
%{state | acc: new_acc, seen: new_seen}
488490
)
489491
end
490492
end
491493
else
492494
[] ->
493-
{:ok, sort_by_ts(acc)}
495+
{:ok, sort_by_ts(state.acc)}
494496

495497
{:error, _reason} = err ->
496498
err

0 commit comments

Comments
 (0)