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

What to do about transducers #3

Open
Harris-Miller opened this issue Jan 10, 2023 · 0 comments
Open

What to do about transducers #3

Harris-Miller opened this issue Jan 10, 2023 · 0 comments

Comments

@Harris-Miller
Copy link
Collaborator

Transducers are a particularly challenging thing to do in typescript do to how "functions that act as transformers" are isomorphic to them

const a =  [1, 2, 3];

const f = R.compose(
  R.map(R.multiply(2)),
  R.map(R.add(3)),
);

// compose runs the array values through the first map and then the second
f(a); // [5,8,11]

// transduce runs the init transformer into the multiple transformers into the add transformer
R.transduce(f, R.flip(R.append), [], a); // [9, 12, 15]

Now while the above example works fine, once you start transitioning through the types it gets weird quickly

const a =  [1, 2, 3];

// this is completely contrived but gets the point across
const f = R.compose(
  R.map(R.toString()),
  R.map(R.add(3)),
);

f(a); // ['3', '6', '9'];

// because now `toString` is processed first, `add` does string concatenation
R.transduce(f, R.flip(R.append), [], a); // ['13, '23', '33']

Typescript doesn't have "look-ahead" capabilities where it can see where a function is used to determine what its type should be for isomorphic behavior like this

The only way around this that I can think of is to have a separate function for compose and pipe that is typed specifically to the order of operation here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant