-
Notifications
You must be signed in to change notification settings - Fork 88
/
05_applicative.ts
66 lines (49 loc) · 1.48 KB
/
05_applicative.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
/*
Models the dice roll of a role-playing game.
*/
import { pipe } from 'fp-ts/function'
import * as IO from 'fp-ts/IO'
import { Monoid } from 'fp-ts/Monoid'
import * as R from 'fp-ts/Random'
// ------------------------------------
// model
// ------------------------------------
export interface Die extends IO.IO<number> {}
// ------------------------------------
// constructors
// ------------------------------------
export const die = (faces: number): Die => R.randomInt(1, faces)
// ------------------------------------
// combinators
// ------------------------------------
export const modifier = (n: number) => (die: Die): Die =>
pipe(
die,
IO.map((m) => m + n)
)
const liftA2 = <A, B, C>(f: (a: A) => (b: B) => C) => (fa: IO.IO<A>) => (
fb: IO.IO<B>
): IO.IO<C> => pipe(fa, IO.map(f), IO.ap(fb))
export const add: (
second: Die
) => (first: Die) => Die = liftA2((a: number) => (b: number) => a + b)
export const multiply = (n: number) => (die: Die): Die =>
pipe(
die,
IO.map((m) => m * n)
)
// ------------------------------------
// instances
// ------------------------------------
export const monoidDie: Monoid<Die> = {
concat: (first, second) => pipe(first, add(second)),
empty: () => 0 // <= un dado con zero facce
}
// ------------------------------------
// tests
// ------------------------------------
const d6 = die(6)
const d8 = die(8)
// 2d6 + 1d8 + 2
const _2d6_1d8_2 = pipe(d6, multiply(2), add(d8), modifier(2))
console.log(_2d6_1d8_2())