Fp-ts: Use tuple as function arguments

Created on 22 Dec 2019  路  9Comments  路  Source: gcanti/fp-ts

馃殌 Feature request

Current Behavior

If you want to pass a tuple as function arguments you have to deconstruct the tuple, but miss point free syntax.

declare const f: (a: A, b: B) => C;

pipe(
  tuple(a, b),
  x => f(...x),
);

Desired Behavior

Use function apT to resolve this

declare const f: (a: A, b: B) => C;

// apT will apply the tuple as function arguments
const fT: (t: [A, B]) => C = apT(f);

pipe(
  tuple(a, b),
  fT,
);

Suggested Solution

This function could be implemented as

const apT: <A extends Array<unknown>, B>(f: (...a: A) => B) => (a: A) => B =
    f => a => f(...a);

Your environment

Every OS

| Software | Version(s) |
| ---------- | ---------- |
| fp-ts | 2.2.0 |
| TypeScript | 3.7.2 |

new feature

Most helpful comment

@gcanti better than unary in my opinion

All 9 comments

Is unary the right name for this functionality? It clashes with all other references and definitions of unary function or operator I have seen.

@gkamperis we can change the name, no problem. How can we name a combinator that accepts a function (a: A, b: B) => C and returns a function (x: [A, B]) => C?

Ideally should be a <name> such that

  • <name>: (f: (a: A, b: B) => C) => (t: [A, B]) => C
  • un<name>: (f: (t: [A, B]) => C) => (a: A, b: B) => C)

(like the pair "curry" / "uncurry")

  • apT / unapT?
  • unary / ununary?

It's a tough one... But personally I would exclude unary.

Take the below with kindness... :)

untupleArg
destructureArg

In Scala there's tupled

@gcanti better than unary in my opinion

Thanks for the feature! :+1:

Are you considering adding unTupled yet?

export function unTupled<A extends Array<unknown>, B>(f: (a: A) => B): (...a: A) => B {
  return (...a) => f(a)
}

Great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

miguelferraro picture miguelferraro  路  3Comments

FredericLatour picture FredericLatour  路  3Comments

bioball picture bioball  路  4Comments

mmkal picture mmkal  路  3Comments

gcanti picture gcanti  路  3Comments