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

Add isNull predicate #6

Merged
merged 1 commit into from
May 16, 2024
Merged

Add isNull predicate #6

merged 1 commit into from
May 16, 2024

Conversation

anttih
Copy link
Contributor

@anttih anttih commented May 16, 2024

Allows writing a faster decoder for optional values because you don't have to call case_ twice to first peel off the null case and then do the actual decoding for the value. This came up while porting backend-optimizer to use json where I had to add a nullable:

nullable :: JSON -> Maybe JSON
nullable json = case_
  (const Nothing)
  (Just <<< fromBoolean)
  (Just <<< fromNumber)
  (Just <<< fromString)
  (Just <<< fromJArray)
  (Just <<< fromJObject)
  json

I can add predicates for other types as well if needed.

Ping @natefaubion

Allows writing a faster decoder for optional values because you don't
have to call `case_` twice to first peel off the null case and then do
the actual decoding for the value.
@garyb
Copy link
Member

garyb commented May 16, 2024

Sounds good to me. I think we can do without the other predicates - I was aiming to keep the FFI footprint of this library as small as possible for portability reasons - but null does seem like a special case where you'll often want to guard against it rather than fold over the all the available types. I guess there is toNull too which kinda works for that, but it's not really like the others since you're only going to care about the Just-iness of the reuslt.

@garyb garyb merged commit da46957 into purescript:master May 16, 2024
1 check passed
@natefaubion
Copy link

It's not totally obvious to me why this needed to be FFI, FWIW. a toNull implemented with case_ is OK enough. For optimization purposes I just need a function to exist in the library to hook into with obvious semantics that can potentially be inlined during codegen, but it doesn't necessarily need to be FFI.

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.

3 participants