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

Investigate structured conditional computations #410

Closed
kasperisager opened this issue Sep 23, 2020 · 0 comments · Fixed by #702
Closed

Investigate structured conditional computations #410

kasperisager opened this issue Sep 23, 2020 · 0 comments · Fixed by #702
Assignees

Comments

@kasperisager
Copy link
Contributor

kasperisager commented Sep 23, 2020

#394 (comment)

Inspired by https://www.staff.ncl.ac.uk/andrey.mokhov/selective-functors.pdf, I drafted this API:

interface Selective<S, T = never> {
  value: Either<S, T>;

  when<P extends S, U>(
    when: Refinement<S, P>,
    mapper: Mapper<P, U>
  ): Selective<S, T | U>;

  get(): T;

  getOr<U>(value: U): T | U;

  getOrElse<U>(thunk: Thunk<U>): T | U;
};

The idea is that Selective models selective function application of type S -> T. Given a type refinement S -> P, Selective#when() applies a function P -> U, producing a new Selective of type S -> T | U. It looks like this in action:

declare const string: Selective<string>;

declare const isFoo: Refinement<string, "foo">;

declare const isBar: Refinement<string, "bar">;

const number = string
  // If string is "foo", produce 42
  .when(isFoo, (foo) => 42)
  // If string is "bar", produce 56
  .when(isBar, (bar) => 56)
  // If string is neither "bar" nor "foo", produce 23
  .getOr(23);
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 a pull request may close this issue.

1 participant