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

Predicate factory #217

Open
eloytoro opened this issue Jan 24, 2024 · 3 comments
Open

Predicate factory #217

eloytoro opened this issue Jan 24, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@eloytoro
Copy link

Is your feature request related to a problem? Please describe.

the match function is the way to start defining cases for a value, but its design is limited because its reactive rather than proactive, by this i mean that you can only define cases when the value exists in the scope but there's no way to define case handling logic in a deferred and composable way

Describe the solution you'd like

I think this can be solved if there were an API that allowed for the definition of cases without having the value that will be matched against them, for example

const digitsFlow = flow<number>()
  .with(P.number, (n) => n.toString().length)
  .exhaustive();

Now we have a lazy definition of pattern matching, that can be run against any number of values in a pure way and it could also be made composable

const digits10 = digitsFlow(10)

const digitsStringFlow = flow<number | string>)
  .compose(digitsFlow) // new method to compose flows together
  .with(P.string, (s) => n.length)
  .exhaustive() // ts is smart enough to know that the `number` case was handled exhaustively

Describe alternatives you've considered

the naive way of course would be to make heavy use of lambdas but it would be quite ugly for composability

const digitsFlow = (n: number) => match(n)
  .with(P.number, (n) => n.toString().length)
  .exhaustive();

const digitsStringFlow = (n: string | number) => match(n)
  .with(P.number, => digitsFlow) // here you'd have to manually check that all cases are handled
  .with(P.string, (s) => n.length)
  .exhaustive()
@eloytoro eloytoro added the enhancement New feature or request label Jan 24, 2024
@phaux
Copy link

phaux commented Jan 26, 2024

related: #215 #209

@orimay
Copy link

orimay commented Apr 10, 2024

Just came here to open the same issue. Besides, this way we'll get rid of a ton of runtime allocations (every callback, every array — they are all objects created at runtime and bloat GC).

@orimay
Copy link

orimay commented Apr 11, 2024

I wrote a small representation of how it might be build:

Playground

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

No branches or pull requests

3 participants