diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2022-03-24 13:28:26 -0700 |
---|---|---|
committer | Eli Schwartz <eschwartz93@gmail.com> | 2022-09-12 18:51:27 -0400 |
commit | 0ef3c2b4366f0e6256d55b3e5967bde681b6d606 (patch) | |
tree | 1071dcc888b01d9001bc5073cc830407852a496b /mesonbuild/mesonlib | |
parent | b5a66ce2a38d6f760a17833f3efe83c10b000116 (diff) | |
download | meson-0ef3c2b4366f0e6256d55b3e5967bde681b6d606.zip meson-0ef3c2b4366f0e6256d55b3e5967bde681b6d606.tar.gz meson-0ef3c2b4366f0e6256d55b3e5967bde681b6d606.tar.bz2 |
mesonlib: Add a function to find the first element in an iterable which matches a predicate
Diffstat (limited to 'mesonbuild/mesonlib')
-rw-r--r-- | mesonbuild/mesonlib/universal.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/mesonbuild/mesonlib/universal.py b/mesonbuild/mesonlib/universal.py index c501a30..4fbdf1e 100644 --- a/mesonbuild/mesonlib/universal.py +++ b/mesonbuild/mesonlib/universal.py @@ -92,6 +92,7 @@ __all__ = [ 'exe_exists', 'expand_arguments', 'extract_as_list', + 'first', 'generate_list', 'get_compiler_for_source', 'get_filenames_templates_dict', @@ -2292,3 +2293,17 @@ def pickle_load(filename: str, object_name: str, object_type: T.Type) -> T.Any: if major_versions_differ(obj.version, coredata_version): raise MesonVersionMismatchException(obj.version, coredata_version) return obj + + +def first(iter: T.Iterable[_T], predicate: T.Callable[[_T], bool]) -> T.Optional[_T]: + """Find the first entry in an iterable where the given predicate is true + + :param iter: The iterable to search + :param predicate: A finding function that takes an element from the iterable + and returns True if found, otherwise False + :return: The first found element, or None if it is not found + """ + for i in iter: + if predicate(i): + return i + return None |