From a07f9a373b72d897e5b040b26a982f2000c71cba Mon Sep 17 00:00:00 2001 From: Matt Enlow Date: Fri, 12 Jan 2024 12:03:39 -0700 Subject: [PATCH] simplify duplicated code pipeline --- lib/credo/check/design/duplicated_code.ex | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/credo/check/design/duplicated_code.ex b/lib/credo/check/design/duplicated_code.ex index 1f5aeb399..d87355053 100644 --- a/lib/credo/check/design/duplicated_code.ex +++ b/lib/credo/check/design/duplicated_code.ex @@ -88,14 +88,11 @@ defmodule Credo.Check.Design.DuplicatedCode do end defp duplicate_nodes(source_files, mass_threshold) do - chunked_nodes = + nodes = source_files |> Enum.chunk_every(30) |> Task.async_stream(&calculate_hashes_for_chunk(&1, mass_threshold), timeout: :infinity, ordered: false) - |> Enum.map(fn {:ok, hashes} -> hashes end) - - nodes = - Enum.reduce(chunked_nodes, %{}, fn current_hashes, existing_hashes -> + |> Enum.reduce(%{}, fn {:ok, current_hashes}, existing_hashes -> Map.merge(existing_hashes, current_hashes, fn _hash, node_items1, node_items2 -> node_items1 ++ node_items2 end) @@ -197,10 +194,7 @@ defmodule Credo.Check.Design.DuplicatedCode do else hash = ast |> Credo.Code.remove_metadata() |> to_hash node_item = %{node: ast, filename: filename, mass: nil} - node_items = Map.get(existing_hashes, hash, []) - - updated_hashes = Map.put(existing_hashes, hash, node_items ++ [node_item]) - + updated_hashes = Map.update(existing_hashes, hash, [node_item], &[node_item | &1]) {ast, updated_hashes} end end