aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src
diff options
context:
space:
mode:
authorHans Bolinder <[email protected]>2018-03-20 10:42:18 +0100
committerGitHub <[email protected]>2018-03-20 10:42:18 +0100
commit269814984a005f23fd1b5305c99cd92b68fd1641 (patch)
tree466154699c1150650b11e2e1c90e775dd9e1e86a /lib/stdlib/src
parent01460976cfa3222f618ac494f462eccd0ba9a1c8 (diff)
parent5c7209c2874849a5a7339e23fdbd9aed1bb935b2 (diff)
downloadotp-269814984a005f23fd1b5305c99cd92b68fd1641.tar.gz
otp-269814984a005f23fd1b5305c99cd92b68fd1641.tar.bz2
otp-269814984a005f23fd1b5305c99cd92b68fd1641.zip
Merge pull request #1744 from uabboli/hasse/stdlib/lists_search/OTP-14675
stdlib: Add function lists:search/2
Diffstat (limited to 'lib/stdlib/src')
-rw-r--r--lib/stdlib/src/lists.erl19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/stdlib/src/lists.erl b/lib/stdlib/src/lists.erl
index af9d63ddd6..06c90c0280 100644
--- a/lib/stdlib/src/lists.erl
+++ b/lib/stdlib/src/lists.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 1996-2016. All Rights Reserved.
+%% Copyright Ericsson AB 1996-2018. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
@@ -38,8 +38,8 @@
-export([all/2,any/2,map/2,flatmap/2,foldl/3,foldr/3,filter/2,
partition/2,zf/2,filtermap/2,
- mapfoldl/3,mapfoldr/3,foreach/2,takewhile/2,dropwhile/2,splitwith/2,
- split/2,
+ mapfoldl/3,mapfoldr/3,foreach/2,takewhile/2,dropwhile/2,
+ search/2, splitwith/2,split/2,
join/2]).
%%% BIFs
@@ -1399,6 +1399,19 @@ dropwhile(Pred, [Hd|Tail]=Rest) ->
end;
dropwhile(Pred, []) when is_function(Pred, 1) -> [].
+-spec search(Pred, List) -> {value, Value} | false when
+ Pred :: fun((T) -> boolean()),
+ List :: [T],
+ Value :: T.
+
+search(Pred, [Hd|Tail]) ->
+ case Pred(Hd) of
+ true -> {value, Hd};
+ false -> search(Pred, Tail)
+ end;
+search(Pred, []) when is_function(Pred, 1) ->
+ false.
+
-spec splitwith(Pred, List) -> {List1, List2} when
Pred :: fun((T) -> boolean()),
List :: [T],