Skip to content

Commit

Permalink
chore: add aliases for orElse
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed May 22, 2023
1 parent 9ba16df commit 56b0a62
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 144 deletions.
143 changes: 69 additions & 74 deletions docs/modules/Option.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ Added in v2.0.0
- [guard](#guard)
- [let](#let)
- [error handling](#error-handling)
- [alt](#alt)
- [altW](#altw)
- [getOrElse](#getorelse)
- [getOrElseW](#getorelsew)
- [orElse](#orelse)
- [filtering](#filtering)
- [compact](#compact)
- [filter](#filter)
Expand Down Expand Up @@ -136,6 +135,8 @@ Added in v2.0.0
- [tryCatch](#trycatch)
- [tryCatchK](#trycatchk)
- [legacy](#legacy)
- [alt](#alt)
- [altW](#altw)
- [chain](#chain)
- [chainFirst](#chainfirst)
- [chainFirstEitherK](#chainfirsteitherk)
Expand Down Expand Up @@ -485,78 +486,6 @@ Added in v2.13.0

# error handling

## alt

Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
types of kind `* -> *`.

In case of `Option` returns the left-most non-`None` value.

| x | y | pipe(x, alt(() => y) |
| ------- | ------- | -------------------- |
| none | none | none |
| some(a) | none | some(a) |
| none | some(b) | some(b) |
| some(a) | some(b) | some(a) |

**Signature**

```ts
export declare const alt: <A>(that: LazyArg<Option<A>>) => (fa: Option<A>) => Option<A>
```

**Example**

```ts
import * as O from 'fp-ts/Option'
import { pipe } from 'fp-ts/function'

assert.deepStrictEqual(
pipe(
O.none,
O.alt(() => O.none)
),
O.none
)
assert.deepStrictEqual(
pipe(
O.some('a'),
O.alt<string>(() => O.none)
),
O.some('a')
)
assert.deepStrictEqual(
pipe(
O.none,
O.alt(() => O.some('b'))
),
O.some('b')
)
assert.deepStrictEqual(
pipe(
O.some('a'),
O.alt(() => O.some('b'))
),
O.some('a')
)
```

Added in v2.0.0

## altW

Less strict version of [`alt`](#alt).

The `W` suffix (short for **W**idening) means that the return types will be merged.

**Signature**

```ts
export declare const altW: <B>(that: LazyArg<Option<B>>) => <A>(fa: Option<A>) => Option<B | A>
```

Added in v2.9.0

## getOrElse

Extracts the value out of the structure, if it exists. Otherwise returns the given default value
Expand Down Expand Up @@ -605,6 +534,44 @@ export declare const getOrElseW: <B>(onNone: LazyArg<B>) => <A>(ma: Option<A>) =

Added in v2.6.0

## orElse

Returns the provided `Option` `that` if `self` is `None`, otherwise returns `self`.

**Signature**

```ts
export declare const orElse: {
<B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<B | A>
<A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B>
}
```

**Example**

```ts
import * as O from 'fp-ts/Option'

assert.deepStrictEqual(
O.orElse(O.none, () => O.none),
O.none
)
assert.deepStrictEqual(
O.orElse(O.some(1), () => O.none),
O.some(1)
)
assert.deepStrictEqual(
O.orElse(O.none, () => O.some('b')),
O.some('b')
)
assert.deepStrictEqual(
O.orElse(O.some(1), () => O.some('b')),
O.some(1)
)
```

Added in v2.16.0

# filtering

## compact
Expand Down Expand Up @@ -1046,6 +1013,34 @@ Added in v2.10.0

# legacy

## alt

Alias of `orElse`.

**Signature**

```ts
export declare const alt: <A>(that: LazyArg<Option<A>>) => (fa: Option<A>) => Option<A>
```

Added in v2.0.0

## altW

Alias of `orElse`.

Less strict version of [`alt`](#alt).

The `W` suffix (short for **W**idening) means that the return types will be merged.

**Signature**

```ts
export declare const altW: <B>(that: LazyArg<Option<B>>) => <A>(fa: Option<A>) => Option<B | A>
```

Added in v2.9.0

## chain

Alias of `flatMap`.
Expand Down
98 changes: 28 additions & 70 deletions src/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,66 +471,46 @@ export const Foldable: Foldable1<URI> = {
}

/**
* Less strict version of [`alt`](#alt).
* Returns the provided `Option` `that` if `self` is `None`, otherwise returns `self`.
*
* The `W` suffix (short for **W**idening) means that the return types will be merged.
* @param self - The first `Option` to be checked.
* @param that - The `Option` to return if `self` is `None`.
*
* @example
* import * as O from "fp-ts/Option"
*
* assert.deepStrictEqual(O.orElse(O.none, () => O.none), O.none)
* assert.deepStrictEqual(O.orElse(O.some(1), () => O.none), O.some(1))
* assert.deepStrictEqual(O.orElse(O.none, () => O.some('b')), O.some('b'))
* assert.deepStrictEqual(O.orElse(O.some(1), () => O.some('b')), O.some(1))
*
* @category error handling
* @since 2.9.0
* @since 2.16.0
*/
export const altW: <B>(that: LazyArg<Option<B>>) => <A>(fa: Option<A>) => Option<A | B> = (that) => (fa) =>
isNone(fa) ? that() : fa
export const orElse: {
<B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<A | B>
<A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B>
} = dual(2, <A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B> => (isNone(self) ? that() : self))

/**
* Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
* types of kind `* -> *`.
*
* In case of `Option` returns the left-most non-`None` value.
* Alias of `orElse`.
*
* | x | y | pipe(x, alt(() => y) |
* | ------- | ------- | -------------------- |
* | none | none | none |
* | some(a) | none | some(a) |
* | none | some(b) | some(b) |
* | some(a) | some(b) | some(a) |
* Less strict version of [`alt`](#alt).
*
* @example
* import * as O from 'fp-ts/Option'
* import { pipe } from 'fp-ts/function'
* The `W` suffix (short for **W**idening) means that the return types will be merged.
*
* assert.deepStrictEqual(
* pipe(
* O.none,
* O.alt(() => O.none)
* ),
* O.none
* )
* assert.deepStrictEqual(
* pipe(
* O.some('a'),
* O.alt<string>(() => O.none)
* ),
* O.some('a')
* )
* assert.deepStrictEqual(
* pipe(
* O.none,
* O.alt(() => O.some('b'))
* ),
* O.some('b')
* )
* assert.deepStrictEqual(
* pipe(
* O.some('a'),
* O.alt(() => O.some('b'))
* ),
* O.some('a')
* )
* @category legacy
* @since 2.9.0
*/
export const altW: <B>(that: LazyArg<Option<B>>) => <A>(fa: Option<A>) => Option<A | B> = orElse

/**
* Alias of `orElse`.
*
* @category error handling
* @category legacy
* @since 2.0.0
*/
export const alt: <A>(that: LazyArg<Option<A>>) => (fa: Option<A>) => Option<A> = altW
export const alt: <A>(that: LazyArg<Option<A>>) => (fa: Option<A>) => Option<A> = orElse

/**
* @category instances
Expand Down Expand Up @@ -920,28 +900,6 @@ export const getOrElseW =
*/
export const getOrElse: <A>(onNone: LazyArg<A>) => (ma: Option<A>) => A = getOrElseW

/**
* Returns the provided `Option` `that` if `self` is `None`, otherwise returns `self`.
*
* @param self - The first `Option` to be checked.
* @param that - The `Option` to return if `self` is `None`.
*
* @example
* import * as O from "fp-ts/Option"
*
* assert.deepStrictEqual(O.orElse(O.none, () => O.none), O.none)
* assert.deepStrictEqual(O.orElse(O.some(1), () => O.none), O.some(1))
* assert.deepStrictEqual(O.orElse(O.none, () => O.some('b')), O.some('b'))
* assert.deepStrictEqual(O.orElse(O.some(1), () => O.some('b')), O.some(1))
*
* @category error handling
* @since 2.16.0
*/
export const orElse: {
<B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<A | B>
<A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B>
} = dual(2, <A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B> => (isNone(self) ? that() : self))

/**
* @category mapping
* @since 2.10.0
Expand Down

0 comments on commit 56b0a62

Please sign in to comment.