Feedback: Functional vs. Imperative solution performs 3 passes vs 1 in the imperative version #116
Yuhang Ji (wakuflair)
started this conversation in
General
Replies: 1 comment
|
Yuhang Ji (@wakuflair) Thanks for the feedback. The intent was to emphasize that while the functional style can be very elegant, complex logic is sometimes best done imperatively. Can you please provide a code snippet to illustrate what you had in mind for the example? |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
In the refactoring_imperative_to_functional exercise, the solution computes
avg_powerin one iterator pass,max_tempin a second, then partitions servers by health in a third explicit for loop — three passes total over the fleet. The imperative version does all three in a single pass, making it objectively faster by a 3× constant factor.The comment correctly notes that the three-way partition "is BETTER as a loop", but the same reasoning applies to the scalar aggregates: splitting them into separate iterator chains comes at a performance cost that goes unacknowledged. If the goal is to show when functional style is appropriate, the example would be stronger if it either used a single-pass
fold(to demonstrate functional style done right) or explicitly called out that the hybrid approach trades some performance for readability in the aggregate computations.As it stands, the solution is neither more readable nor more performant than the imperative version, which may leave new learners with a misleading impression of what functional style offers in Rust.
All reactions