Oh! yes. Yes we're on the same page as far as laziness goes. The lazy version will only construct as much of the intermediate list as you need to get the next answer, and potentially the garbage collector can reclaim the parts of the intermediate list that have already gone past.
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.
foldl(op, names, acc){
if(names.empty)
return acc
else
foldl(op, names.tail, op(names.head, acc)
}
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
op(name, acc){
let p = Person.init(name)
if(p.isValid)
return acc += [p]
else
return acc
}
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.