3 ms·
> In this case, it's unfortunate that there's no, "will this name create a valid person object?" predicate. much much better to filter the names, then make the
by masklinn 10y ago
> In this case, it's unfortunate that there's no, "will this name create a valid person object?" predicate. much much better to filter the names, then make the objects.
No "object" is made if the name can't create a "valid person object", it'll return a stack-allocated null/nothing value. That pattern is also much better for concurrency issues.
> much much better to filter the names, then make the objects.
You're just duplicating the validation logic (or worse, the "objects creation" assumes it's given valid names and does no checks)
- jfoutz 10y ago> You're just duplicating the validation logic (or worse, the "objects creation" assumes it's given valid names and does no checks) I think the right way to do it is with a fold.
- masklinn 10y agoIn Haskell you'd use `mapMaybe :: (a -> Maybe b) -> [a] -> [b]`[0], it's a combined map+filter+map specialised for Maybe (option types). Rust has Iterator::filter_map[1] which does more or less the same thing. [0] http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Maybe.html#v:mapMaybe http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Ma... [1] https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter_map https://doc.rust-lang.org/std/iter/trait.Iterator.html#metho...