Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flip the arguments of a curried function #1748

Merged
merged 5 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/modules/function.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,25 @@ Added in v2.0.0

## flip

Flips the order of the arguments of a function of two arguments.
Flips the arguments of a curried function.

**Signature**

```ts
export declare function flip<A, B, C>(f: (a: A) => (b: B) => C): (b: B) => (a: A) => C
export declare function flip<A, B, C>(f: (a: A, b: B) => C): (b: B, a: A) => C
```

**Example**

```ts
import { flip } from 'fp-ts/function'

const f = (a: number) => (b: string) => a - b.length

assert.strictEqual(flip(f)('aaa')(2), -1)
```

Added in v2.0.0

## flow
Expand Down
22 changes: 19 additions & 3 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,28 @@ export const constUndefined: Lazy<undefined> = /*#__PURE__*/ constant(undefined)
export const constVoid: Lazy<void> = constUndefined

/**
* Flips the order of the arguments of a function of two arguments.
* Flips the arguments of a curried function.
*
* @example
* import { flip } from 'fp-ts/function'
*
* const f = (a: number) => (b: string) => a - b.length
*
* assert.strictEqual(flip(f)('aaa')(2), -1)
*
* @since 2.0.0
*/
export function flip<A, B, C>(f: (a: A, b: B) => C): (b: B, a: A) => C {
return (b, a) => f(a, b)
export function flip<A, B, C>(f: (a: A) => (b: B) => C): (b: B) => (a: A) => C
/** @deprecated */
export function flip<A, B, C>(f: (a: A, b: B) => C): (b: B, a: A) => C
export function flip(f: Function): Function {
return (...args: Array<any>) => {
if (args.length > 1) {
return f(args[1], args[0])
}

return (a: any) => f(a)(args[0])
}
}

/**
Expand Down
10 changes: 7 additions & 3 deletions test/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ const f = (n: number) => n + 1
const g = (n: number) => n * 2

describe('function', () => {
it('flip', () => {
const f = (a: number, b: string) => a - b.length
U.deepStrictEqual(_.flip(f)('aaa', 2), -1)
test('flip', () => {
const f1 = (a: number) => (b: string) => a - b.length
const f2 = (a: number, b: string) => a - b.length

U.deepStrictEqual(_.flip(f1)('aaa')(2), -1)
// eslint-disable-next-line deprecation/deprecation
U.deepStrictEqual(_.flip(f2)('aaa', 2), -1)
})

it('not', () => {
Expand Down