Skip to content

Commit

Permalink
builtin (fun): fix declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliy-art committed Nov 28, 2024
1 parent ff092c0 commit cff223d
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 82 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tarantoolscript",
"version": "0.30.7",
"version": "0.30.8",
"author": "Vitaliy Artemov [email protected]",
"description": "TypeScript definitions for Tarantool Lua API.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/fun/Basic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export declare function each(
* @see {@link https://luafun.github.io/basic.html#fun.each}
*/
export declare function each<TParam, TState, TReturn extends unknown[]>(
fun: (this: void, ...args: [...TReturn]) => unknown,
fun: (this: void, ...args: TReturn) => unknown,
...iterParams: [...IterParams<TParam, TState, TReturn>]
): void;

Expand Down
69 changes: 53 additions & 16 deletions src/builtin/fun/Filtering.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,22 @@ import { FunIterator } from "./FunIterator";
* @returns An iterator.
* @see {@link https://luafun.github.io/filtering.html#fun.filter}
*/
export declare function filter<T>(predicate: (this: void, element: T) => boolean, value: T[]): FunIterator<number, [T]>;
export declare function filter<T>(
predicate: (this: void, element: T) => boolean,
value: T[]
): FunIterator<number, [T]>;

/**
* Return a new iterator of those elements that satisfy the `predicate`.
* @param predicate An predicate to filter the iterator.
* @param value An iterator.
* @returns An iterator.
* @see {@link https://luafun.github.io/filtering.html#fun.filter}
*/
export declare function filter<TState, TReturn extends unknown[]>(
predicate: (this: void, ...args: TReturn) => boolean,
value: FunIterator<TState, TReturn>
): FunIterator<TState, TReturn>;

/**
* Return a new iterator of those elements that satisfy the `predicate`.
Expand All @@ -20,7 +35,7 @@ export declare function filter<T>(predicate: (this: void, element: T) => boolean
*/
export declare function filter(
predicate: (this: void, key: string, value: any) => boolean,
value: AnyTable,
value: AnyTable
): FunIterator<string, [string, unknown]>;

/**
Expand All @@ -32,7 +47,7 @@ export declare function filter(
*/
export declare function filter(
predicate: (this: void, element: string) => boolean,
value: string,
value: string
): FuncIterator<number, [string]>;

/**
Expand All @@ -47,9 +62,9 @@ export declare function filter(
* @returns An iterator.
* @see {@link https://luafun.github.io/filtering.html#fun.filter}
*/
export declare function filter<TParam, TState, TReturn = unknown[]>(
predicate: (this: void, ...params: [...TReturn]) => boolean,
...iterParams: [...IterParams<TParam, TState, TReturn>],
export declare function filter<TParam, TState, TReturn extends unknown[]>(
predicate: (this: void, ...params: TReturn) => boolean,
...iterParams: [...IterParams<TParam, TState, TReturn>]
): FunIterator<TState, TReturn>;

export declare const remove_if: typeof filter;
Expand All @@ -61,9 +76,12 @@ export declare const remove_if: typeof filter;
* @returns An iterator.
* @see {@link https://luafun.github.io/filtering.html#fun.grep}
*/
declare function grepStrings(regexp: string, value: string[]): FunIterator<number, [string]>
declare function grepStrings(
regexp: string,
value: string[]
): FunIterator<number, [string]>;

export declare const grep: typeof filter & typeof grepStrings
export declare const grep: typeof filter & typeof grepStrings;

/**
* Return two iterators where elements do and do not satisfy the `predicate`.
Expand All @@ -74,9 +92,21 @@ export declare const grep: typeof filter & typeof grepStrings
*/
export declare function partition<T>(
predicate: (this: void, element: T) => boolean,
value: T[],
value: T[]
): LuaMultiReturn<[FunIterator<number, [T]>, FunIterator<number, [T]>]>;

/**
* Return two iterators where elements do and do not satisfy the `predicate`.
* @param predicate Function to filter the iterator.
* @param value An iterator.
* @returns An iterator pair.
* @see {@link https://luafun.github.io/filtering.html#fun.partition}
*/
export declare function partition<TState, TReturn extends unknown[]>(
predicate: (this: void, ...args: TReturn) => boolean,
value: FunIterator<TState, TReturn>
): LuaMultiReturn<[FunIterator<TState, TReturn>, FunIterator<TState, TReturn>]>;

/**
* Return two iterators where elements do and do not satisfy the `predicate`.
* @param predicate Function to filter the map.
Expand All @@ -86,8 +116,13 @@ export declare function partition<T>(
*/
export declare function partition(
predicate: (this: void, key: string, value: any) => boolean,
value: AnyTable,
): LuaMultiReturn<[FunIterator<string, [string, unknown]>, FunIterator<string, [string, unknown]>]>;
value: AnyTable
): LuaMultiReturn<
[
FunIterator<string, [string, unknown]>,
FunIterator<string, [string, unknown]>
]
>;

/**
* Return two iterators where elements do and do not satisfy the `predicate`.
Expand All @@ -98,8 +133,10 @@ export declare function partition(
*/
export declare function partition(
predicate: (this: void, element: string) => boolean,
value: string,
): LuaMultiReturn<[FuncIterator<number, [string]>, FuncIterator<number, [string]>]>;
value: string
): LuaMultiReturn<
[FuncIterator<number, [string]>, FuncIterator<number, [string]>]
>;

/**
* Return two iterators where elements do and do not satisfy the `predicate`.
Expand All @@ -113,7 +150,7 @@ export declare function partition(
* @returns An iterator pair.
* @see {@link https://luafun.github.io/filtering.html#fun.partition}
*/
export declare function partition<TParam, TState, TReturn = unknown[]>(
predicate: (this: void, ...params: [...TReturn]) => boolean,
...iterParams: [...IterParams<TParam, TState, TReturn>],
export declare function partition<TParam, TState, TReturn extends unknown[]>(
predicate: (this: void, ...params: TReturn) => boolean,
...iterParams: [...IterParams<TParam, TState, TReturn>]
): LuaMultiReturn<[FunIterator<TState, TReturn>, FunIterator<TState, TReturn>]>;
50 changes: 22 additions & 28 deletions src/builtin/fun/FunIterator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ export declare type FunIterator<
* @returns The n-th element of iteration.
* @see {@link https://luafun.github.io/slicing.html#fun.nth}
*/
nth(n: number): LuaMultiReturn<[...TReturn] | [undefined]>;
nth(n: number): LuaMultiReturn<TReturn | [undefined]>;

/**
* Extract the first element of iterator. If the iterator is empty then an error is raised.
* @returns A first element of iterator.
* @see {@link https://luafun.github.io/slicing.html#fun.head}
*/
head(): LuaMultiReturn<[...TReturn]>;
head(): LuaMultiReturn<TReturn>;

/**
* An alias for `head()`.
* @see {@link https://luafun.github.io/slicing.html#fun.head}
*/
car(): LuaMultiReturn<[...TReturn]>;
car(): LuaMultiReturn<TReturn>;

/**
* Return a copy of iterator without its first element. If the iterator is empty then an empty iterator is returned.
Expand Down Expand Up @@ -72,7 +72,7 @@ export declare type FunIterator<
* @see {@link https://luafun.github.io/slicing.html#fun.take_while}
*/
take_while(
predicate: (this: void, ...params: [...TReturn]) => boolean
predicate: (this: void, ...params: TReturn) => boolean
): FunIterator<TState, TReturn>;

/**
Expand All @@ -91,7 +91,7 @@ export declare type FunIterator<
* @see {@link https://luafun.github.io/slicing.html#fun.take}
*/
take(
predicate: (this: void, ...params: [...TReturn]) => boolean
predicate: (this: void, ...params: TReturn) => boolean
): FunIterator<TState, TReturn>;

/**
Expand All @@ -110,7 +110,7 @@ export declare type FunIterator<
* @see {@link https://luafun.github.io/slicing.html#fun.drop_while}
*/
drop_while(
predicate: (this: void, ...params: [...TReturn]) => boolean
predicate: (this: void, ...params: TReturn) => boolean
): FunIterator<TState, TReturn>;

/**
Expand All @@ -129,7 +129,7 @@ export declare type FunIterator<
* @see {@link https://luafun.github.io/slicing.html#fun.drop}
*/
drop(
predicate: (this: void, ...params: [...TReturn]) => boolean
predicate: (this: void, ...params: TReturn) => boolean
): FunIterator<TState, TReturn>;

/**
Expand All @@ -153,7 +153,7 @@ export declare type FunIterator<
* @see {@link https://luafun.github.io/slicing.html#fun.span}
*/
span(
predicate: (this: void, ...params: [...TReturn]) => boolean
predicate: (this: void, ...params: TReturn) => boolean
): LuaMultiReturn<
[FunIterator<TState, TReturn>, FunIterator<TState, TReturn>]
>;
Expand All @@ -179,7 +179,7 @@ export declare type FunIterator<
* @see {@link https://luafun.github.io/slicing.html#fun.split}
*/
split(
predicate: (this: void, ...params: [...TReturn]) => boolean
predicate: (this: void, ...params: TReturn) => boolean
): LuaMultiReturn<
[FunIterator<TState, TReturn>, FunIterator<TState, TReturn>]
>;
Expand Down Expand Up @@ -263,7 +263,7 @@ export declare type FunIterator<
* @see {@link https://luafun.github.io/filtering.html#fun.filter}
*/
filter(
predicate: (this: void, ...params: [...TReturn]) => boolean
predicate: (this: void, ...params: TReturn) => boolean
): FunIterator<TState, TReturn>;

/**
Expand All @@ -273,7 +273,7 @@ export declare type FunIterator<
* @see {@link https://luafun.github.io/filtering.html#fun.remove_if}
*/
remove_if(
predicate: (this: void, ...params: [...TReturn]) => boolean
predicate: (this: void, ...params: TReturn) => boolean
): FunIterator<TState, TReturn>;

/**
Expand All @@ -294,7 +294,7 @@ export declare type FunIterator<
* @see {@link https://luafun.github.io/filtering.html#fun.partition}
*/
partition(
predicate: (this: void, ...params: [...TReturn]) => boolean
predicate: (this: void, ...params: TReturn) => boolean
): LuaMultiReturn<
[FunIterator<TState, TReturn>, FunIterator<TState, TReturn>]
>;
Expand All @@ -306,10 +306,7 @@ export declare type FunIterator<
* @returns Reducing result.
* @see {@link https://luafun.github.io/reducing.html#fun.foldl}
*/
foldl<R>(
accfun: (this: void, acc: R, ...args: [...TReturn]) => R,
initval: R
): R;
foldl<R>(accfun: (this: void, acc: R, ...args: TReturn) => R, initval: R): R;

/**
* The function reduces the iterator from left to right using the binary operator `accfun` and the initial value `initval`.
Expand All @@ -318,10 +315,7 @@ export declare type FunIterator<
* @returns Reducing result.
* @see {@link https://luafun.github.io/reducing.html#fun.reduce}
*/
reduce<R>(
accfun: (this: void, acc: R, ...args: [...TReturn]) => R,
initval: R
): R;
reduce<R>(accfun: (this: void, acc: R, ...args: TReturn) => R, initval: R): R;

/**
* Return a number of remaining elements in iterator.
Expand Down Expand Up @@ -359,29 +353,29 @@ export declare type FunIterator<
* Returns `true` if all return values of iterator satisfy the `predicate`.
* @see {@link https://luafun.github.io/reducing.html#fun.all}
*/
all(predicate: (this: void, ...args: [...TReturn]) => boolean): boolean;
all(predicate: (this: void, ...args: TReturn) => boolean): boolean;

/**
* Returns `true` if all return values of iterator satisfy the `predicate`.
* @see {@link https://luafun.github.io/reducing.html#fun.every}
*/
every(predicate: (this: void, ...args: [...TReturn]) => boolean): boolean;
every(predicate: (this: void, ...args: TReturn) => boolean): boolean;

/**
* Returns `true` if at least one return values of iterator satisfy the `predicate`.
* The iteration stops on the first such value.
* Therefore, infinite iterators that have at least one satisfying value might work.
* @see {@link https://luafun.github.io/reducing.html#fun.any}
*/
any(predicate: (this: void, ...args: [...TReturn]) => boolean): boolean;
any(predicate: (this: void, ...args: TReturn) => boolean): boolean;

/**
* Returns `true` if at least one return values of iterator satisfy the `predicate`.
* The iteration stops on the first such value.
* Therefore, infinite iterators that have at least one satisfying value might work.
* @see {@link https://luafun.github.io/reducing.html#fun.some}
*/
some(predicate: (this: void, ...args: [...TReturn]) => boolean): boolean;
some(predicate: (this: void, ...args: TReturn) => boolean): boolean;

/**
* Sum up all iteration values. For an empty iterators `0` is returned.
Expand Down Expand Up @@ -545,10 +539,10 @@ export declare type FunIterator<
): FunIterator<TResultState, TResultReturn>;
};

type EachIterator<TReturn = any[]> = (
fun: (this: void, ...args: [...TReturn]) => unknown
type EachIterator<TReturn extends unknown[]> = (
fun: (this: void, ...args: TReturn) => unknown
) => void;

type GrepPredicateOrRegexp<TReturn = any[]> = TReturn extends string[]
type GrepPredicateOrRegexp<TReturn extends unknown[]> = TReturn extends string[]
? string | ((this: void, element: string) => boolean)
: (this: void, ...params: [...TReturn]) => boolean;
: (this: void, ...params: TReturn) => boolean;
Loading

0 comments on commit cff223d

Please sign in to comment.