Effect CommunityEC
Effect Community3y ago
21 replies
leonitous

Questioning the Correctness of `List.map` Implementation

I might be missing something, but I don't think that the implementation for List.map is correct? The type signature says that I can pass a function with the following signature: f: (a: any, i: number) => any[] and I inferred that i must be an index parameter, as that is the same functionality as in JS. However, look at these example cases:

import { List, pipe } from "effect";

// prints:
// {
//   _id: 'List',
//   _tag: 'Cons',
//   values: [
//     [ 1, undefined ],
//     [ 2, undefined ],
//     [ 3, undefined ],
//     [ 4, undefined ],
//     [ 5, undefined ]
//   ]
// }
const test: List.List<[number, number]> = pipe(
    List.fromIterable([1, 2, 3, 4, 5]),
    List.map((x, index) => [x, index])
);
console.log(test.toJSON());


import { List, pipe } from "effect";

// Same thing but without pipe, idk why I thought it would be different
// prints:
// {
//   _id: 'List',
//   _tag: 'Cons',
//   values: [
//     [ 1, undefined ],
//     [ 2, undefined ],
//     [ 3, undefined ],
//     [ 4, undefined ],
//     [ 5, undefined ]
//   ]
// }
const l1 = List.fromIterable([1, 2, 3, 4, 5]);
const l2 = List.map((x, index) => [x, index])(l1);
console.log(l2.toJSON());


Reading the source (https://github.com/Effect-TS/effect/blob/e0ef64102b05c874e844f680f53746821609e1b6/src/List.ts#L734-L752) for List.map it seems like it only ever calls the mapping function with one parameter which makes sense why the index is always undefined. Is the known/desired, i.e is the implementation correct but the type signature wrong. Or is it a bug, i.e the type signature is correct but the implementation is wrong? I am willing and I think I could submit a PR if this is indeed a bug.
Was this page helpful?