DAY 27
0
Software Development

## Introduction

concept

``````State a s
``````

State Monad 的 constructor 也跟 Reader Monad一樣，都是放入函式，比較值得注意的是，State 裡面包含了 `s``a`, `s` 表示為狀態, `a` 則是結果

``````const State = run => ({
run,
})

State.of = a => State(s => [a, s])
``````

Functor

``````const State = run => ({
run,
map: f => State(a => {
const [y, s] = run(a);
return [f(y), s]
})
})
``````

``````const seed = 1;

const randomInRange = (min, max) => s =>
State(generator).map(value).map(normalize(min, max)).run(s);

randomInRange(0, 1)(seed)

// [5, 1103527590]
``````

``````const nameList = ["jing", "jing*5", "jing-tech"];

const randomName = (from) => (seed) =>
randomInRange(0, from.length)
.map((index) => from[index])
.run(seed);

const [name, r1] = randomName(nameList)(1); // ["jing*5", 1103527590]
``````

Chain

``````const State = (run) => ({
run,
map: (f) =>
State((a) => {
const [y, s] = run(a);
return [f(y), s];
}),
chain: f =>
State(x => {
const [y, s] = run(x);
return f(y).run(s)
}),
});
``````

``````const randomInRange = (min, max) =>
State(generator).map(value).map(normalize(min, max));

const randomFrom = (from) =>
randomInRange(0, from.length).map((index) => from[index]);

const keepImprove = randomFrom(['Study', 'Coding', "Writing"]);
const lazy = randomFrom(['Couch Potato', 'Netflix']);
``````
``````const seed = 1;
const result = randomInRange(0, 1)
.map((n) => n === 1)
.chain((bool) => (bool ? keepImprove : lazy))
.run(seed); // ["Couch Potato", 377401600]
``````

## 小結

OKAY，result 竟然是慫恿筆者當 Couch Potato，那就不客氣了！決定把剩下的留到下一章寫，感謝大家閱讀!!!