import { type Fn, type PFn, type PipeShunt, type PromiseChange, type CollPipeShunt, type PipeShuntReturn, type PromisePipeShunt, type PromiseCollPipeShunt, type PromisePipeShuntReturn, type AutoPipeShuntReturn, type AutoPipeShunt, type AutoCollPipeShunt } from '../../mod.js'; interface AutoPipe { /** ## pipe : 函数嵌套参数化运行; [point free](https://wiki.haskell.org/Pointfree)在Ts中的实现 @example Usage ```ts //Synchronization function const res = pipe( 1,//1 (x: number) => x + 1,//2 (x: number) => x * 2,//4 (x: number) => x + 1,//5 ) assertEquals(res, 5) //Asynchronous function const res2 = await pipe( 's', (x: string) => x + 'a', async (x: string) => x + 'b', )) assertEquals(res2, 'sab') ``` @tips 1. 最好有三个及以上的函数调用才能体现出pipe的优势 2. 能自动解包Promise作为参数传递运行的情况 : + 能否当成异步函数运行主要取决于{@link is_async_func}这个函数 + 传入的函数中有一个是带async关键字的函数 <只需含有一个即可辨别> + 可await运行的flow/lzpipe @category Function */ (a: A): A; (a: A, b: PFn): AutoPipeShuntReturn; (a: A, b: PFn, c: PFn, C>): AutoPipeShuntReturn; (a: A, b: PFn, c: PFn, C>, d: PFn, D>): AutoPipeShuntReturn, D>; (a: A, b: PFn, c: PFn, C>, d: PFn, D>, e: PFn, E>): AutoPipeShuntReturn, D>, E>; (a: A, b: PFn, c: PFn, C>, d: PFn, D>, e: PFn, E>, f: PFn, F>): AutoPipeShuntReturn, D>, E>, F>; (a: A, b: PFn, c: PFn, C>, d: PFn, D>, e: PFn, E>, f: PFn, F>, g: PFn, G>): AutoPipeShuntReturn, D>, E>, F>, G>; (a: A, b: PFn, c: PFn, C>, d: PFn, D>, e: PFn, E>, f: PFn, F>, g: PFn, G>, h: PFn, H>): AutoPipeShuntReturn, D>, E>, F>, G>, H>; (a: A, b: PFn, c: PFn, C>, d: PFn, D>, e: PFn, E>, f: PFn, F>, g: PFn, G>, h: PFn, H>, i: PFn, I>): AutoPipeShuntReturn, D>, E>, F>, G>, H>, I>; (...fns: Array>): unknown; } interface SyncPipe { /** ## pipe.sync : 不会自动解包Promise的pipe @example ```ts const origin = 0 const first = (x: number) => x + 1 const second = (x: number) => x * 2 const third = (x: number) => Promise.resolve(x + 11) const res = pipe.sync(origin, first, second) assertEquals(res, 2) pipe .sync(origin, third, async (r) => { return (await r) + 1 }) .then((res) => { assertEquals(res, 12) }) ``` @category Function */ (a: A): A; (a: A, ab: Fn): PipeShuntReturn; (a: A, ab: Fn, bc: Fn, C>): PipeShuntReturn; (a: A, ab: Fn, bc: Fn, C>, cd: Fn, D>): PipeShuntReturn, D>; (a: A, ab: Fn, bc: Fn, C>, cd: Fn, D>, de: Fn, E>): PipeShuntReturn, D>, E>; (a: A, ab: Fn, bc: Fn, C>, cd: Fn, D>, de: Fn, E>, ef: Fn, F>): PipeShuntReturn, D>, E>, F>; (a: A, ab: Fn, bc: Fn, C>, cd: Fn, D>, de: Fn, E>, ef: Fn, F>, fg: Fn, G>): PipeShuntReturn, D>, E>, F>, G>; (a: A, ab: Fn, cd: Fn, C>, de: Fn, D>, ef: Fn, E>, fg: Fn, F>, gh: Fn, H>): PipeShuntReturn, D>, E>, F>, G>, H>; (a: A, ab: Fn, bc: Fn, C>, cd: Fn, D>, de: Fn, E>, ef: Fn, F>, fg: Fn, G>, gh: Fn, H>, hi: Fn, I>): PipeShuntReturn, D>, E>, F>, G>, H>, I>; (...fns: Array>): unknown; } interface AsyncPipe { /** ## pipe.async : 会自动解包Promise的pipe @example ```ts const origin = 0 const first = (x: number) => x + 1 const second = (x: number) => x * 2 const third = (x: number) => Promise.resolve(x + 11) const res = await pipe.async(origin, first, second) assertEquals(res, 2) const res2 = await pipe.async(origin, third, first) assertEquals(res2, 12) ``` @category Function */ (a: A): PromiseChange; (a: A, ab: PFn): PromisePipeShuntReturn; (a: A, ab: PFn, bc: PFn, C>): PromisePipeShuntReturn; (a: A, ab: PFn, bc: PFn, C>, cd: PFn, D>): PromisePipeShuntReturn, D>; (a: A, ab: PFn, bc: PFn, C>, cd: PFn, D>, de: PFn, E>): PromisePipeShuntReturn, D>, E>; (a: A, ab: PFn, bc: PFn, C>, cd: PFn, D>, de: PFn, E>, ef: PFn, F>): PromisePipeShuntReturn, D>, E>, F>; (a: A, ab: PFn, bc: PFn, C>, cd: PFn, D>, de: PFn, E>, ef: PFn, F>, fg: PFn, G>): PromisePipeShuntReturn, D>, E>, F>, G>; (a: A, ab: PFn, bc: PFn, C>, cd: PFn, D>, de: PFn, E>, ef: PFn, F>, fg: PFn, G>, gh: PFn, H>): PromisePipeShuntReturn, D>, E>, F>, G>, H>; (a: A, ab: PFn, bc: PFn, C>, cd: PFn, D>, de: PFn, E>, ef: PFn, F>, fg: PFn, G>, gh: PFn, H>, hi: PFn, I>): PromisePipeShuntReturn, D>, E>, F>, G>, H>, I>; (...fns: Array>): Promise; } interface Pipe extends AutoPipe { readonly sync: SyncPipe; readonly async: AsyncPipe; } /** ## pipe : 函数嵌套参数化运行; [point free](https://wiki.haskell.org/Pointfree)在Ts中的实现 @example basic ```ts //Synchronization function const res = pipe( 1,//1 (x: number) => x + 1,//2 (x: number) => x * 2,//4 (x: number) => x + 1,//5 ) assertEquals(res, 5) //Asynchronous function const res2 = await pipe( 's', (x: string) => x + 'a', async (x: string) => x + 'b', )) assertEquals(res2, 'sab') ``` @example pipe配合 Shunt 数据结构,做到 '截流' 操作 ```ts // 一个io操作:可能存在异常情况 const io_operate = (path: string): Result => { if (path === 'mod.ts') return Ok(path) return IllegalOperatError.err('your path is not exist') } // 截流操作 : 如果数据返回Err就直接返回,否则就进行解包然后进行下一步操作 const closure = ( result: Result ): Shunt> => { if (result.is_ok) return Mainstream(result.unwrap()) return Reflux(result as Err) } // 读取到地址的后续操作 const io_then = (path: string) => { assert(path === 'mod.ts') return Ok(true) } // 'mos' --io_operate--> Result --closure--> Shunt> -> if Mainstream -> io_then | if Reflux -> return const resf: Result = pipe('mos', io_operate, closure, io_then) assert((resf as Err).unwrap_err().instance_of(IllegalOperatError)) const rest: Result = pipe('mos', io_operate, closure, io_then) assert(rest) ``` @example 非传入纯函数时,注意绑定this指向 ```ts const arr = [1, 2, 4, 5, 6] const arr_p = pipe((v: number) => v * 2, arr.map.bind(arr)) assert(equal(arr_p, [2, 4, 8, 10, 12])) ``` @tips 1. 最好有三个及以上的函数调用才能体现出pipe的优势 2. 能自动解包Promise作为参数传递运行的情况 : + 能否当成异步函数运行主要取决于{@link is_async_func}这个函数 + 传入的函数中有一个是带async关键字的函数 <只需含有一个即可辨别> + 可await运行的flow/lzpipe 3. 对于非纯函数,要注意绑定this指向 @category Function */ export declare const pipe: Pipe; export {}; //# sourceMappingURL=pipe.d.ts.map