forked from gcanti/fp-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskOption.ts
91 lines (74 loc) · 2.49 KB
/
TaskOption.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { Monad1 } from '../src/Monad'
import { Option, fromEither } from '../src/Option'
import * as optionT from '../src/OptionT'
import { Task, task, tryCatch as tryCatchTask } from '../src/Task'
import { Lazy } from '../src/function'
declare module '../src/HKT' {
interface URI2HKT<A> {
TaskOption: TaskOption<A>
}
}
const optionTTask = optionT.getOptionT(task)
export const URI = 'TaskOption'
export type URI = typeof URI
const optionTfold = optionT.fold(task)
export class TaskOption<A> {
readonly _A!: A
readonly _URI!: URI
constructor(readonly value: Task<Option<A>>) {}
run(): Promise<Option<A>> {
return this.value.run()
}
map<B>(f: (a: A) => B): TaskOption<B> {
return new TaskOption(optionTTask.map(this.value, f))
}
ap<B>(fab: TaskOption<(a: A) => B>): TaskOption<B> {
return new TaskOption(optionTTask.ap<A, B>(fab.value, this.value))
}
ap_<B, C>(this: TaskOption<(b: B) => C>, fb: TaskOption<B>): TaskOption<C> {
return fb.ap(this)
}
chain<B>(f: (a: A) => TaskOption<B>): TaskOption<B> {
return new TaskOption(optionTTask.chain(a => f(a).value, this.value))
}
fold<R>(r: R, some: (a: A) => R): Task<R> {
return optionTfold(r, some, this.value)
}
orElse(f: () => TaskOption<A>): TaskOption<A> {
return new TaskOption(this.value.chain(e => e.fold(f().value, a => optionTTask.of(a))))
}
}
const map = <A, B>(fa: TaskOption<A>, f: (a: A) => B): TaskOption<B> => {
return fa.map(f)
}
const optionTsome = optionT.some(task)
const of = <A>(a: A): TaskOption<A> => new TaskOption(optionTsome(a))
const ap = <A, B>(fab: TaskOption<(a: A) => B>, fa: TaskOption<A>): TaskOption<B> => {
return fa.ap(fab)
}
const chain = <A, B>(fa: TaskOption<A>, f: (a: A) => TaskOption<B>): TaskOption<B> => {
return fa.chain(f)
}
export const some = of
export const none = new TaskOption(optionT.none(task)())
const optionTfromOption = optionT.fromOption(task)
export const fromOption = <A>(oa: Option<A>): TaskOption<A> => {
return new TaskOption(optionTfromOption(oa))
}
const optionTliftF = optionT.liftF(task)
export const fromTask = <A>(ma: Task<A>): TaskOption<A> => {
return new TaskOption(optionTliftF(ma))
}
export const orElse = <A>(f: () => TaskOption<A>) => (fa: TaskOption<A>): TaskOption<A> => {
return fa.orElse(f)
}
export const tryCatch = <A>(f: Lazy<Promise<A>>): TaskOption<A> => {
return new TaskOption(tryCatchTask(f, () => undefined).map(e => fromEither(e)))
}
export const taskOption: Monad1<URI> = {
URI,
map,
of,
ap,
chain
}