% $Id: Partition.oz,v 1.1 2011/04/21 18:37:21 leavens Exp leavens $ % Partition splits an IStream into two depending on a predicate % % AUTHOR: Gary T. Leavens declare fun lazy {LFilter X|Xs Pred} %% REQUIRES: X|Xs is an IStream %% ENSURES: Result is the IStream of elements of X|Xs that satisfy Pred if {Pred X} then X|{LFilter Xs Pred} else {LFilter Xs Pred} end end fun {Compose F G} %% REQUIRES: F and G are functions of one argument, %% and G's return type is F's argument type. %% ENSURES: Result is the composition of F with G. fun {$ X} {F {G X}} end end proc {Partition Xs Pred ?OutTrue ?OutFalse} %% REQUIRES: OutTrue and OutFalse are undetermined, Xs is an IStream %% and Pred is a predicate that takes the kind of arguments in Xs. %% EFFECT: OutTrue and OutFalse are assigned IStreams of the elements %% that satisfy Pred (OutTrue) and that don't (OutFalse). OutTrue = {LFilter Xs Pred} OutFalse = {LFilter Xs {Compose Not Pred}} end