Skip to content

Commit

Permalink
Function: flip apply
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 7, 2023
1 parent b3e7ff3 commit 2bb91d1
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 124 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-bats-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fp-ts/core": minor
---

Function: flip apply
10 changes: 5 additions & 5 deletions docs/modules/Either.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1205,8 +1205,8 @@ const onLeft = (errors: ReadonlyArray<string>): string => `Errors: ${errors.join

const onRight = (value: number): string => `Ok: ${value}`

assert.strictEqual(pipe(E.right(1), E.match(onLeft, onRight)), 'Ok: 1')
assert.strictEqual(pipe(E.left(['error 1', 'error 2']), E.match(onLeft, onRight)), 'Errors: error 1, error 2')
assert.deepStrictEqual(pipe(E.right(1), E.match(onLeft, onRight)), 'Ok: 1')
assert.deepStrictEqual(pipe(E.left(['error 1', 'error 2']), E.match(onLeft, onRight)), 'Errors: error 1, error 2')
```

Added in v1.0.0
Expand Down Expand Up @@ -1404,9 +1404,9 @@ import * as E from '@fp-ts/core/Either'

const f = E.exists((n: number) => n > 2)

assert.strictEqual(f(E.left('a')), false)
assert.strictEqual(f(E.right(1)), false)
assert.strictEqual(f(E.right(3)), true)
assert.deepStrictEqual(f(E.left('a')), false)
assert.deepStrictEqual(f(E.right(1)), false)
assert.deepStrictEqual(f(E.right(3)), true)
```

Added in v1.0.0
Expand Down
23 changes: 17 additions & 6 deletions docs/modules/Function.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,21 @@ Added in v1.0.0
## apply
Apply a function to a given value.
**Signature**
```ts
export declare const apply: <A>(a: A) => <B>(self: (a: A) => B) => B
export declare const apply: <A, B>(self: (a: A) => B) => (a: A) => B
```
**Example**
```ts
import { pipe, apply } from '@fp-ts/core/Function'
import { increment } from '@fp-ts/core/Number'

assert.deepStrictEqual(pipe(2, apply(increment)), 3)
```

Added in v1.0.0
Expand Down Expand Up @@ -302,7 +313,7 @@ import { flip } from '@fp-ts/core/Function'

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

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

Added in v1.0.0
Expand Down Expand Up @@ -387,7 +398,7 @@ const double = (n: number): number => n * 2
const f = flow(len, double)
assert.strictEqual(f('aaa'), 6)
assert.deepStrictEqual(f('aaa'), 6)
```

Added in v1.0.0
Expand Down Expand Up @@ -662,10 +673,10 @@ const len = (s: string): number => s.length
const double = (n: number): number => n * 2
// without pipe
assert.strictEqual(double(len('aaa')), 6)
assert.deepStrictEqual(double(len('aaa')), 6)
// with pipe
assert.strictEqual(pipe('aaa', len, double), 6)
assert.deepStrictEqual(pipe('aaa', len, double), 6)
```

Added in v1.0.0
Expand All @@ -687,7 +698,7 @@ import { tupled } from '@fp-ts/core/Function'
const add = tupled((x: number, y: number): number => x + y)
assert.strictEqual(add([1, 2]), 3)
assert.deepStrictEqual(add([1, 2]), 3)
```

Added in v1.0.0
Expand Down
48 changes: 24 additions & 24 deletions docs/modules/Option.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,14 @@ export declare const getOrElse: <B>(onNone: any) => <A>(self: Option<A>) => B |
import { some, none, getOrElse } from '@fp-ts/core/Option'
import { pipe } from '@fp-ts/core/Function'

assert.strictEqual(
assert.deepStrictEqual(
pipe(
some(1),
getOrElse(() => 0)
),
1
)
assert.strictEqual(
assert.deepStrictEqual(
pipe(
none(),
getOrElse(() => 0)
Expand Down Expand Up @@ -635,8 +635,8 @@ export declare const isNone: <A>(self: Option<A>) => self is None
```ts
import { some, none, isNone } from '@fp-ts/core/Option'

assert.strictEqual(isNone(some(1)), false)
assert.strictEqual(isNone(none()), true)
assert.deepStrictEqual(isNone(some(1)), false)
assert.deepStrictEqual(isNone(none()), true)
```

Added in v1.0.0
Expand All @@ -656,9 +656,9 @@ export declare const isOption: (input: unknown) => input is Option<unknown>
```ts
import { some, none, isOption } from '@fp-ts/core/Option'

assert.strictEqual(isOption(some(1)), true)
assert.strictEqual(isOption(none()), true)
assert.strictEqual(isOption({}), false)
assert.deepStrictEqual(isOption(some(1)), true)
assert.deepStrictEqual(isOption(none()), true)
assert.deepStrictEqual(isOption({}), false)
```

Added in v1.0.0
Expand All @@ -678,8 +678,8 @@ export declare const isSome: <A>(self: Option<A>) => self is Some<A>
```ts
import { some, none, isSome } from '@fp-ts/core/Option'

assert.strictEqual(isSome(some(1)), true)
assert.strictEqual(isSome(none()), false)
assert.deepStrictEqual(isSome(some(1)), true)
assert.deepStrictEqual(isSome(none()), false)
```

Added in v1.0.0
Expand Down Expand Up @@ -970,8 +970,8 @@ export declare const getOrNull: <A>(self: Option<A>) => A | null
import { some, none, getOrNull } from '@fp-ts/core/Option'
import { pipe } from '@fp-ts/core/Function'

assert.strictEqual(pipe(some(1), getOrNull), 1)
assert.strictEqual(pipe(none(), getOrNull), null)
assert.deepStrictEqual(pipe(some(1), getOrNull), 1)
assert.deepStrictEqual(pipe(none(), getOrNull), null)
```

Added in v1.0.0
Expand Down Expand Up @@ -1014,8 +1014,8 @@ export declare const getOrUndefined: <A>(self: Option<A>) => A | undefined
import { some, none, getOrUndefined } from '@fp-ts/core/Option'
import { pipe } from '@fp-ts/core/Function'

assert.strictEqual(pipe(some(1), getOrUndefined), 1)
assert.strictEqual(pipe(none(), getOrUndefined), undefined)
assert.deepStrictEqual(pipe(some(1), getOrUndefined), 1)
assert.deepStrictEqual(pipe(none(), getOrUndefined), undefined)
```

Added in v1.0.0
Expand Down Expand Up @@ -1246,7 +1246,7 @@ export declare const match: <B, A, C = B>(onNone: any, onSome: (a: A) => C) => (
import { some, none, match } from '@fp-ts/core/Option'
import { pipe } from '@fp-ts/core/Function'

assert.strictEqual(
assert.deepStrictEqual(
pipe(
some(1),
match(
Expand All @@ -1257,7 +1257,7 @@ assert.strictEqual(
'a some containing 1'
)

assert.strictEqual(
assert.deepStrictEqual(
pipe(
none(),
match(
Expand Down Expand Up @@ -1402,11 +1402,11 @@ import * as N from '@fp-ts/core/Number'
import { pipe } from '@fp-ts/core/Function'

const O = liftOrder(N.Order)
assert.strictEqual(O.compare(none(), none()), 0)
assert.strictEqual(O.compare(none(), some(1)), -1)
assert.strictEqual(O.compare(some(1), none()), 1)
assert.strictEqual(O.compare(some(1), some(2)), -1)
assert.strictEqual(O.compare(some(1), some(1)), 0)
assert.deepStrictEqual(O.compare(none(), none()), 0)
assert.deepStrictEqual(O.compare(none(), some(1)), -1)
assert.deepStrictEqual(O.compare(some(1), none()), 1)
assert.deepStrictEqual(O.compare(some(1), some(2)), -1)
assert.deepStrictEqual(O.compare(some(1), some(1)), 0)
```

Added in v1.0.0
Expand Down Expand Up @@ -1543,21 +1543,21 @@ export declare const exists: <A>(predicate: any) => (self: Option<A>) => boolean
import { some, none, exists } from '@fp-ts/core/Option'
import { pipe } from '@fp-ts/core/Function'

assert.strictEqual(
assert.deepStrictEqual(
pipe(
some(1),
exists((n) => n > 0)
),
true
)
assert.strictEqual(
assert.deepStrictEqual(
pipe(
some(1),
exists((n) => n > 1)
),
false
)
assert.strictEqual(
assert.deepStrictEqual(
pipe(
none(),
exists((n) => n > 0)
Expand Down Expand Up @@ -1615,7 +1615,7 @@ import { some, none, reduceAll } from '@fp-ts/core/Option'
import { pipe } from '@fp-ts/core/Function'

const iterable = [some(1), none(), some(2), none()]
assert.strictEqual(
assert.deepStrictEqual(
pipe(
iterable,
reduceAll(0, (b, a) => b + a)
Expand Down
10 changes: 5 additions & 5 deletions src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,14 +664,14 @@ export const Foldable: foldable.Foldable<EitherTypeLambda> = {
*
* const onRight = (value: number): string => `Ok: ${value}`
*
* assert.strictEqual(
* assert.deepStrictEqual(
* pipe(
* E.right(1),
* E.match(onLeft , onRight)
* ),
* 'Ok: 1'
* )
* assert.strictEqual(
* assert.deepStrictEqual(
* pipe(
* E.left(['error 1', 'error 2']),
* E.match(onLeft , onRight)
Expand Down Expand Up @@ -1080,9 +1080,9 @@ export const contains = <A>(equivalence: Equivalence<A>) =>
*
* const f = E.exists((n: number) => n > 2)
*
* assert.strictEqual(f(E.left('a')), false)
* assert.strictEqual(f(E.right(1)), false)
* assert.strictEqual(f(E.right(3)), true)
* assert.deepStrictEqual(f(E.left('a')), false)
* assert.deepStrictEqual(f(E.right(1)), false)
* assert.deepStrictEqual(f(E.right(3)), true)
*
* @since 1.0.0
*/
Expand Down
20 changes: 14 additions & 6 deletions src/Function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,17 @@ export const getMonoid = <M>(Monoid: monoid.Monoid<M>) =>
}

/**
* Apply a function to a given value.
*
* @example
* import { pipe, apply } from '@fp-ts/core/Function'
* import { increment } from '@fp-ts/core/Number'
*
* assert.deepStrictEqual(pipe(2, apply(increment)), 3)
*
* @since 1.0.0
*/
export const apply = <A>(a: A) => <B>(self: (a: A) => B): B => self(a)
export const apply = <A, B>(self: (a: A) => B) => (a: A): B => self(a)

/**
* A lazy argument
Expand Down Expand Up @@ -166,7 +174,7 @@ export const constVoid: LazyArg<void> = constUndefined
*
* const f = (a: number) => (b: string) => a - b.length
*
* assert.strictEqual(flip(f)('aaa')(2), -1)
* assert.deepStrictEqual(flip(f)('aaa')(2), -1)
*
* @since 1.0.0
*/
Expand All @@ -185,7 +193,7 @@ export const flip = <A extends Array<unknown>, B extends Array<unknown>, C>(
*
* const f = flow(len, double)
*
* assert.strictEqual(f('aaa'), 6)
* assert.deepStrictEqual(f('aaa'), 6)
*
* @see {@link pipe}
* @since 1.0.0
Expand Down Expand Up @@ -316,7 +324,7 @@ export const absurd = <A>(_: never): A => {
*
* const add = tupled((x: number, y: number): number => x + y)
*
* assert.strictEqual(add([1, 2]), 3)
* assert.deepStrictEqual(add([1, 2]), 3)
*
* @since 1.0.0
*/
Expand All @@ -341,10 +349,10 @@ export const untupled = <A extends ReadonlyArray<unknown>, B>(f: (a: A) => B): (
* const double = (n: number): number => n * 2
*
* // without pipe
* assert.strictEqual(double(len('aaa')), 6)
* assert.deepStrictEqual(double(len('aaa')), 6)
*
* // with pipe
* assert.strictEqual(pipe('aaa', len, double), 6)
* assert.deepStrictEqual(pipe('aaa', len, double), 6)
*
* @see {@link flow}
* @since 1.0.0
Expand Down
Loading

0 comments on commit 2bb91d1

Please sign in to comment.