Skip to content

Commit

Permalink
Deprecate the existing behaviour of flip in favour of flipArgs and re…
Browse files Browse the repository at this point in the history
…use the flip name instead of flipC
  • Loading branch information
thewilkybarkid committed Sep 1, 2022
1 parent e2d385e commit 20268fc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
29 changes: 20 additions & 9 deletions docs/modules/function.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Added in v2.0.0
- [constant](#constant)
- [decrement](#decrement)
- [flip](#flip)
- [flipC](#flipc)
- [flipArgs](#flipargs)
- [flow](#flow)
- [hole](#hole)
- [identity](#identity)
Expand Down Expand Up @@ -307,34 +307,45 @@ 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
```

Added in v2.0.0
**Example**

## flipC
```ts
import { flip } from 'fp-ts/function'
Flips the arguments of a curried function.
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 flipC<A, B, C>(f: (a: A) => (b: B) => C): (b: B) => (a: A) => C
export declare function flipArgs<A, B, C>(f: (a: A, b: B) => C): (b: B, a: A) => C
```

**Example**

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

Added in v2.13.0
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'

//
// flip
// flipArgs
//

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

//
// tuple
Expand Down
36 changes: 26 additions & 10 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,28 +194,44 @@ 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.
*
* @since 2.0.0
* @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.13.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: any[]) => {
if (args.length > 1) {
return flipArgs(f as any)(args[0], args[1])
}

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

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

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

describe('function', () => {
it('flip', () => {
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)
})

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

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

0 comments on commit 20268fc

Please sign in to comment.