Skip to content

Commit

Permalink
Simplify a pipe
Browse files Browse the repository at this point in the history
Refs #388
  • Loading branch information
thewilkybarkid committed Oct 24, 2022
1 parent 8ed88c4 commit 22baca6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/write-review/write-review-change-author.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { format } from 'fp-ts-routing'
import * as E from 'fp-ts/Either'
import { fromOption as fromOption_ } from 'fp-ts/FromEither'
import * as O from 'fp-ts/Option'
import { Reader } from 'fp-ts/Reader'
import * as RA from 'fp-ts/ReadonlyArray'
import { constUndefined, flow, pipe } from 'fp-ts/function'
import { Lazy, constUndefined, flow, pipe } from 'fp-ts/function'
import { Status, StatusOpen } from 'hyper-ts'
import * as RM from 'hyper-ts/lib/ReaderMiddleware'
import * as D from 'io-ts/Decoder'
Expand Down Expand Up @@ -39,11 +40,11 @@ export const writeReviewChangeAuthor = (doi: PreprintId['doi'], index: number) =
),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW('author', ({ form }) =>
RM.fromEither(
fromOption(() => 'not-found')(
pipe(
form.otherAuthors ?? [],
E.fromOptionK(() => 'not-found')(RA.lookup(index)),
E.let('index', () => index),
O.fromNullable(form.otherAuthors),
O.chain(RA.lookup(index)),
O.let('index', () => index),
),
),
),
Expand Down Expand Up @@ -282,3 +283,7 @@ function fromReaderK<R, A extends ReadonlyArray<unknown>, B, I = StatusOpen, E =
): (...a: A) => RM.ReaderMiddleware<R, I, I, E, B> {
return (...a) => RM.rightReader(f(...a))
}

// https://github.com/DenisFrezzato/hyper-ts/pull/89
const fromOption: <E>(onNone: Lazy<E>) => <R, I, A>(ma: O.Option<A>) => RM.ReaderMiddleware<R, I, I, E, A> =
fromOption_(RM.FromEither)
15 changes: 10 additions & 5 deletions src/write-review/write-review-remove-author.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { format } from 'fp-ts-routing'
import * as E from 'fp-ts/Either'
import { fromOption as fromOption_ } from 'fp-ts/FromEither'
import * as O from 'fp-ts/Option'
import { Reader } from 'fp-ts/Reader'
import * as RA from 'fp-ts/ReadonlyArray'
import { flow, pipe } from 'fp-ts/function'
import { Lazy, flow, pipe } from 'fp-ts/function'
import { Status, StatusOpen } from 'hyper-ts'
import * as M from 'hyper-ts/lib/Middleware'
import * as RM from 'hyper-ts/lib/ReaderMiddleware'
Expand Down Expand Up @@ -44,11 +45,11 @@ export const writeReviewRemoveAuthor = (doi: PreprintId['doi'], index: number) =
),
RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))),
RM.bindW('author', ({ form }) =>
RM.fromEither(
fromOption(() => 'not-found')(
pipe(
form.otherAuthors ?? [],
E.fromOptionK(() => 'not-found')(RA.lookup(index)),
E.let('index', () => index),
O.fromNullable(form.otherAuthors),
O.chain(RA.lookup(index)),
O.let('index', () => index),
),
),
),
Expand Down Expand Up @@ -271,3 +272,7 @@ function fromReaderK<R, A extends ReadonlyArray<unknown>, B, I = StatusOpen, E =
): (...a: A) => RM.ReaderMiddleware<R, I, I, E, B> {
return (...a) => RM.rightReader(f(...a))
}

// https://github.com/DenisFrezzato/hyper-ts/pull/89
const fromOption: <E>(onNone: Lazy<E>) => <R, I, A>(ma: O.Option<A>) => RM.ReaderMiddleware<R, I, I, E, A> =
fromOption_(RM.FromEither)

0 comments on commit 22baca6

Please sign in to comment.