Comments (4)
Have you tried something like
def maybe_delete(struct, path) do
case Pathex.delete(struct, path) do
{:ok, result} -> result
:error -> struct
end
end
?
from pathex.
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.
from pathex.
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
maybe delete(structure, path)
Or even
maybe over(structure, path, fn x -> x + 1 end)
But not
maybe view(structure, path)
...
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 Map
module provides only version of delete
and I think that's okay for Pathex
to provide the same functionality
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
- Create
MyProject.PathexCommon
and import it everyewhere you need it. - Use
maybe
macro from above. - Create your own
MyProject.PathexExtension
where you'll add ability to import your helpers andPathex
within oneuse
- Fork
Pathex
and add anything you like. I can answer any questions about adding a verb intoPathex
, becausegen
function there is not trivial
By the way, I have a Pathex.HTML
project in development and I am thinking about creating Pathex.Language
(this is not a final version of the name) which will have extra lenses and verbs for general nested structures, perhaps I'll add this delete
there
from pathex.
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.
from pathex.
Related Issues (12)
- Error when using tuple with variable in path creation
- Pathex does not compile with elixir 1.14.0-rc0 HOT 2
- Invalid behaviour of `Pathex.delete!` when using concatenation and/or `all` lense HOT 5
- All lens deletion bug HOT 1
- Dialyzer complains about deletion functions HOT 5
- exists?/2 bug
- Single-line import of core, lenses, and combinator HOT 3
- Is it possible to define collection type when using from_list? HOT 2
- path(y :: :list, :json)
- Delete a key? HOT 4
- How should I create a path from a list? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pathex.