Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename "seed" to "accumulator" in Reducer type signature #35

Open
Frikki opened this issue Nov 18, 2017 · 4 comments
Open

Rename "seed" to "accumulator" in Reducer type signature #35

Frikki opened this issue Nov 18, 2017 · 4 comments

Comments

@Frikki
Copy link
Collaborator

Frikki commented Nov 18, 2017

The type definition for reduce() in @typed/prelude does not match the type definition for reduce() in @typed/list.

@typed/list:

export declare type Reducer<A, B> = (seed: B, value: A, index: number) => B;
export declare type ReduceArity3 = {
    <A, B>(f: Reducer<A, B>, seed: B, list: List<A>): B;
    <A, B>(f: Reducer<A, B>, seed: B): ReduceArity1<A, B>;
    <A, B>(f: Reducer<A, B>): ReduceArity2<A, B>;
};
export declare type ReduceArity2<A, B> = {
    (seed: B, list: List<A>): B;
    (seed: B): ReduceArity1<A, B>;
};
export declare type ReduceArity1<A, B> = {
    (list: List<A>): B;
};

@typed/prelude:

export function reduce<A, B>(f: (acc: A, val: B) => A, seed: A, arr: B[]): A

The “prelude”-reduce doesn’t allow ReadonlyArrays as lists, for example.

In addition, it seems incorrect to call the first parameter of the Reducer for seed. To my knowledge, it is common to call it accumulator or just acc. It is, however, correct to call it seed in the reduce.

@TylorS
Copy link
Owner

TylorS commented Nov 19, 2017

It may not be obvious that it supports ReadonlyArray, also Array and ArrayLike, because they are valid implementations of List.

Beyond that, it seems you'd like to have the name of the first parameter to Reducer<A, B> be renamed to accumulator? I'd be happy to see that change 👍

@Frikki
Copy link
Collaborator Author

Frikki commented Nov 19, 2017

The prelude export is not List<A> but B[]. Thus, you can not use a ReadonlyArray nor ArrayLike if you operate with the prelude version. In addition, prelude’s reduce specifies the first template type A as the seed and return type, whereas the list version has the template types reversed.

@TylorS
Copy link
Owner

TylorS commented Nov 20, 2017

Not to dismiss the issue you're running into, but I've been unable to recreate your issue.
screen shot 2017-11-20 at 9 19 55 am

@Frikki
Copy link
Collaborator Author

Frikki commented Nov 20, 2017

You’re right, you’re right. The code I was looking at was importing reduce from @most/prelude. Must have been tired. My bad.

I still would like to introduce the parameter name change, though.

@TylorS TylorS changed the title Fix reduce type definition in prelude Rename "seed" to "accumulator" in Reducer type signature Dec 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants