list__filter_any

Thomas Charles CONWAY conway at cs.mu.OZ.AU
Mon Nov 2 08:15:31 AEDT 1998


Zoltan Somogyi writes:
> 

	[deletia]

> Others may wish to look at the new library predicate list__filter_any.

	[deletia]

> library/list.m:
> 	Add a new predicate list__filter_any for use by llds_out.m.

	[deletia]

> Index: library/list.m
> ===================================================================
> RCS file: /home/mercury1/repository/mercury/library/list.m,v
> retrieving revision 1.85
> diff -u -u -r1.85 list.m
> --- list.m	1998/09/15 07:38:31	1.85
> +++ list.m	1998/11/01 05:55:39
> @@ -432,6 +432,14 @@
>  :- pred list__filter_map(pred(X, Y), list(X), list(Y), list(X)).
>  :- mode list__filter_map(pred(in, out) is semidet, in, out, out) is det.
>  
> +	% list__filter_any(Pred, List) takes a closure with one input argument
> +	% and for each member of List `X', calls the closure. If call(Pred, X)
> +	% succeeds for any member of List, list__filter_any succeeds
> +	% immediately. If call(Pred, X) fails on all members of the list,
> +	% list__filter_any fails.
> +:- pred list__filter_any(pred(X), list(X)).
> +:- mode list__filter_any(pred(in) is semidet, in) is semidet.
> +
>  	% list__takewhile(Predicate, List, UptoList, AfterList) takes a
>  	% closure with one input argument, and calls it on successive members
>  	% of List as long as the calls succeed. The elements for which
> @@ -1022,6 +1030,13 @@
>  		M = [H0|M1]
>          ),
>          list__filter_map(P, T0, L1, M1).
> +
> +list__filter_any(P, [H|T]) :-
> +	( call(P, H) ->
> +		true
> +	;
> +		list__filter_any(P, T)
> +	).
>  

For what it's worth...

It's not much harder, and almost certainly more efficient
to write:
	some [] (member(X, List), foo(X))

compared to

	filter_any(foo, List)

Thomas
-- 
Thomas Conway <conway at cs.mu.oz.au>
Nail here [] for new monitor.  )O+



More information about the developers mailing list