|
hmm, i don't think lazyness is enough. for example, in the billion names example, let's say the first and last elements are valid. at the first step you'll wind up with something like validPerson : thunk
after n steps where n < 1e9 validPerson : invalid : invalid : invalid : ... : thunk
then finally at n = 1e9 validPerson : invalid : ... : validPerson
because the intermediate calculations still need to happen.getting that first answer is cheap and quick. getting the second answer requires evaluating those billion - 1 thunks. maybe you're thinking of list fusion? AFAIK, only haskell does that. Perhaps i'm misunderstanding your point. But even then, lisp, scheme, smalltalk, python and perl don't implement map and filter in a lazy way. Of course, i'm thinking in terms of haskell lazy, perhaps, again, i'm misunderstanding and you mean something else. In retrospect, i think i'll revise my opinion and say map.filter is probably a bigger code smell than referentially transparent mutation. |
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.