Skip to content

Add isNull predicate#6

Merged
garyb merged 1 commit intopurescript:masterfrom
anttih:is-null
May 16, 2024
Merged

Add isNull predicate#6
garyb merged 1 commit intopurescript:masterfrom
anttih:is-null

Conversation

@anttih
Copy link
Copy Markdown
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
Copy Markdown
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
@natefaubion
Copy link
Copy Markdown

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