by okasaki · 5y ago · 0 comments
errors = list()
for user in users:
if not sendEmail(user.email)
errors.append("Error sending emails")Because I say "what" do to do without going into details of "how" I feel like I have spent less mental capacity reading this portion of code.
However, it does require your transformations to be simple and composable, shove a giant multi-line lambda in there it yes, the loop will be better.
For what it's worth, your example doesn't map 1-to-1 to the intention of what I wrote. Doing it in a more imperative style, I'd have to write
emails = list()
for user in users:
emails.push(user.email)
res = sendEmail emails
if res.IsError:
Error("Error sending emails")
else:
res
(excuse my pseudo Python) try:
return sendEmail(user.email for user in users)
except IsError:
Error("Error sending emails") # if it does something
I agree FP pipelines are nice, but not everything needs it.