import type { Fn } from "@thi.ng/api" ; import type { Reducer } from "./api.js" ; import { frequencies } from "./frequencies.js" ; import { $$reduce } from "./reduce.js" ; /**
 * Similar to  normFrequencies , but automatically computes normalization
 * basis instead of requiring it ahead of time as argument.
 *
 *  example export function normFrequenciesAuto < A > ( ) ; export function normFrequenciesAuto < A > ( xs ) ; export function normFrequenciesAuto < A , B > ( key ) ; export function normFrequenciesAuto < A , B > ( key , xs ) ; export function normFrequenciesAuto ( ... args ) { const res = $$reduce ( normFrequenciesAuto , args ) ; if ( res !== undefined ) { return res ; } const [ init , complete , reduce ] = frequencies ( ... ( < [ ] > args ) ) ; let norm = 0 ; return [ init , ( acc ) => { acc = complete ( acc ) ; for ( let p of acc ) { acc . set ( p [ 0 ] , p [ 1 ] / norm ) ; } return acc ; } , ( acc , x ) => ( norm ++ , reduce ( acc , x ) ) , ] ; }