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

Backport Option.orElse from effect/ #1868

Merged
merged 1 commit into from May 22, 2023

Conversation

george-wilson-rea
Copy link
Contributor

There is precedent for this naming in Scala

Before submitting a pull request, please make sure the following is done:

  • Fork the repository and create your branch from master.
  • Run npm install in the repository root.
  • If you've fixed a bug or added code that should be tested, add tests!
  • Ensure the test suite passes (npm test).

Note. If you've fixed a bug please link the related issue or, if missing, open an issue before sending a PR.

Note. If you find a typo in the documentation, make sure to modify the corresponding source (docs are generated).

Note. If you want to send a PR related to fp-ts@1.x please create your branch from 1.x

@gcanti
Copy link
Owner

gcanti commented May 19, 2023

Thanks @george-wilson-rea, though I would backport orElse from Effect as a "dual", auto-widening function, see (https://github.com/Effect-TS/data/blob/ba31af7b1a98f804e1a78fa82a2a8d558b119eb6/src/Option.ts#L415)

/cc @sukovanej

So, adapted to fp-ts, something like:

/**
 * 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))

p.s.
"dual" means that you can use the function both data-first and data-last:

pipe(self, orElse(that)) // data-last
orElse(self, that) // data-first

@george-wilson-rea george-wilson-rea changed the title Add Option.orElse and Option.orElseW Backport Option.orElse from effect/ May 22, 2023
@george-wilson-rea
Copy link
Contributor Author

Thanks, I've updated the PR to exactly what you posted there. Hopefully it's good now.

@gcanti gcanti merged commit 63aaccd into gcanti:master May 22, 2023
1 check passed
@gcanti gcanti added this to the 2.16 milestone May 22, 2023
renovate bot added a commit to valora-inc/wallet that referenced this pull request Aug 25, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [fp-ts](https://togithub.com/gcanti/fp-ts) | [`2.13.1` ->
`2.16.1`](https://renovatebot.com/diffs/npm/fp-ts/2.13.1/2.16.1) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/fp-ts/2.16.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/fp-ts/2.16.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/fp-ts/2.13.1/2.16.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/fp-ts/2.13.1/2.16.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>gcanti/fp-ts (fp-ts)</summary>

###
[`v2.16.1`](https://togithub.com/gcanti/fp-ts/blob/HEAD/CHANGELOG.md#2161)

[Compare
Source](https://togithub.com/gcanti/fp-ts/compare/2.16.0...01b8661f2fa594d6f2010573f010d358e6808d13)

**Bug Fix**

- fix type signature of `ReaderTaskEither.tapReaderIO`,
[#&#8203;1895](https://togithub.com/gcanti/fp-ts/issues/1895)

###
[`v2.16.0`](https://togithub.com/gcanti/fp-ts/blob/HEAD/CHANGELOG.md#2160)

[Compare
Source](https://togithub.com/gcanti/fp-ts/compare/2.15.0...2.16.0)

- `tapEither` (dual) (aka `chainFirstEitherK` / `chainFirstEitherKW`)
[gcanti/fp-ts#1864
- `tapIO` (dual) (aka `chainFirstIOK`)
[gcanti/fp-ts#1865
- `as` / `asUnit`
[gcanti/fp-ts#1867
- `tapTask` (dual) (aka `chainFirstTaskK`)
[gcanti/fp-ts#1869
- `mapError` (dual) (aka `mapLeft`)
[gcanti/fp-ts#1870
- `mapBoth` (dual) (aka `bimap`)
[gcanti/fp-ts#1872
- `tapReader` (dual) (aka `chainFirstReaderK` / `chainFirstIReaderKW`)
[gcanti/fp-ts#1871
- `tapReaderEither` (dual) (aka `chainFirstReaderEitherK` /
`chainFirstEitherKW`)
[gcanti/fp-ts#1873
- `tapReaderIO` (dual) (aka `chainFirstReaderIOK`)
[gcanti/fp-ts#1873
- `tapReaderTask` (dual) (aka `chainFirstReaderTaskK`)
[gcanti/fp-ts#1873
- `tapTaskEither` (dual) (aka `chainFirstTaskEitherK` /
`chainFirstTaskEitherKW`)
[gcanti/fp-ts#1873
- `flatMapReaderTask` (dual) (aka `chainReaderTaskK` /
`chainReaderTaskKW`)
[gcanti/fp-ts#1874
- `flatMapTaskEither` (dual) (aka `chainTaskEitherK` /
`chainTaskEitherKW`)
[gcanti/fp-ts#1874
- `flatMapIO` (dual) (aka `chainIOK`)
[gcanti/fp-ts#1874
- `Option.orElse` (dual) (aka `alt` / `altW`)
[gcanti/fp-ts#1868
- `flatMapTask` (dual) (aka `chainTaskK`)
[gcanti/fp-ts#1876
- `flatMapReader` (dual) (aka `chainReaderK` / `chainReaderKW`)
[gcanti/fp-ts#1876
- `flatMapReaderIO` (dual) (aka `chainReaderIOK` / `chainReaderIOKW`)
[gcanti/fp-ts#1879
- `flatMap*` (aka `chain*K` / `chain*KW`)
[gcanti/fp-ts#1880

###
[`v2.15.0`](https://togithub.com/gcanti/fp-ts/blob/HEAD/CHANGELOG.md#2150)

[Compare
Source](https://togithub.com/gcanti/fp-ts/compare/2.14.0...2.15.0)

**New Feature**

-   `function`
    -   add `LazyArg`
-   add `tap` to
    -   `Either`
    -   `IO`
    -   `IOEither`
    -   `IOOption`
    -   `Option`
    -   `Reader`
    -   `ReaderEither`
    -   `ReaderIO`
    -   `ReaderTask`
    -   `ReaderTaskEither`
    -   `State`
    -   `StateReaderTaskEither`
    -   `Task`
    -   `TaskEither`
    -   `TaskOption`
-   add `flatMapNullable` (dual) to
    -   `Either`
    -   `IOEither`
    -   `ReaderEither`
    -   `ReaderTaskEither`
    -   `StateReaderTaskEither`
    -   `TaskEither`
-   add `flatMapOption` (dual) to
    -   `Either`
    -   `IOEither`
    -   `ReaderEither`
    -   `ReaderTaskEither`
    -   `StateReaderTaskEither`
    -   `TaskEither`
-   add `liftNullable` to
    -   `Either`
    -   `IOEither`
    -   `ReaderEither`
    -   `ReaderTaskEither`
    -   `StateReaderTaskEither`
    -   `TaskEither`
-   add `liftOption` to
    -   `Either`
    -   `IOEither`
    -   `ReaderEither`
    -   `ReaderTaskEither`
    -   `StateReaderTaskEither`
    -   `TaskEither`
-   add `flatMapEither` (dual) to
    -   `IOEither`
    -   `ReaderEither`
    -   `ReaderTaskEither`
    -   `TaskEither`
-   `Array`
    -   add index to `flatMap`
-   `NonEmptyArray`
    -   add index to `flatMap`
-   `ReadonlyArray`
    -   add index to `flatMap`
-   `ReadonlyNonEmptyArray`
    -   add index to `flatMap`

###
[`v2.14.0`](https://togithub.com/gcanti/fp-ts/blob/HEAD/CHANGELOG.md#2140)

[Compare
Source](https://togithub.com/gcanti/fp-ts/compare/2.13.2...2.14.0)

**New Feature**

-   add `flatMap` to
    -   `Either`
    -   `IOEither`
    -   `Reader`
    -   `ReaderEither`
    -   `ReaderIO`
    -   `ReaderTask`
    -   `ReaderTaskEither`
    -   `StateReaderTaskEither`
    -   `TaskEither`
    -   `NonEmptyArray`
    -   `ReadonlyNonEmptyArray`
    -   `Tree`
    -   `Array`
    -   `Identity`
    -   `IO`
    -   `IOOption`
    -   `Option`
    -   `ReadonlyArray`
    -   `State`
    -   `Task`
    -   `TaskOption`

###
[`v2.13.2`](https://togithub.com/gcanti/fp-ts/blob/HEAD/CHANGELOG.md#2132)

[Compare
Source](https://togithub.com/gcanti/fp-ts/compare/2.13.1...2.13.2)

- add `chainOptionKW`,
[#&#8203;1846](https://togithub.com/gcanti/fp-ts/issues/1846)
([@&#8203;DenisFrezzato](https://togithub.com/DenisFrezzato))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 5pm,every weekend" in timezone
America/Los_Angeles, Automerge - "after 5pm,every weekend" in timezone
America/Los_Angeles.

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/valora-inc/wallet).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi41Ni4wIiwidXBkYXRlZEluVmVyIjoiMzYuNTYuMCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants