zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

map_map.ex (1082B)


      1 defmodule Credo.Check.Refactor.MapMap do
      2   use Credo.Check,
      3     explanations: [
      4       check: """
      5       One `Enum.map/2` is more efficient than `Enum.map/2 |> Enum.map/2`.
      6 
      7       This should be refactored:
      8 
      9           [:a, :b, :c]
     10           |> Enum.map(&inspect/1)
     11           |> Enum.map(&String.upcase/1)
     12 
     13       to look like this:
     14 
     15           Enum.map([:a, :b, :c], fn letter ->
     16             letter
     17             |> inspect()
     18             |> String.upcase()
     19           end)
     20 
     21       The reason for this is performance, because the two separate calls
     22       to `Enum.map/2` require two iterations whereas doing the functions
     23       in the single `Enum.map/2` only requires one.
     24       """
     25     ]
     26 
     27   alias Credo.Check.Refactor.EnumHelpers
     28 
     29   @doc false
     30   def run(source_file, params \\ []) do
     31     issue_meta = IssueMeta.for(source_file, params)
     32 
     33     message = "One `Enum.map/2` is more efficient than `Enum.map/2 |> Enum.map/2`"
     34     trigger = "|>"
     35 
     36     Credo.Code.prewalk(
     37       source_file,
     38       &EnumHelpers.traverse(&1, &2, issue_meta, message, trigger, :map, :map, __MODULE__)
     39     )
     40   end
     41 end