|
|
|
|
|
by lkitching
3499 days ago
|
|
I understood from your comment that the imperative version "avoids building the name size list of persons" that you thought the declaritive version would construct an intermediate List[Person] the same size as the source list of names. Most modern languages (e.g. C#, F#, Clojure, Rust) implement map and filter using lazy sequences rather than eagerly constructing intermediate collections (admittedly I don't have much experience with the languages you list). The use laziness will avoid the need to construct a large intermediate collection of Person instances. Obviously you'll need to iterate the entire source collection to find all valid Persons but the same applies to the imperative version. The lazy version composes better however and avoids extra work if some downstream caller only requires the first n valid Persons. |
|
But, there's already a good clean functional composable answer to this kind of thing,
fold takes an operation (like map does) a list (like map does) and an accumulator for building up results, and it returns the resulting accumulator.
so with tail recursion, you get no stack growth. (i mean head like the first element of names, and tail as everything else)the op would look something like
So fold trundles down the list of names, the op checks each name to see if it's good, and only adds the good names to the final list. whenever fold notices it's out of more names to try, it just returns whatever the current accumulator might be.Map forces you to build the intermediate representation, which the imperative version avoids.