vegabook
:digraph.add_edge gives unpredictable results when adding an edge, one only knows that the first element in the list will be :"$e"
I am trying to create an atomic :digraph.add_vertex+:digraph.add_edge+:dets.insert function. If any clauses in the with statement below fail, I need to roll back the previous ones. So if the last clause (:dets.insert) fails, I need to delete the vertex and the edge I just created. In order to do that, I need to destructure the return of the :digraph.add_edge function in the with clause, so that I can refer back to the created edge in the else part…
defp atomic_vedge({detspath, graph}, v, parent, meta, ts) do
# atomic add of a vertex and its edge to parent and save to dets
# if any stage fails preceding successes are rolled back
with {:addvertex, v} <- {:addvertex, :digraph.add_vertex(graph, v, meta)},
{:addedge, [:"$e" | rest]} <- {:addedge, :digraph.add_edge(graph, parent, v)},
{:insert, :ok} <-
{:insert, :dets.insert(detspath, {v, parent, ts, :vertedge, meta})} do
{:ok, {detspath, graph}}
else
{:addvertex, {:error, reason}} ->
{:error, reason}
{:addedge, {:error, reason}} ->
:digraph.del_vertex(graph, v)
{:error, reason}
{:insert, false} ->
:digraph.del_vertex(graph, v)
:digraph.del_edge(graph, [:"$e" | rest])
{:error, "dets vertex insert failed"}
end
end
However this is giving me an error when compiling:
Does this mean I cannot destructure into [first | rest] in a with statement? So I cannot track what the :digraph.add_edge returned in order to roll it back? It seems to be a problem with the [head | rest] part because the previous {:addvertex, v} doesn’t seem to cause a problem.
(the problem here btw is that :digraph is side-effecty. It doesn’t return a new graph each time because it uses ETS tables and that’s why I’m having to jump through these hoops)
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security













Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sabiwara
This is a scope problem:
restis not available in theelseclauses since it only gets defined when the pattern-matching succeeds.Since you only need it in the
insertcase, you could pass it in the tuple maybe?{:insert, rest, :dets.insert(...)}Regarding
v, it is available in the parent scope, which is why you’re not getting the error (but it might not be the value ofvyou expect). Here is a minimal example:PS: Complex
elseclauses inwithcan be considered an anti-pattern, maybe this code would actually be clearer with simply nestingcase? This way, the necessary variables from intermediate matches would be in scope.Eiji
Elixirworks as expected. The bug is in your code. Firstly let’s take a look at the warning. As it says therestvariable is not used insidedo … elseblock. Secondly the value is assigned to variable only onsuccess, so it cannot be used outside ofdo … elseblock which contains instructions for success scenario. Having above in mind the error message is also correct as there is norestvariable assigned forelse … endblock which contains an instructions whenwithmatches fails.You can still access this data, but this is a bit tricky … See a simplified example below:
The above code obviously returns:
vegabook
yeah I grokked the basics of
withstatements and liked them so much I wanted to use them extensively. Maybe too extensively! Now I grok them properly. Thank you. Back to good ol’ nestedcasein this (ahem) case.Eiji
Nesting
caseis also anti-pattern. SinceElixirversion1.12.0(see Kernel.then/2) and especially afterLivebookversion0.7(see Interactive user interface to visualize and edit Elixir pipelines) my personal guilty pleasure is to use pipes often, really really often.It’s much more clear than above
withcode since we see much better:kokolegorille
You might have an arrow too much… it should be fn instead of fn →
Where I could use multiple heads anonymous function… I would prefer normal Module function with multiple heads.
But it’s just a question of taste
adamu
Did you just tell the guy who wrote it to look it up?
Eiji
Thanks, yes I have written it from memory.
Personally I have 3 levels here:
tap/thenmacros like for example the ones from ok hex packageUmm … maybe?
Elixirfeaturevegabook
Looks okay by me the nested case. Thanks for help. Actually I don’t see how this is really an anti-pattern as @Eiji was saying, for modest nesting depths, and Racket people might actually love it
The only slightly messy thing here is trapping the
:detserror, and I know “let it fail” and all that but in this case I do need to handle it because otherwise I’ll have dangling edges and vertices in my graph. Any thoughts on this erlang lib error trapping would be appreciated but this thing for now passes a bunch of tests. Again thank you for help to all.Eiji
Never heard about “Racket people”. Google also translates racket as same as rocket. I guess such people prefer to not use chaining calls in
Rubyor pipes inBashand of course they have their own secret arguments, I never heard of, for that.Sorry, but it sounds like an
Elixirdeveloper saying about other language:Well … no matter what we talk about like pipes, let it fail principle or literally anything else … They are all parts of language, no? Depending on the case you would sooner or later use them more or less and I really don’t get a point when people coming from other languages expects … I’m not even sure what exactly …
As always I’m speaking generally. One time I saw someone doing … something? Just imagine that whole logic is in only one function (from parsing custom absinthe type, via middleware, resolver and context logic up to custom
ectoqueries). 300+ LOC for just one function, over 10 levels of indention, even rewritten functions ofElixircore and mandatorily with allcredorules disabled (he added credo btw.). All of that because:Elixircore functions does not work as a language he came fromcredoand I following it “complain”and some other arguments like that
After experiencing a hell of unclear rules coming from “some language” by default I just can’t look positively on a code which do not follow any
Elixirstyle guide. Of course I don’t mean to force someone to use my favourite one. Also there is no rule which covers all cases, but especially on forum when sharing opinions and code samples for possibly thousands of readers I try to keep consistent style which is well explained in some style guide.Of course
Elixirlanguage gives a lot of freedom, for example we could even use non-ASCII characters in variable names, so in theory I could write a code withPolishnaming. I can of course write tons ofdelegatedfunctions and make all core functions be callable usingPolishnaming and just for sure for me it would look good. First of all I’m native Polish and secondly it would be my own code, so I can consider it as a clear.However with all that freedom there comes responsibility. Such code is not clear for everyone and therefore it’s definitely not good at least when sharing in international community. Same comes for other things like many indention levels. There is no point in thinking if for you or someone else it’s clear. All I know is that it’s terribly hard to read in my personal opinion and from my perspective such code should be refactored.
As always I’m never trying to be guru. After all that years I’m sure about few things and one of them is to follow in most cases a good practices, style guides and other resources like that. Instead of forcing other to do something in specific way it’s better to show alternatives. I believe that my simplified version speaks for itself well enough, so that’s all from me. Have a good night.
sabiwara
I agree that avoiding over-nesting and splitting in small functions is good advice in general.
Regarding what the exact maximum nesting level should be,
1feels quite opinionated. For example, it is quite easy to find examples that are nesting 2 or 3caseexpressions within the Elixir codebase.But like anything readability related, this is a very controversial topic, so I don’t think that there’s such a thing as a correct answer.
For the record, here is an opposite take embracing nesting over splitting functions (not necessarily agreeing but interesting to hear both sides).