Skip to content

Commit 7324012

Browse files
committed
Examples: fix code style
1 parent 4b5838a commit 7324012

10 files changed

+253
-114
lines changed

examples/ArrayOption.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export const URI = 'ArrayOption'
1515

1616
export type URI = typeof URI
1717

18+
const optionTfold = optionT.fold(array)
19+
1820
export class ArrayOption<A> {
1921
// prettier-ignore
2022
readonly '_A': A
@@ -34,25 +36,38 @@ export class ArrayOption<A> {
3436
return new ArrayOption(optionTArray.chain(a => f(a).value, this.value))
3537
}
3638
fold<R>(r: R, some: (a: A) => R): Array<R> {
37-
return optionT.fold(array)(r, some, this.value)
39+
return optionTfold(r, some, this.value)
3840
}
3941
}
4042

41-
export const map = <A, B>(fa: ArrayOption<A>, f: (a: A) => B): ArrayOption<B> => fa.map(f)
43+
const map = <A, B>(fa: ArrayOption<A>, f: (a: A) => B): ArrayOption<B> => {
44+
return fa.map(f)
45+
}
4246

43-
export const of = <A>(a: A): ArrayOption<A> => new ArrayOption(optionT.some(array)(a))
47+
const optionTsome = optionT.some(array)
48+
const of = <A>(a: A): ArrayOption<A> => {
49+
return new ArrayOption(optionTsome(a))
50+
}
4451

45-
export const ap = <A, B>(fab: ArrayOption<(a: A) => B>, fa: ArrayOption<A>): ArrayOption<B> => fa.ap(fab)
52+
const ap = <A, B>(fab: ArrayOption<(a: A) => B>, fa: ArrayOption<A>): ArrayOption<B> => {
53+
return fa.ap(fab)
54+
}
4655

47-
export const chain = <A, B>(fa: ArrayOption<A>, f: (a: A) => ArrayOption<B>): ArrayOption<B> => fa.chain(f)
56+
const chain = <A, B>(fa: ArrayOption<A>, f: (a: A) => ArrayOption<B>): ArrayOption<B> => {
57+
return fa.chain(f)
58+
}
4859

4960
export const some = of
5061

5162
export const none = new ArrayOption(optionT.none(array)())
5263

53-
export const fromOption = <A>(oa: Option<A>): ArrayOption<A> => new ArrayOption(optionT.fromOption(array)(oa))
64+
const optionTfromOption = optionT.fromOption(array)
65+
export const fromOption = <A>(oa: Option<A>): ArrayOption<A> => {
66+
return new ArrayOption(optionTfromOption(oa))
67+
}
5468

55-
export const liftF = <A>(ma: Array<A>): ArrayOption<A> => new ArrayOption(optionT.liftF(array)(ma))
69+
const optionTliftF = optionT.liftF(array)
70+
export const fromArray = <A>(ma: Array<A>): ArrayOption<A> => new ArrayOption(optionTliftF(ma))
5671

5772
export const arrayOption: Monad1<URI> = {
5873
URI,

examples/EitherOption.ts

+24-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export const URI = 'EitherOption'
1515

1616
export type URI = typeof URI
1717

18+
const optionTfold = optionT.fold(either.either)
19+
1820
export class EitherOption<L, A> {
1921
// prettier-ignore
2022
readonly '_A': A
@@ -36,28 +38,40 @@ export class EitherOption<L, A> {
3638
return new EitherOption(optionTEither.chain(a => f(a).value, this.value))
3739
}
3840
fold<R>(r: R, some: (a: A) => R): either.Either<L, R> {
39-
return optionT.fold(either.either)(r, some, this.value)
41+
return optionTfold(r, some, this.value)
4042
}
4143
}
4244

43-
export const map = <L, A, B>(fa: EitherOption<L, A>, f: (a: A) => B): EitherOption<L, B> => fa.map(f)
45+
const map = <L, A, B>(fa: EitherOption<L, A>, f: (a: A) => B): EitherOption<L, B> => {
46+
return fa.map(f)
47+
}
4448

45-
export const of = <L, A>(a: A): EitherOption<L, A> => new EitherOption(optionT.some(either.either)(a))
49+
const optionTsome = optionT.some(either.either)
50+
const of = <L, A>(a: A): EitherOption<L, A> => {
51+
return new EitherOption(optionTsome(a))
52+
}
4653

47-
export const ap = <L, A, B>(fab: EitherOption<L, (a: A) => B>, fa: EitherOption<L, A>): EitherOption<L, B> => fa.ap(fab)
54+
const ap = <L, A, B>(fab: EitherOption<L, (a: A) => B>, fa: EitherOption<L, A>): EitherOption<L, B> => {
55+
return fa.ap(fab)
56+
}
4857

49-
export const chain = <L, A, B>(fa: EitherOption<L, A>, f: (a: A) => EitherOption<L, B>): EitherOption<L, B> =>
50-
fa.chain(f)
58+
const chain = <L, A, B>(fa: EitherOption<L, A>, f: (a: A) => EitherOption<L, B>): EitherOption<L, B> => {
59+
return fa.chain(f)
60+
}
5161

5262
export const some = of
5363

5464
export const none = new EitherOption(optionT.none(either.either)())
5565

56-
export const fromOption = <L, A>(oa: Option<A>): EitherOption<L, A> =>
57-
new EitherOption(optionT.fromOption(either.either)(oa))
66+
const optionTfromOption = optionT.fromOption(either.either)
67+
export const fromOption = <L, A>(oa: Option<A>): EitherOption<L, A> => {
68+
return new EitherOption(optionTfromOption(oa))
69+
}
5870

59-
export const liftF = <L, A>(ma: either.Either<L, A>): EitherOption<L, A> =>
60-
new EitherOption(optionT.liftF(either.either)(ma))
71+
const optionTliftF = optionT.liftF(either.either)
72+
export const fromEither = <L, A>(ma: either.Either<L, A>): EitherOption<L, A> => {
73+
return new EitherOption(optionTliftF(ma))
74+
}
6175

6276
export const eitherOption: Monad2<URI> = {
6377
URI,

examples/Moore.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,29 @@ export class Moore<L, A> {
5959
}
6060
}
6161

62-
export const map = <L, A, B>(fa: Moore<L, A>, f: (a: A) => B): Moore<L, B> => fa.map(f)
62+
const map = <L, A, B>(fa: Moore<L, A>, f: (a: A) => B): Moore<L, B> => {
63+
return fa.map(f)
64+
}
6365

64-
export const ap = <L, A, B>(fab: Moore<L, (a: A) => B>, fa: Moore<L, A>): Moore<L, B> => fa.ap(fab)
66+
const ap = <L, A, B>(fab: Moore<L, (a: A) => B>, fa: Moore<L, A>): Moore<L, B> => {
67+
return fa.ap(fab)
68+
}
6569

66-
export const chain = <L, A, B>(fa: Moore<L, A>, f: (a: A) => Moore<L, B>): Moore<L, B> => fa.chain(f)
70+
const chain = <L, A, B>(fa: Moore<L, A>, f: (a: A) => Moore<L, B>): Moore<L, B> => {
71+
return fa.chain(f)
72+
}
6773

68-
export const extract = <L, A>(fa: Moore<L, A>): A => fa.extract()
74+
const extract = <L, A>(fa: Moore<L, A>): A => {
75+
return fa.extract()
76+
}
6977

70-
export const extend = <L, A, B>(fa: Moore<L, A>, f: (fa: Moore<L, A>) => B): Moore<L, B> => fa.extend(f)
78+
const extend = <L, A, B>(fa: Moore<L, A>, f: (fa: Moore<L, A>) => B): Moore<L, B> => {
79+
return fa.extend(f)
80+
}
7181

72-
export const promap = <A, B, C, D>(fla: Moore<B, C>, f: (a: A) => B, g: (c: C) => D): Moore<A, D> => fla.promap(f, g)
82+
const promap = <A, B, C, D>(fla: Moore<B, C>, f: (a: A) => B, g: (c: C) => D): Moore<A, D> => {
83+
return fla.promap(f, g)
84+
}
7385

7486
/** Construct a Moore machine from a state valuation and transition function */
7587
export const unfoldMoore = <S, L, A>(f: (s: S) => [A, (l: L) => S]) => (s: S): Moore<L, A> => {

examples/ReaderEither.ts

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as readerT from '../src/ReaderT'
2-
import * as either from '../src/Either'
2+
import { Either, either } from '../src/Either'
33
import { Monad3 } from '../src/Monad'
44

5-
const readerTEither = readerT.getReaderT(either.either)
5+
const readerTEither = readerT.getReaderT(either)
66

77
declare module '../src/HKT' {
88
interface URI2HKT3<U, L, A> {
@@ -23,7 +23,7 @@ export class ReaderEither<E, L, A> {
2323
readonly '_U': E
2424
// prettier-ignore
2525
readonly '_URI': URI
26-
constructor(readonly run: (e: E) => either.Either<L, A>) {}
26+
constructor(readonly run: (e: E) => Either<L, A>) {}
2727
map<B>(f: (a: A) => B): ReaderEither<E, L, B> {
2828
return new ReaderEither(readerTEither.map(f, this.run))
2929
}
@@ -41,46 +41,42 @@ export class ReaderEither<E, L, A> {
4141
}
4242
}
4343

44-
export const map = <E, L, A, B>(fa: ReaderEither<E, L, A>, f: (a: A) => B): ReaderEither<E, L, B> => {
44+
const map = <E, L, A, B>(fa: ReaderEither<E, L, A>, f: (a: A) => B): ReaderEither<E, L, B> => {
4545
return fa.map(f)
4646
}
4747

48-
export const of = <E, L, A>(a: A): ReaderEither<E, L, A> => {
48+
const of = <E, L, A>(a: A): ReaderEither<E, L, A> => {
4949
return new ReaderEither(readerTEither.of(a))
5050
}
5151

52-
export const ap = <E, L, A, B>(
53-
fab: ReaderEither<E, L, (a: A) => B>,
54-
fa: ReaderEither<E, L, A>
55-
): ReaderEither<E, L, B> => {
52+
const ap = <E, L, A, B>(fab: ReaderEither<E, L, (a: A) => B>, fa: ReaderEither<E, L, A>): ReaderEither<E, L, B> => {
5653
return fa.ap(fab)
5754
}
5855

59-
export const chain = <E, L, A, B>(
60-
fa: ReaderEither<E, L, A>,
61-
f: (a: A) => ReaderEither<E, L, B>
62-
): ReaderEither<E, L, B> => {
56+
const chain = <E, L, A, B>(fa: ReaderEither<E, L, A>, f: (a: A) => ReaderEither<E, L, B>): ReaderEither<E, L, B> => {
6357
return fa.chain(f)
6458
}
6559

66-
export const ask = <E, L>(e: E): ReaderEither<E, L, E> => {
67-
return new ReaderEither(readerT.ask(either.either)())
60+
const readerTask = readerT.ask(either)
61+
export const ask = <E, L>(): ReaderEither<E, L, E> => {
62+
return new ReaderEither(readerTask())
6863
}
6964

65+
const readerTasks = readerT.asks(either)
7066
export const asks = <E, L, A>(f: (e: E) => A): ReaderEither<E, L, A> => {
71-
return new ReaderEither(readerT.asks(either.either)(f))
67+
return new ReaderEither(readerTasks(f))
7268
}
7369

7470
export const local = <E>(f: (e: E) => E) => <L, A>(fa: ReaderEither<E, L, A>): ReaderEither<E, L, A> => {
7571
return new ReaderEither(e => fa.run(f(e)))
7672
}
7773

78-
export const alt = <E, L, A>(fx: ReaderEither<E, L, A>, fy: ReaderEither<E, L, A>): ReaderEither<E, L, A> => {
79-
return new ReaderEither(e => fx.run(e).alt(fy.run(e)))
74+
export const fromEither = <E, L, A>(fa: Either<L, A>): ReaderEither<E, L, A> => {
75+
return new ReaderEither(() => fa)
8076
}
8177

82-
export const fromEither = <E, L, A>(fa: either.Either<L, A>): ReaderEither<E, L, A> => {
83-
return new ReaderEither(() => fa)
78+
export const alt = <E, L, A>(fx: ReaderEither<E, L, A>, fy: ReaderEither<E, L, A>): ReaderEither<E, L, A> => {
79+
return new ReaderEither(e => fx.run(e).alt(fy.run(e)))
8480
}
8581

8682
export const readerEither: Monad3<URI> = {

examples/ReaderIO.ts

+27-7
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,39 @@ export class ReaderIO<E, A> {
3939
}
4040
}
4141

42-
export const map = <E, A, B>(fa: ReaderIO<E, A>, f: (a: A) => B): ReaderIO<E, B> => fa.map(f)
42+
const map = <E, A, B>(fa: ReaderIO<E, A>, f: (a: A) => B): ReaderIO<E, B> => {
43+
return fa.map(f)
44+
}
4345

44-
export const of = <E, A>(a: A): ReaderIO<E, A> => new ReaderIO(readerTIO.of(a))
46+
const of = <E, A>(a: A): ReaderIO<E, A> => {
47+
return new ReaderIO(readerTIO.of(a))
48+
}
4549

46-
export const ap = <E, A, B>(fab: ReaderIO<E, (a: A) => B>, fa: ReaderIO<E, A>): ReaderIO<E, B> => fa.ap(fab)
50+
const ap = <E, A, B>(fab: ReaderIO<E, (a: A) => B>, fa: ReaderIO<E, A>): ReaderIO<E, B> => {
51+
return fa.ap(fab)
52+
}
4753

48-
export const chain = <E, A, B>(fa: ReaderIO<E, A>, f: (a: A) => ReaderIO<E, B>): ReaderIO<E, B> => fa.chain(f)
54+
const chain = <E, A, B>(fa: ReaderIO<E, A>, f: (a: A) => ReaderIO<E, B>): ReaderIO<E, B> => {
55+
return fa.chain(f)
56+
}
4957

50-
export const ask = <E>(e: E): ReaderIO<E, E> => new ReaderIO(readerT.ask(io)())
58+
const readerTask = readerT.ask(io)
59+
export const ask = <E>(): ReaderIO<E, E> => {
60+
return new ReaderIO(readerTask())
61+
}
5162

52-
export const asks = <E, A>(f: (e: E) => A): ReaderIO<E, A> => new ReaderIO(readerT.asks(io)(f))
63+
const readerTasks = readerT.asks(io)
64+
export const asks = <E, A>(f: (e: E) => A): ReaderIO<E, A> => {
65+
return new ReaderIO(readerTasks(f))
66+
}
67+
68+
export const local = <E>(f: (e: E) => E) => <A>(fa: ReaderIO<E, A>): ReaderIO<E, A> => {
69+
return new ReaderIO(e => fa.run(f(e)))
70+
}
5371

54-
export const local = <E>(f: (e: E) => E) => <A>(fa: ReaderIO<E, A>): ReaderIO<E, A> => new ReaderIO(e => fa.run(f(e)))
72+
export const fromIO = <E, A>(fa: IO<A>): ReaderIO<E, A> => {
73+
return new ReaderIO(() => fa)
74+
}
5575

5676
export const readerIO: Monad2<URI> = {
5777
URI,

examples/ReaderTaskEither.ts

+44-18
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,64 @@ export class ReaderTaskEither<E, L, A> {
4444
}
4545
}
4646

47-
export const map = <E, L, A, B>(fa: ReaderTaskEither<E, L, A>, f: (a: A) => B): ReaderTaskEither<E, L, B> => fa.map(f)
47+
const map = <E, L, A, B>(fa: ReaderTaskEither<E, L, A>, f: (a: A) => B): ReaderTaskEither<E, L, B> => {
48+
return fa.map(f)
49+
}
4850

49-
export const of = <E, L, A>(a: A): ReaderTaskEither<E, L, A> => new ReaderTaskEither(readerTTaskEither.of(a))
51+
const of = <E, L, A>(a: A): ReaderTaskEither<E, L, A> => {
52+
return new ReaderTaskEither(readerTTaskEither.of(a))
53+
}
5054

51-
export const ap = <E, L, A, B>(
55+
const ap = <E, L, A, B>(
5256
fab: ReaderTaskEither<E, L, (a: A) => B>,
5357
fa: ReaderTaskEither<E, L, A>
54-
): ReaderTaskEither<E, L, B> => fa.ap(fab)
58+
): ReaderTaskEither<E, L, B> => {
59+
return fa.ap(fab)
60+
}
5561

56-
export const chain = <E, L, A, B>(
62+
const chain = <E, L, A, B>(
5763
fa: ReaderTaskEither<E, L, A>,
5864
f: (a: A) => ReaderTaskEither<E, L, B>
59-
): ReaderTaskEither<E, L, B> => fa.chain(f)
65+
): ReaderTaskEither<E, L, B> => {
66+
return fa.chain(f)
67+
}
6068

61-
export const ask = <E, L>(e: E): ReaderTaskEither<E, L, E> => new ReaderTaskEither(readerT.ask(taskEither.taskEither)())
69+
const readerTask = readerT.ask(taskEither.taskEither)
70+
export const ask = <E, L>(e: E): ReaderTaskEither<E, L, E> => {
71+
return new ReaderTaskEither(readerTask())
72+
}
6273

63-
export const asks = <E, L, A>(f: (e: E) => A): ReaderTaskEither<E, L, A> =>
64-
new ReaderTaskEither(readerT.asks(taskEither.taskEither)(f))
74+
const readerTasks = readerT.asks(taskEither.taskEither)
75+
export const asks = <E, L, A>(f: (e: E) => A): ReaderTaskEither<E, L, A> => {
76+
return new ReaderTaskEither(readerTasks(f))
77+
}
6578

66-
export const local = <E>(f: (e: E) => E) => <L, A>(fa: ReaderTaskEither<E, L, A>): ReaderTaskEither<E, L, A> =>
67-
new ReaderTaskEither(e => fa.run(f(e)))
79+
export const local = <E>(f: (e: E) => E) => <L, A>(fa: ReaderTaskEither<E, L, A>): ReaderTaskEither<E, L, A> => {
80+
return new ReaderTaskEither(e => fa.run(f(e)))
81+
}
6882

69-
export const right = <E, L, A>(fa: Task<A>): ReaderTaskEither<E, L, A> =>
70-
new ReaderTaskEither(() => taskEither.right(fa))
83+
export const right = <E, L, A>(fa: Task<A>): ReaderTaskEither<E, L, A> => {
84+
return new ReaderTaskEither(() => taskEither.right(fa))
85+
}
86+
87+
export const left = <E, L, A>(fa: Task<L>): ReaderTaskEither<E, L, A> => {
88+
return new ReaderTaskEither(() => taskEither.left(fa))
89+
}
7190

72-
export const left = <E, L, A>(fa: Task<L>): ReaderTaskEither<E, L, A> => new ReaderTaskEither(() => taskEither.left(fa))
91+
export const fromTaskEither = <E, L, A>(fa: TaskEither<L, A>): ReaderTaskEither<E, L, A> => {
92+
return new ReaderTaskEither(() => fa)
93+
}
7394

74-
export const fromEither = <E, L, A>(fa: Either<L, A>): ReaderTaskEither<E, L, A> =>
75-
new ReaderTaskEither(() => taskEither.fromEither(fa))
95+
export const fromEither = <E, L, A>(fa: Either<L, A>): ReaderTaskEither<E, L, A> => {
96+
return new ReaderTaskEither(() => taskEither.fromEither(fa))
97+
}
7698

77-
export const tryCatch = <E, L, A>(f: (e: E) => Promise<A>, onrejected: (reason: {}) => L): ReaderTaskEither<E, L, A> =>
78-
new ReaderTaskEither(e => taskEither.tryCatch(() => f(e), onrejected))
99+
export const tryCatch = <E, L, A>(
100+
f: (e: E) => Promise<A>,
101+
onrejected: (reason: {}) => L
102+
): ReaderTaskEither<E, L, A> => {
103+
return new ReaderTaskEither(e => taskEither.tryCatch(() => f(e), onrejected))
104+
}
79105

80106
export const readerTaskEither: Monad3<URI> = {
81107
URI,

0 commit comments

Comments
 (0)