Rewrote some heavily imperative snippets in FP style

Just couldn't help to rewrite some heavily imperative snippets in FP style, which is (arguably) more readable and concise.

Collection view is properly used, so that mapping and filtering transformations don't need to traverse every element if not necessary.