Skip to content

Commit

Permalink
Meilleur nommage
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitfred committed Dec 19, 2024
1 parent bf97134 commit c3344a0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions apps/transport/lib/registry/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ defmodule Transport.Registry.Engine do
create_empty_csv_with_headers(output_file)

enumerate_gtfs_resources(limit, formats)
|> Extractor.traverse(&prepare_extractor/1)
|> Extractor.map_result(&prepare_extractor/1)
|> Task.async_stream(&download/1, max_concurrency: 10, timeout: 120_000)
# one for Task.async_stream
|> Extractor.keep_results()
|> Extractor.cat_results()
# one for download/1
|> Extractor.keep_results()
|> Extractor.traverse(&extract_from_archive/1)
|> Extractor.cat_results()
|> Extractor.map_result(&extract_from_archive/1)
|> dump_to_csv(output_file)
end

Expand Down
14 changes: 7 additions & 7 deletions apps/transport/lib/registry/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ defmodule Transport.Registry.Extractor do

@callback extract_from_archive(path :: Path.t()) :: result([Stop.t()])

@spec keep_results(Stream.t(result(term()))) :: Stream.t(term())
def keep_results(enumerable), do: Stream.flat_map(enumerable, &keep_result/1)
@spec cat_results(Stream.t(result(term()))) :: Stream.t(term())
def cat_results(enumerable), do: Stream.flat_map(enumerable, &keep_ok/1)

defp keep_result({:ok, result}), do: [result]
defp keep_result(_), do: []
defp keep_ok({:ok, result}), do: [result]
defp keep_ok(_), do: []

@spec traverse(Stream.t(term()), (term() -> result(term()))) :: Stream.t(term())
def traverse(enumerable, mapper) do
@spec map_result(Stream.t(term()), (term() -> result(term()))) :: Stream.t(term())
def map_result(enumerable, mapper) do
enumerable
|> Stream.map(mapper)
|> keep_results()
|> cat_results()
end
end
22 changes: 11 additions & 11 deletions apps/transport/test/registry/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ defmodule Transport.Registry.ExtractorTest do
require Integer
alias Transport.Registry.Extractor

test "keep_results" do
assert [] == keep_results([])
assert [] == keep_results([{:error, "Error message"}])
assert [1, 3] == keep_results([{:ok, 1}, {:error, "Error message"}, {:ok, 3}])
test "cat_results" do
assert [] == cat_results([])
assert [] == cat_results([{:error, "Error message"}])
assert [1, 3] == cat_results([{:ok, 1}, {:error, "Error message"}, {:ok, 3}])
end

test "traverse" do
assert [] == traverse([], &even_is_forbidden/1)
assert [1, 3, 5, 7, 9] == traverse(1..10, &even_is_forbidden/1)
test "map_result" do
assert [] == map_result([], &even_is_forbidden/1)
assert [1, 3, 5, 7, 9] == map_result(1..10, &even_is_forbidden/1)
end

defp keep_results(enumerable) do
enumerable |> Extractor.keep_results() |> Enum.to_list()
defp cat_results(enumerable) do
enumerable |> Extractor.cat_results() |> Enum.to_list()
end

defp traverse(enumerable, mapper) do
enumerable |> Extractor.traverse(mapper) |> Enum.to_list()
defp map_result(enumerable, mapper) do
enumerable |> Extractor.map_result(mapper) |> Enum.to_list()
end

defp even_is_forbidden(i) when Integer.is_odd(i), do: {:ok, i}
Expand Down

0 comments on commit c3344a0

Please sign in to comment.