import type { Predicate2 } from "@thi.ng/api" ; import { SEMAPHORE } from "@thi.ng/api/api" ; import type { Reducer , Transducer } from "./api.js" ; import { compR } from "./compr.js" ; import { __iter } from "./iterator.js" ; import { ensureReduced } from "./reduced.js" ; /**
 * Transducer which for each input `x` (apart from the very first one)
 * applies given predicate `pred` to previous input and `x`. Only passes
 * values downstream as long as the predicate returns a falsy result.
 * Once the result is truthy, `x` is considered converged and the
 * transformation is terminated (by emitting a  reduced value).
 *
 *  remarks This can be used to limit processing of inputs only as long as
 * there're noticeable changes (according to the predicate) and then
 * stop the transducer pipeline once results have converged.
 *
 * See also:  {@link (takeWhile:1)} example param pred param src export function converge < T > ( pred ? ) ; export function converge < T > ( pred , src ) ; export function converge < T > ( ... args ) { return ( __iter ( converge , args ) || ( ( rfn ) => { const r = rfn [ 2 ] ; const pred = args [ 0 ] ; let prev = SEMAPHORE ; let done = false ; return compR ( rfn , ( acc , x ) => { if ( done || ( prev !== SEMAPHORE && pred ( prev , x ) ) ) { done = true ; return ensureReduced ( r ( acc , x ) ) ; } prev = x ; return r ( acc , x ) ; } ) ; } ) ) ; }