aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src
diff options
context:
space:
mode:
authorHans Bolinder <[email protected]>2018-02-27 15:42:52 +0100
committerHans Bolinder <[email protected]>2018-03-09 14:45:42 +0100
commit5c7209c2874849a5a7339e23fdbd9aed1bb935b2 (patch)
tree66d3f1b29fb11f9db988b514800be926737a5641 /lib/stdlib/src
parent13e83e2e11b3d211c17c53140249a235b4d7a436 (diff)
downloadotp-5c7209c2874849a5a7339e23fdbd9aed1bb935b2.tar.gz
otp-5c7209c2874849a5a7339e23fdbd9aed1bb935b2.tar.bz2
otp-5c7209c2874849a5a7339e23fdbd9aed1bb935b2.zip
stdlib: Add function lists:search/2
This is essentially PR 102, https://github.com/erlang/otp/pull/102. The OTP Technical Board decided to change the name of the function to 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],