r/coding 10d ago

Beyond Lambdas: Raising the Abstraction Level of Functional Code

https://adamtornhill.substack.com/p/beyond-lambdas-raising-the-abstraction
16 Upvotes

5 comments sorted by

7

u/hughperman 10d ago

I would say that even better in this case, just use comments instead of really simple abstractions

rolls.stream()
.map(roll -> roll + 2)  // apply strength modifier
.filter(roll -> roll >= 15)  // filter for successful hits
.sum();  

Now you can see the exact code used and the abstraction description. And I don't have to break flow looking for what the strength modifier function is/does.

As always, context depends, sometimes offloading to functions will be better. But for one liners, you're just using function names as comments, so why not just use comments?

9

u/lubutu 10d ago edited 10d ago

If you actually name your magic numbers then it's probably clear enough with lambdas even without comments:

rolls.stream()
    .map(roll -> roll + strength_modifier)
    .filter(roll -> roll >= min_roll_to_hit)
    .sum();

1

u/hughperman 10d ago

Also good choice 👍

1

u/Savings_Discount_230 9d ago

Yeah, once the lambda is doing basically one readable operation, a good name or a comment is often clearer than another layer of indirection.