forked from gcanti/fp-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskValidation.ts
57 lines (47 loc) · 1.68 KB
/
TaskValidation.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
import { Applicative2C, getApplicativeComposition } from '../src/Applicative'
import { Functor2, getFunctorComposition } from '../src/Functor'
import { Semigroup } from '../src/Semigroup'
import * as task from '../src/Task'
import * as validation from '../src/Validation'
import { phantom } from '../src/function'
declare module '../src/HKT' {
interface URI2HKT2<L, A> {
TaskValidation: TaskValidation<L, A>
}
}
const taskValidationFunctor = getFunctorComposition(task.task, validation.validation)
export const URI = 'TaskValidation'
export type URI = typeof URI
export class TaskValidation<L, A> {
readonly _A!: A
readonly _L!: L
readonly _URI!: URI
constructor(readonly value: task.Task<validation.Validation<L, A>>) {}
map<B>(f: (a: A) => B): TaskValidation<L, B> {
return new TaskValidation(taskValidationFunctor.map(this.value, f))
}
fold<R>(failure: (l: L) => R, success: (a: A) => R): task.Task<R> {
return this.value.map(v => v.fold(failure, success))
}
}
const map = <L, A, B>(fa: TaskValidation<L, A>, f: (a: A) => B): TaskValidation<L, B> => {
return fa.map(f)
}
export const getApplicative = <L>(S: Semigroup<L>): Applicative2C<URI, L> => {
const taskValidationApplicative = getApplicativeComposition(task.task, validation.getApplicative(S))
const of = <A>(a: A): TaskValidation<L, A> => new TaskValidation(taskValidationApplicative.of(a))
const ap = <A, B>(fab: TaskValidation<L, (a: A) => B>, fa: TaskValidation<L, A>): TaskValidation<L, B> => {
return new TaskValidation(taskValidationApplicative.ap(fab.value, fa.value))
}
return {
URI,
_L: phantom,
map,
of,
ap
}
}
export const taskValidation: Functor2<URI> = {
URI,
map
}