node-options icon indicating copy to clipboard operation
node-options copied to clipboard

Would you accept a PR for .tap ?

Open FGRibreau opened this issue 7 years ago • 4 comments

:)

FGRibreau avatar May 10 '18 14:05 FGRibreau

As in a function that behaves as

const tap = (option, f) => option.map(value => (f(value), value));

? Could you give some examples of when this might be useful?

mwilliamson avatar May 10 '18 14:05 mwilliamson

Exactly :)

It's useful for mutation (sometimes needed) and debugging purpose.

e.g.:

option.tap(val => val.x += 1).valueOrElse(0);

FGRibreau avatar May 10 '18 14:05 FGRibreau

My preference is to use map in a separate statement. This keeps the API smaller, and makes the flow of data clearer without having to understand tap.

option.map(val => val.x += 1);
const value = option.valueOrElse(0);

mwilliamson avatar May 10 '18 15:05 mwilliamson

Indeed, one could always mimic .tap behaviour using map but my request concerns code flow & conciseness, it's not a must have but a very good nice to have and every time I use this library, I miss it :)

FGRibreau avatar May 11 '18 08:05 FGRibreau