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

Strict conversion functions? #38

Open
gfredericks opened this issue Apr 4, 2017 · 3 comments
Open

Strict conversion functions? #38

gfredericks opened this issue Apr 4, 2017 · 3 comments

Comments

@gfredericks
Copy link

I personally rather like bijections more than coercions.

E.g., if I have a web API that is supposed to accept snake case query params and I use ->kebab-case to translate them to the codebase's internal format, then what I really have is an API that accepts snake case and kebab case and arbitrary combinations of the two. And this bothers me a bit.

Would it be welcome to add functions to this library that convert from one format to another and perhaps throw exceptions if the input is not in the expected format?

Problems

  • n² new functions, which is a lot
  • is this even well defined enough to implement reliably?
@qerub
Copy link
Collaborator

qerub commented Apr 23, 2017

I share your sentiments and have been thinking about revising the API to take this shape:

(convert input :to kebab-case)
;; best-effort coercion of input

(convert input :from snake_case :to kebab-case)
;; throws exception if input is not snake_case

kebab-case
;; => {:splitter  …fn of String => [String]…
;;     :joiner    …fn of [String] => String…
;;     :validator …fn of String => boolean…}

What do you think? It avoids the n² problem nicely.

Perhaps also:

((convert :from snake_case :to kebab-case) input)
;; convert function can be partially applied

@gfredericks
Copy link
Author

This definitely addresses my concerns. I can't think of any improvements.

@tendant
Copy link

tendant commented Nov 14, 2017

I am trying to use this library exactly the same way. With the improved API, the result is much more predictable.

It is better to have more functions with predictable result, than just a few functions with some unpredictable corner cases.

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