Module

x/functional/mod.ts>chainRec

Common Functional Programming Algebraic data types for JavaScript that is compatible with most modern browsers and Deno.
Latest
variable chainRec
import { chainRec } from "https://deno.land/x/functional@v1.3.4/mod.ts";

chainRec

ChainRec r => ((a -> c, b -> c, a) -> r c) -> a -> r b

This function is a combinator for the chainRec algebra. It takes a ternary function, an initial value and, a chainable recursive functor.

import Task from "https://deno.land/x/functional@v1.3.2/library/Task.js";
import { chainRec } from "https://deno.land/x/functional@v1.3.2/library/utilities.js";

const multiplyAll = curry((x, n) => chainRec(
  (Loop, Done, cursor) =>
    cursor === n ? Done(Pair(cursor, null)) : Loop(Pair(cursor + 1, Task.of([ x * (cursor + 1) ]))),
  0
));

const container = await multiplyAll(42, 10)(Task.of([ 0 ])).run();

const value = safeExtract("Failed.", container);

assertEquals(value, [ 0, 42, 84, 126, 168, 210, 252, 294, 336, 378, 420 ]);