filter, lazy append, flatMap implemented using foldRight