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

variants of S.get, S.gets, and S.parseJson which use $.test #481

Closed
davidchambers opened this issue Jan 26, 2018 · 3 comments
Closed

variants of S.get, S.gets, and S.parseJson which use $.test #481

davidchambers opened this issue Jan 26, 2018 · 3 comments
Assignees

Comments

@davidchambers
Copy link
Member

Using $.test in conjunction with S.get, S.gets, or S.parseJson is incredibly powerful. Consider these examples:

> S.parseJson ($.test ([]) ($.Array ($.Integer))) ('[1,2')
Nothing

> S.parseJson ($.test ([]) ($.Array ($.Integer))) ('[1,2,3]')
Just ([1, 2, 3])

> S.parseJson ($.test ([]) ($.Array ($.Integer))) ('[1,2,3,0.5]')
Nothing

We could provide _-suffixed variants for convenience:

get_ :: Type -> String -> Any -> Maybe a
gets_ :: Type -> Array String -> Any -> Maybe a
parseJson_ :: Type -> String -> Maybe a

One could then write:

> S.parseJson_ ($.Array ($.Integer)) ('[1,2')
Nothing

> S.parseJson_ ($.Array ($.Integer)) ('[1,2,3]')
Just ([1, 2, 3])

> S.parseJson_ ($.Array ($.Integer)) ('[1,2,3,0.5]')
Nothing

👍 / 👎 ?

@miwillhite
Copy link

👍 I like this a lot! Shaping data is one of the biggest and most important tasks I find myself in when working with the large, unkempt codebase that is my current job ;)

This is a great, non-intrusive (vs creating ADTs for everything) way of adding some safety around the data floating around my application.

@najlachamseddine
Copy link

I like it as it makes the type checking on data more explicit

@davidchambers
Copy link
Member Author

#513 greatly improved the usability of S.get, S.gets, and S.parseJson, so variants of these functions are no longer necessary.

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

3 participants