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

putState? #15

Closed
mgreenbe opened this issue Jun 29, 2018 · 5 comments
Closed

putState? #15

mgreenbe opened this issue Jun 29, 2018 · 5 comments

Comments

@mgreenbe
Copy link

Is there a built-in parser, analogous to putState (formerly, setState) in Parsec that sets the user state and returns nothing? Is there a mechanism to replace user state without performing an explicit mutation/side effect (e.g., userState.tags.push(...) like in the examples in the readme)?

@GregRos
Copy link
Owner

GregRos commented Jun 30, 2018

Nope. It's sort-of by design. In Haskell, there is no way to mutate anything at all, so you have to be able to replace the user state. In F# or similar, you can't just define properties on the state object willy-nilly either, so again you need to have a way to alter it.

In JavaScript though, I think it's not needed and keeping the same user state object throughout the parsing session has advantages. There is no problem of parallelism, since this is totally synchronous JS we're talking about. You need to allocate fewer objects, and there is no problem of erasing data other parsers need to function. It also just seems appealing in terms of API, since that way there is a single, unified method of modifying user state, and you can do it from pretty much any parser/combinator.

If you don't want to add/delete properties from the object whenever you want, you can always create a specific property, e.g. immutable, and update only that property with a new object every time. The result is quite similar to a putState parser.

But if you really don't like it, I can just add it in. Can you explain why it's important to you?

@mgreenbe
Copy link
Author

mgreenbe commented Jul 1, 2018

My reasoning is twofold: A putState parser would make it really simple to port things from (F)Parsec. Second, I'd like userState state to revert if the parser needs to backtrack, and that won't happen automatically if I'm always mutating it in place (right?).

@GregRos
Copy link
Owner

GregRos commented Jul 2, 2018

@mgreenbe Interesting! I never really thought about reverting user state. And yeah, you're right, it's probably not possible unless I allow replacing user state in the first place.

Now that I see the reasoning, it's definitely something I'll add. The two techniques can be complementary.

@mgreenbe
Copy link
Author

mgreenbe commented Jul 3, 2018

Thanks! This will be a big help to me.

@GregRos
Copy link
Owner

GregRos commented Jul 28, 2019

Now there is a replaceState combinator that fulfills this role. It replaces the state in the context of the parser you're applying it to.

@GregRos GregRos closed this as completed Jul 28, 2019
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

2 participants