// @flow import type { Chain } from './Chain' import type { Either } from './Either' import { HKT } from './HKT' import * as either from './Either' export interface ChainRec extends Chain { chainRec(f: (a: A) => HKT>, a: A): HKT; } export function tailRec(f: (a: A) => Either, a: A): B { let v = f(a) while (either.isLeft(v)) { v = f(either.fromLeft(v)) } return either.fromRight(v) }