forked from gcanti/fp-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugging-with-Trace.ts
More file actions
47 lines (37 loc) · 877 Bytes
/
debugging-with-Trace.ts
File metadata and controls
47 lines (37 loc) · 877 Bytes
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
import { left } from 'fp-ts/lib/Either'
import { head } from 'fp-ts/lib/Array'
import { Option, option, some } from 'fp-ts/lib/Option'
import { spy, trace, traceA, traceM } from 'fp-ts/lib/Trace'
//
// spy
//
const foo = left<string, number>('foo')
const bar = spy(foo.mapLeft(s => s.length))
console.log(bar)
// => left(3)
//
// trace
//
const bar2 = foo.mapLeft(s => trace('mapping the left side', () => s.length))
console.log(bar2)
// => 'mapping the left side'
//
// traceA
//
const traceAOption = traceA(option)
traceAOption('start computation')
.chain(() => some(1))
.chain(() => traceAOption('end computation'))
// => start computation
// => end computation
//
// traceM
//
const traceMOption = traceM(option)
const baz: Option<number> = some([1, 2, 3])
.chain(traceMOption)
.chain(head)
.chain(traceMOption)
console.log(baz)
// => [1, 2, 3]
// => 1