Implementing a String Augmentation Function using FP Techniques
I'm happy with this
dodo/whilewhile syntax, but I'm curious if there's a more FP-way to implement this as a reducereduce/traversetraverse and a schedule:// If successful, will return an augmented copy of the input string
// If the returned value equals the input, that means there is no more information to extract out of the input
declare const addMore: (input: string) => T.Effect<
MyContext,
MyError,
string
>;
const keepAddingUntilNothingNew = (
initial: string,
maxIterations?: number,
): T.Effect<
MyContext,
MyError,
string
> =>
T.gen(function* ($) {
let previous;
let latest = initial;
let iterationNum = 0;
do {
iterationNum++;
previous = latest;
latest = yield* $(addMore(latest));
} while (
// Continue as long as the iteration gave us new results
!EQ.equals(previous, latest)
// Continue forever if no max iterations is provided,
// or continue as long as we haven't hit that maximum
&& (P.isNullable(maxIterations) || iterationNum < maxIterations)
);
return latest;
});// If successful, will return an augmented copy of the input string
// If the returned value equals the input, that means there is no more information to extract out of the input
declare const addMore: (input: string) => T.Effect<
MyContext,
MyError,
string
>;
const keepAddingUntilNothingNew = (
initial: string,
maxIterations?: number,
): T.Effect<
MyContext,
MyError,
string
> =>
T.gen(function* ($) {
let previous;
let latest = initial;
let iterationNum = 0;
do {
iterationNum++;
previous = latest;
latest = yield* $(addMore(latest));
} while (
// Continue as long as the iteration gave us new results
!EQ.equals(previous, latest)
// Continue forever if no max iterations is provided,
// or continue as long as we haven't hit that maximum
&& (P.isNullable(maxIterations) || iterationNum < maxIterations)
);
return latest;
});