Skip to content

Commit

Permalink
Don't add flipArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid authored and gcanti committed Sep 1, 2022
1 parent a6810dc commit 91113fd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 56 deletions.
25 changes: 1 addition & 24 deletions docs/modules/function.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Added in v2.0.0
- [constant](#constant)
- [decrement](#decrement)
- [flip](#flip)
- [flipArgs](#flipargs)
- [flow](#flow)
- [hole](#hole)
- [identity](#identity)
Expand Down Expand Up @@ -326,29 +325,7 @@ const f = (a: number) => (b: string) => a - b.length
assert.strictEqual(flip(f)('aaa')(2), -1)
```

Added in v2.13.0

## flipArgs

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

**Signature**

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

**Example**

```ts
import { flipArgs } from 'fp-ts/function'
const f = (a: number, b: string) => a - b.length
assert.strictEqual(flipArgs(f)('aaa', 2), -1)
```

Added in v2.13.0
Added in v2.0.0

## flow

Expand Down
4 changes: 2 additions & 2 deletions dtslint/ts3.5/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as _ from '../../src/function'
import * as RA from '../../src/ReadonlyArray'

//
// flipArgs
// flip
//

// should handle generics
_.flipArgs(RA.snoc) // $ExpectType <A>(b: A, a: ReadonlyArray<A>) => ReadonlyNonEmptyArray<A>
_.flip(RA.snoc) // $ExpectType <A>(b: A, a: ReadonlyArray<A>) => ReadonlyNonEmptyArray<A>

//
// tuple
Expand Down
22 changes: 3 additions & 19 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,37 +203,21 @@ export const constVoid: Lazy<void> = constUndefined
*
* assert.strictEqual(flip(f)('aaa')(2), -1)
*
* @since 2.13.0
* @since 2.0.0
*/
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: any[]) => {
return (...args: Array<any>) => {
if (args.length > 1) {
return flipArgs(f as any)(args[0], args[1])
return f(args[1], args[0])
}

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

/**
* Flips the order of the arguments of a function of two arguments.
*
* @example
* import { flipArgs } from 'fp-ts/function'
*
* const f = (a: number, b: string) => a - b.length
*
* assert.strictEqual(flipArgs(f)('aaa', 2), -1)
*
* @since 2.13.0
*/
export function flipArgs<A, B, C>(f: (a: A, b: B) => C): (b: B, a: A) => C {
return (b, a) => f(a, b)
}

/**
* Performs left-to-right function composition. The first argument may have any arity, the remaining arguments must be unary.
*
Expand Down
16 changes: 5 additions & 11 deletions test/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ const g = (n: number) => n * 2

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

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

test('flipArgs', () => {
const f = (a: number, b: string) => a - b.length
U.deepStrictEqual(_.flipArgs(f)('aaa', 2), -1)
U.deepStrictEqual(_.flip(f1)('aaa')(2), -1)
// eslint-disable-next-line deprecation/deprecation
U.deepStrictEqual(_.flip(f2)('aaa', 2), -1)
})

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

0 comments on commit 91113fd

Please sign in to comment.