-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
53 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Transport.Registry.Result do | ||
@moduledoc """ | ||
Type and utilities to represent results. | ||
""" | ||
|
||
@type t(positive) :: {:ok, positive} | {:error, binary()} | ||
|
||
def ok(positive), do: {:ok, positive} | ||
|
||
def error(message), do: {:error, message} | ||
|
||
@spec cat_results(Stream.t(t(term()))) :: Stream.t(term()) | ||
def cat_results(enumerable), do: Stream.flat_map(enumerable, &keep_ok/1) | ||
|
||
defp keep_ok({:ok, result}), do: [result] | ||
defp keep_ok(_), do: [] | ||
|
||
@spec map_result(Stream.t(term()), (term() -> t(term()))) :: Stream.t(term()) | ||
def map_result(enumerable, mapper) do | ||
enumerable | ||
|> Stream.map(mapper) | ||
|> cat_results() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters