Array.prototype.ap

@tabatkins Thanks for the extra insight. fwiw my primary use case is using one arg and destructuring the returned values out to different variables, associated with the result of each function.

const [slug, date, wordCount, charCount] = [buildSlug, getDaysSince, getWordCount, getCharCount].ap(article);

In most cases, I have a number of functions that all expect the same complex type for their parameter(s). One common of this would be a user object, for which you might have functions to calculate the user's full name, age, obfuscated card number, etc.

One particular use case I had involved several helper functions I had which I called repeatedly when setting up and managing an active chess board.

This is the source:

const getPieceName = (piece) => piece.dataset.pieceName;
const getPieceKey = (piece) => piece.dataset.pieceKey;
const getPieceColor = (piece) => piece.dataset.pieceColor;
const getStartPos = (piece) => piece.dataset.startPosition;
const getCurrentPos = (piece) => piece.dataset.currentPosition;
const getPieceAlg = (piece) => piece.style.getPropertyValue('--position').trim();
const getPiecePos = (piece) => algToPos(piece.style.getPropertyValue('--position').trim());
const getMoveCount = (piece) => Number(piece.dataset.moveCount);
const checkIfCurrentPiece = (piece) => getPieceColor(piece) === currentTurn;
const checkIfOpponentPiece = (piece) => getPieceColor(piece) === opponentTurn;

With an ap method, I could destructure this information like so:

const [
  pieceName,
  pieceKey,
  pieceColor,
  startPos,
  currentPos,
  pieceAlg,
  piecePos,
  moveCount,
  isCurrentPiece,
  isOpponentPiece,
] = [
  getPieceName,
  getPieceKey,
  getPieceColor,
  getStartPos,
  getCurrentPos,
  getPieceAlg,
  getPiecePos,
  getMoveCount,
  checkIfCurrentPiece,
  checkIfOpponentPiece,
].ap(piece);

Granted, I think some sort of functional destructuring, initially discussed here, would be better fit for this, which I believe is covered by @rbuckton's proposal-extractors proposal: GitHub - tc39/proposal-extractors: Extractors for ECMAScript.

In that case, that might look like this:

const [
  getPieceName(pieceName),
  getPieceKey(pieceKey),
  getPieceColor(pieceColor),
  getStartPos(startPos),
  getCurrentPos(currentPos),
  getPieceAlg(pieceAlg),
  getPiecePos(piecePos),
  getMoveCount(moveCount),
  checkIfCurrentPiece(isCurrentPiece),
  checkIfOpponentPiece(isOpponentPiece),
] = piece;

@rbuckton Could you please indicate if I got the syntax right here? If that is accepted, I will rescind this proposal, but I was encouraged in my functional destructuring proposal to propose ap directly as array methods might be easier to justify.

If the syntax is wrong and would actually need to be […] = Array(10).fill(piece), then I might admittedly still see significant value in an ap method, or some other way to functionally destructure from a single argument.