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

feat: implement sequence helper function in array #8

Merged
merged 2 commits into from
May 21, 2021
Merged

Conversation

baetheus
Copy link
Owner

TypeScript still has issues inferring the correct type for:

  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)
  )

Since there is a call for an easy sequence (not over a tuple) of arrays
of various ADTs this commit implements a createSequence function in
array.ts.

  import * as A from "../array.ts";
  import * as O from "../option.ts";
  import { identity, pipe } from "../fns.ts";

  const sequence = A.createSequence(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" }

While it is not used to create an adt specific sequence function for
every ADT it's possible that it might be used thusly in the future.

Fixes #7

TypeScript still has issues inferring the correct type for:

      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)
      )

Since there is a call for an easy sequence (not over a tuple) of arrays
of various ADTs this commit implements a createSequence function in
array.ts.

      import * as A from "../array.ts";
      import * as O from "../option.ts";
      import { identity, pipe } from "../fns.ts";

      const sequence = A.createSequence(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" }

While it is not used to create an adt specific sequence function for
every ADT it's possible that it might be used thusly in the future.
@noverby
Copy link

noverby commented May 21, 2021

I'm not sure how to use the helper function, if you have an array of readonly Eithers returned from Array.map:

const seq = A.createSequence(E.Applicative);
const loadConfigFile = (confPath: string): E.Either<Error, ReadonlyArray<Config>>

F.pipe(
  A.map(loadConfigFile), // Type: readonly E.Either<Error, ReadonlyArray<Config>>[]
  seq,
)

It results in the error:

The type 'readonly Either<Error, readonly Config[]>[]' is 'readonly' and cannot be assigned to the mutable type 'Either<Error, readonly Config[]>[]'

The readonly keyword works fine with the following sequence helper function:

const traverseOption = A.IndexedTraversable.traverse(E.Applicative);
const seq = traverseOption((a: E.Either<Error, ReadonlyArray<Config>>) => a);

@baetheus
Copy link
Owner Author

I've modified the signature of the createSequence function, can you check to see that it works with your code now?

@noverby
Copy link

noverby commented May 21, 2021

@baetheus Now it works, thanks! LGMT

@baetheus baetheus merged commit a12d01d into main May 21, 2021
@baetheus baetheus deleted the feat/sequence branch November 1, 2021 21:33
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

Successfully merging this pull request may close these issues.

Add sequence functions to Array, Option, Either modules
2 participants