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

and_then support in simdjson_result for monadic operations #2162

Open
madhur4127 opened this issue Apr 4, 2024 · 2 comments
Open

and_then support in simdjson_result for monadic operations #2162

madhur4127 opened this issue Apr 4, 2024 · 2 comments

Comments

@madhur4127
Copy link
Contributor

Borrowing the idea of and_then from std::optional / std::expected, error handling for parsing multiple fields can be easy as it can be chained.

simdjson::ondemand::object o;
o[key1].and_then(...).and_then(...);

Instead of checking for error twice in the case above.

@lemire
Copy link
Member

lemire commented Apr 4, 2024

We already support chaining in our main API (on demand). Screenshot from our documentation:

Screenshot 2024-04-04 at 11 36 48 AM

We do not support chaining arbitrary lambda expressions, although it could be added with ease if there is a use case for it.

@madhur4127
Copy link
Contributor Author

I think that chaining refers wrt to nesting of the fields but another way could be to parse fields at the same level of nesting.

I maybe wrong but there's no simple exception free way to check for error without checking each field.

{"a":1, "b":2}

If I want to parse a and then b, I have to check for error twice in each step. With and_then, it's easier as you can focus on error free path and handle errors at last.

One way is to have a variable for each key and then use get() in succession but feels terse than just using and_then

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