import type { Reducer , Transducer } from "./api.js" ; import { __iter , iterator } from "./iterator.js" ; import { ensureReduced , isReduced , unreduced } from "./reduced.js" ; remarks remarks https example param rfn param init param src export function scan < A , B > ( rfn , init ? ) ; export function scan < A , B > ( rfn , init , src ) ; export function scan < A , B > ( ... args ) { return ( ( args . length > 2 && __iter ( scan , args , iterator ) ) || ( ( [ inito , completeo , reduceo ] ) => { const [ initi , completei , reducei ] = args [ 0 ] ; let acc = args . length > 1 && args [ 1 ] != null ? args [ 1 ] : initi ( ) ; return < Reducer < B , A > > [ inito , ( _acc ) => { let a = completei ( acc ) ; if ( a !== acc ) { _acc = unreduced ( reduceo ( _acc , a ) ) ; } acc = a ; return completeo ( _acc ) ; } , ( _acc , x ) => { acc = < any > reducei ( acc , x ) ; if ( isReduced ( acc ) ) { return ensureReduced ( reduceo ( _acc , ( < any > acc ) . deref ( ) ) ) ; } return reduceo ( _acc , acc ) ; } , ] ; } ) ) ; }