-
Notifications
You must be signed in to change notification settings - Fork 17
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
feature request: Delete op that succeeds even if path isn't found #19
Comments
Have you tried something like def maybe_delete(struct, path) do
case Pathex.delete(struct, path) do
{:ok, result} -> result
:error -> struct
end
end ? |
yes, I can wrap it. It's a common enough pattern (for me anyway), that I'd love for it to be built in so I don't have to carry around my own pathex helper library. maybe_delete is a good name. |
Check this out defmacro maybe({operation, meta, [object | tail]}) do
[object_var] = Macro.generate_unique_arguments(1, __CALLER__.context)
call = {operation, meta, [object_var | tail]}
quote do
unquote(object_var) = unquote(object)
case unquote(call) do
{:ok, result} -> result
:error -> unquote(object_var)
end
end
end With this you can write something like
Or even
But I don't think that it is a right way. I mean, adding a function is not a hard part, but there can be a huge amount of functions with a lot of flavours. I am already providing two of them: "exception" style and "either" style. For example, elixir's And writing something like your own macro/higher-order function is not difficult and it doesn't sacrifice readability. And having 3 different functions for doing one thing is not a good design. I think the right way is to provide tools, not solutions. So, I'd suggest for you to
By the way, I have a |
Just noting, that elixir Map.delete always returns the struct even if the key wasn't present. Although it's a breaking change, maybe delete could return {:error, struct}. Again, pathex is great. It has generally made wrangling deeply nested structures more readable while reducing overall number of lines of code. |
I'd love for delete(struct,path) to have an option to always return struct, even if path wasn't found in struct.
I'm not sure if it's a new call, or an optional third param to delete() or pop().
The text was updated successfully, but these errors were encountered: