forked from gcanti/fp-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.ts
31 lines (21 loc) · 969 Bytes
/
14.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
//
// Code for http://www.tomharding.me/2017/05/30/fantas-eel-and-specification-14/
//
import { getArrayMonoid } from '../src/Monoid'
import { Tuple, getMonad, getChainRec } from '../src/Tuple'
const monoidArrayNumber = getArrayMonoid<number>()
const monad = getMonad(monoidArrayNumber)
export const seq = (upper: number): Tuple<Array<number>, number> => {
const seq_ = (init: number): Tuple<Array<number>, number> =>
init >= upper ? new Tuple([init], upper) : monad.chain(new Tuple([init], init + 1), seq_)
return seq_(1)
}
console.log(seq(5).fst)
// => [ 1, 2, 3, 4, 5 ]
// console.log(seq(10000).fst()) // => Maximum call stack size exceeded
import { left, right } from '../src/Either'
const { chainRec } = getChainRec(monoidArrayNumber)
const seqReq = (upper: number): Tuple<Array<number>, number> =>
chainRec(1, init => new Tuple([init], init >= upper ? right(init) : left(init + 1)))
console.log(seqReq(10000).fst)
// => [1, 2, 3, ..., 10000]