Chainable "inspect" method for arrays (Array.prototype.inspect)

Related: Pipeline tap operator `|:`

The tap operator being discussed there doesn't directly help with this use case, as its operator precedence would prevent it from being inserted in the middle of a dot-chain like that. This problem was discussed there as well, and I believe the curren solution is to just convert the dot-chain to a pipline chain, like this:

myArray
  |> %.map(/* ... */)
  |: console.log(%)
  |> %.filter(/* ... */)
  |: console.log(%);

Not ideal, but it is a step in a good direction.


The .inspect() functionality being proposed feels like a very general-purpose piece of functionality that we might like to use in the middle of any dot-chained sequence, so instead of having to add an new method to any object type that may end up inside a long-ish sequence of dot-chains, I think it would be preferable to find some other (probably syntactic) solution that would solve this problem in the general case.

Using that tap operator and converting dot-chains to pipeline chains is one option, as shown above.

Another option would be to piggyback off of a proposal like call this. If that proposal were to go in as-is, we would be allowed to do this:

function inspect(callback) {
  callback(this);
}

myArray
  .map(/* ... */)
  ->inspect(arr => console.log(arr))
  .filter(/* ... */)
  ->inspect(arr => console.log(arr));

Here, the "inspect" function is being grabbed from the local scope and used inside the dot-chain. The inspect function could be made into a built-in function, that could then be used within any arbitrary dot-chain sequence, without requiring an individual inspect method to be defined on every object that wishes to support this behavior.