Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code from the fantas-eel post on ChainRec.
const daggy = require('daggy')
const { Loop, Done } =
daggy.taggedSum({ Loop: ['b']
, Done: ['a'] })
Array.empty = () => []
const Pair = T => {
const Pair_ = daggy.tagged('_1', '_2')
Pair_.prototype.map = function (f) {
return Pair_(this._1, f(this._2))
}
Pair_.prototype.ap = function (fs) {
return Pair_(fs._1.concat(this._1),
fs._2(this._2))
}
Pair_.of = x => Pair_(T.empty(), x)
Pair_.prototype.chain = function (f) {
const that = f(this._2)
return Pair_(this._1.concat(that._1),
that._2)
}
Pair_.chainRec = function (f, init) {
let acc = T.empty()
, step = Loop(init)
do {
const result = f(step.b)
acc = acc.concat(result._1)
step = result._2
} while (step instanceof Loop)
return Pair_(acc, step)
}
return Pair_
}
const Writer = Pair(Array)
const seqRec = upper => Writer.chainRec(
init => Writer([init],
init >= upper ? Done(null)
: Loop(init + 1)),
0)
const seq = upper => {
const seq_ = init =>
init >= upper ? Writer([init], null)
: Writer([init], init + 1)
.chain(seq_)
return seq_(0)
}
// console.log(seq(10000)._1) // Will fail
console.log(seqRec(10000)._1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.