From d3a6870097307f53054625b5ac4217d0a11ef787 Mon Sep 17 00:00:00 2001 From: Serge Tupchii Date: Fri, 19 Jan 2024 21:32:41 +0200 Subject: [PATCH] feat(emqx_utils): add pforeach/2,3 --- apps/emqx_utils/src/emqx_utils.erl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/apps/emqx_utils/src/emqx_utils.erl b/apps/emqx_utils/src/emqx_utils.erl index 046551e8d..0eeef2e5e 100644 --- a/apps/emqx_utils/src/emqx_utils.erl +++ b/apps/emqx_utils/src/emqx_utils.erl @@ -51,6 +51,8 @@ gen_id/0, gen_id/1, explain_posix/1, + pforeach/2, + pforeach/3, pmap/2, pmap/3, readable_error_msg/1, @@ -423,6 +425,15 @@ explain_posix(estale) -> "Stale remote file handle"; explain_posix(exdev) -> "Cross-domain link"; explain_posix(NotPosix) -> NotPosix. +-spec pforeach(fun((A) -> term()), list(A)) -> ok. +pforeach(Fun, List) when is_function(Fun, 1), is_list(List) -> + pforeach(Fun, List, ?DEFAULT_PMAP_TIMEOUT). + +-spec pforeach(fun((A) -> term()), list(A), timeout()) -> ok. +pforeach(Fun, List, Timeout) -> + _ = pmap(Fun, List, Timeout), + ok. + %% @doc Like lists:map/2, only the callback function is evaluated %% concurrently. -spec pmap(fun((A) -> B), list(A)) -> list(B).