import type { Fn } from "@thi.ng/api" ; import type { Reducer , Transducer } from "./api.js" ; import { compR } from "./compr.js" ; import { __iter } from "./iterator.js" ; /**
 * Transducer. Similar to  {@link (map:1)} , but only transforms every
 * `n`-th input value and passes intermediate values unchanged
 * downstream.
 *
 *  remarks example param n param fn export function mapNth < A , B > ( n , fn ) ; export function mapNth < A , B > ( n , offset , fn ) ; export function mapNth < A , B > ( n , fn , src ) ; export function mapNth < A , B > ( n , offset , fn , src ) ; export function mapNth < A , B > ( ... args ) { const iter = __iter ( mapNth , args ) ; if ( iter ) { return iter ; } let n = args [ 0 ] - 1 ; let offset ; let fn ; if ( typeof args [ 1 ] === "number" ) { offset = args [ 1 ] ; fn = args [ 2 ] ; } else { fn = args [ 1 ] ; offset = 0 ; } return ( rfn ) => { const r = rfn [ 2 ] ; let skip = 0 , off = offset ; return compR ( rfn , ( acc , x ) => { if ( off === 0 ) { if ( skip === 0 ) { skip = n ; return r ( acc , fn ( x ) ) ; } skip -- ; } else { off -- ; } return r ( acc , x ) ; } ) ; } ; }