remeda icon indicating copy to clipboard operation
remeda copied to clipboard

feat: Add tap function

Open vlad-iakovlev opened this issue 1 year ago • 3 comments

Replace this quote with a description of your changes

See #149, #114

Make sure that you:

  • [ ] Typedoc added for new methods and updated for changed
  • [ ] Tests added for new methods and updated for changed
  • [ ] New methods added to src/index.ts
  • [ ] New methods added to mapping.md

vlad-iakovlev avatar Jul 29 '22 09:07 vlad-iakovlev

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit b993c192da9c5c6e2e6b7ea10da983d254271ace:

Sandbox Source
remeda-example-vanilla Configuration

codesandbox-ci[bot] avatar Jul 29 '22 09:07 codesandbox-ci[bot]

any chance we can add the lazy version as described here?

  • https://github.com/remeda/remeda/pull/114#issuecomment-754524515

TkDodo avatar Jul 29 '22 10:07 TkDodo

@TkDodo not changed yet. See if there any way to implement this on current version of pipe

As I read discussion, @lstkz suggested here to run fn callback on every lazy call. That already could be done with R.map(R.tap(fn)), no changes required. It that way is ok, let's left as-is.

For me more interesting to make tap that will run tap callback in pipe with array but only with those element that will go through the pipe (like in this test). That's what I'm trying to implement now

vlad-iakovlev avatar Jul 29 '22 10:07 vlad-iakovlev

@TkDodo I totally forgot how should R.pipe(['a', 'b', 'c'], R.tap(console.log)) print result?

  1. 3 calls with 'a', 'b', 'c'
  2. 1 call with 'a,b,c' or something like that?

vlad-iakovlev avatar Jan 15 '23 19:01 vlad-iakovlev

I hope you can continue with this PR, tap is a very useful quick debug tool.

paour avatar Mar 31 '23 09:03 paour

@vlad-yakovlev, it's been almost a year since this was opened, where do we stand regarding this, do you want to see it shipped? I'm closing the PR for now for housekeeping, but feel free to open it again and I'll see what I can do to help.

eranhirsch avatar Jul 13 '23 09:07 eranhirsch

hey, i do want to see this merged, i think the current implementation works fine as-is?

R.pipe(['a', 'b', 'c'], R.tap(console.log)) has console.log called once

R.pipe(['a', 'b', 'c'], R.map(R.tap(console.log))) has console.log called thrice

in particular i dont think R.tap should be lazy, as it does not take an array as input, it takes in any T and returns T?

with respect to

For me more interesting to make tap that will run tap callback in pipe with array but only with those element that will go through the pipe (like in this test). That's what I'm trying to implement now

i think this is fine if not implemented? that would mean having different behavior if the input type is an array or not

as another example, consider R.identity, which in theory could be lazy, but is not:

R.pipe(['a', 'b', 'c'], R.identity, R.take(1)) means R.identity still gets called with ['a', 'b', 'c']

(maybe it'd be nice if it was lazy, but i would consider this a change that will change other functions as well, and it would change behavior)

cjquines avatar Sep 23 '23 14:09 cjquines

in particular i dont think R.tap should be lazy

why not? As shown in this example, we'd only expect 3 logs, not 5

R.pipe(
  [1, 2, 3, 4, 5],
  R.tap(console.log),
  R.take(3)
);

I don't mind if we don't have that in the first version, but since it works for other functions, it should work for tap, too, don't you think?

TkDodo avatar Nov 03 '23 16:11 TkDodo

i did say that i think this example would have one log:

R.pipe(
  [1, 2, 3, 4, 5],
  R.tap(console.log),
  R.take(3)
);

should log [1, 2, 3, 4, 5], while

R.pipe(
  [1, 2, 3, 4, 5],
  R.map(R.tap(console.log)),
  R.take(3)
);

should log 1, then 2, then 3. it follows from map being lazy, not tap.

the type of tap should be T => T, not T[] => T.

cjquines avatar Nov 03 '23 23:11 cjquines