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

Add sequence functions to Array, Option, Either modules #7

Closed
noverby opened this issue May 20, 2021 · 3 comments · Fixed by #8
Closed

Add sequence functions to Array, Option, Either modules #7

noverby opened this issue May 20, 2021 · 3 comments · Fixed by #8
Assignees
Labels
enhancement New feature or request

Comments

@noverby
Copy link

noverby commented May 20, 2021

There are no convenience sequence functions like in fp-ts for doing:

  • Option<T>[] -> Option<T[]>
  • Either<E,T>[] -> Either<E,T[]>
@baetheus
Copy link
Owner

baetheus commented May 20, 2021

This was a decision made early on when working out the type classes in hkts. You can see a discussion on it here. You can create a sequence function for any ADT with an Applicative class by doing this:

import * as A from "https://deno.land/x/fun/array.ts";
import * as O from "https://deno.land/x/fun/option.ts";
import { identity, pipe } from "https://deno.land/x/fun/fns.ts";

const sequence = pipe(
  O.map(identity),
  A.traverse(O.Applicative)
)

console.log(sequence([O.some(1), O.some(2)])) // { tag: "Some", value: [ 1, 2 ] }
console.log(sequence([O.none, O.some(2)])) // { tag: "None" }

I have two reasons for not creating a sequence function for each adt.

  1. It's trivial to create that behavior from Array's traverse export.
  2. I want to keep cross module imports low (ie. not pulling in all of the array.ts file for people that only want option or either).

I hope that previous issue link and the above code work for you!

@baetheus
Copy link
Owner

After playing around with the traverse function for awhile I found that the generic is not inferred properly by TypeScript still in the above example. I've implemented a createSequence function that coerces to the correct type in PR #8. Perhaps you wouldn't mind reviewing that?

@baetheus baetheus added the enhancement New feature or request label May 20, 2021
@baetheus baetheus self-assigned this May 20, 2021
@noverby
Copy link
Author

noverby commented May 21, 2021

@baetheus Nice, thanks! I will do a review.

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

Successfully merging a pull request may close this issue.

2 participants