Unexpected behaviour of Nothing inside List inside Association












7












$begingroup$


When evaluating the following input:



<|a -> {c, d, e}|> /. d -> Nothing


I get (expression 1):



<|a -> {c, Nothing, e}|>


However I would expect to get (expression 2):



<|a -> {c, e}|>


Nevertheless, if i place the cursor on the unexpected output (expression 1) and evaluate it by pressing Shift + Enter, I get the expected output (expression 2), which is even more puzzling.



Mathematica version is 11.0.0.0



Should this behaviour be expected or is it a bug? Why?










share|improve this question









$endgroup$








  • 1




    $begingroup$
    closely related: Pattern matching to a function evaluation inside an Association
    $endgroup$
    – kglr
    yesterday
















7












$begingroup$


When evaluating the following input:



<|a -> {c, d, e}|> /. d -> Nothing


I get (expression 1):



<|a -> {c, Nothing, e}|>


However I would expect to get (expression 2):



<|a -> {c, e}|>


Nevertheless, if i place the cursor on the unexpected output (expression 1) and evaluate it by pressing Shift + Enter, I get the expected output (expression 2), which is even more puzzling.



Mathematica version is 11.0.0.0



Should this behaviour be expected or is it a bug? Why?










share|improve this question









$endgroup$








  • 1




    $begingroup$
    closely related: Pattern matching to a function evaluation inside an Association
    $endgroup$
    – kglr
    yesterday














7












7








7


1



$begingroup$


When evaluating the following input:



<|a -> {c, d, e}|> /. d -> Nothing


I get (expression 1):



<|a -> {c, Nothing, e}|>


However I would expect to get (expression 2):



<|a -> {c, e}|>


Nevertheless, if i place the cursor on the unexpected output (expression 1) and evaluate it by pressing Shift + Enter, I get the expected output (expression 2), which is even more puzzling.



Mathematica version is 11.0.0.0



Should this behaviour be expected or is it a bug? Why?










share|improve this question









$endgroup$




When evaluating the following input:



<|a -> {c, d, e}|> /. d -> Nothing


I get (expression 1):



<|a -> {c, Nothing, e}|>


However I would expect to get (expression 2):



<|a -> {c, e}|>


Nevertheless, if i place the cursor on the unexpected output (expression 1) and evaluate it by pressing Shift + Enter, I get the expected output (expression 2), which is even more puzzling.



Mathematica version is 11.0.0.0



Should this behaviour be expected or is it a bug? Why?







list-manipulation replacement associations






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









darmualdarmual

413




413








  • 1




    $begingroup$
    closely related: Pattern matching to a function evaluation inside an Association
    $endgroup$
    – kglr
    yesterday














  • 1




    $begingroup$
    closely related: Pattern matching to a function evaluation inside an Association
    $endgroup$
    – kglr
    yesterday








1




1




$begingroup$
closely related: Pattern matching to a function evaluation inside an Association
$endgroup$
– kglr
yesterday




$begingroup$
closely related: Pattern matching to a function evaluation inside an Association
$endgroup$
– kglr
yesterday










5 Answers
5






active

oldest

votes


















8












$begingroup$

The observed puzzle is resolved by the following two observations:



From Leonid's answer in the linked q/a:






  • Association is HoldAllComplete. Once it is created, its parts will then normally be held unevaluated.




From Nothing>> Details





  • Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.




You can Map ReplaceAll on the association to force removal of Nothings:



ReplaceAll[d -> Nothing] /@ assoc (* or *)
Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>



<|a -> {c, e}|>







share|improve this answer











$endgroup$





















    5












    $begingroup$

    Use Replace with level spec All rather than ReplaceAll:



    Replace[<|a -> {c, d, e}|>, d -> Nothing, All]



    <|a -> {c, e}|>




    Related: Is there a difference between Replace with parameter "All" and ReplaceAll






    share|improve this answer









    $endgroup$





















      3












      $begingroup$

      Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.



      This will not do what your input suggests but let me anticipate your needs:



      <|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //. 
      {a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
      d -> Nothing



      <|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>






      share|improve this answer









      $endgroup$





















        3












        $begingroup$

        For completeness sakes (I believe Coolwater's answer is best):



        assoc = Association[ a → {c,d,e} ];


        Query



        assoc // Query[ All, ReplaceAll[ d → Nothing ] ]

        (* <|a -> {c, e}|> *)


        Note: This will need repeated use of All for nested associations.



        DeleteCases



        Before there was Nothing there was DeleteCases and the like:



        assoc // DeleteCases[ #, d, Infinity ]&

        (* <|a -> {c, e}|> *)





        share|improve this answer











        $endgroup$





















          1












          $begingroup$

          You may use Query to evaluate the result of the replace in an Association. Query gives direct access to the key's value which then evaluated in Query.



          Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>



          <|a -> {c, e}|>



          Hope this helps.






          share|improve this answer









          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "387"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189530%2funexpected-behaviour-of-nothing-inside-list-inside-association%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            8












            $begingroup$

            The observed puzzle is resolved by the following two observations:



            From Leonid's answer in the linked q/a:






            • Association is HoldAllComplete. Once it is created, its parts will then normally be held unevaluated.




            From Nothing>> Details





            • Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.




            You can Map ReplaceAll on the association to force removal of Nothings:



            ReplaceAll[d -> Nothing] /@ assoc (* or *)
            Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>



            <|a -> {c, e}|>







            share|improve this answer











            $endgroup$


















              8












              $begingroup$

              The observed puzzle is resolved by the following two observations:



              From Leonid's answer in the linked q/a:






              • Association is HoldAllComplete. Once it is created, its parts will then normally be held unevaluated.




              From Nothing>> Details





              • Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.




              You can Map ReplaceAll on the association to force removal of Nothings:



              ReplaceAll[d -> Nothing] /@ assoc (* or *)
              Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>



              <|a -> {c, e}|>







              share|improve this answer











              $endgroup$
















                8












                8








                8





                $begingroup$

                The observed puzzle is resolved by the following two observations:



                From Leonid's answer in the linked q/a:






                • Association is HoldAllComplete. Once it is created, its parts will then normally be held unevaluated.




                From Nothing>> Details





                • Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.




                You can Map ReplaceAll on the association to force removal of Nothings:



                ReplaceAll[d -> Nothing] /@ assoc (* or *)
                Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>



                <|a -> {c, e}|>







                share|improve this answer











                $endgroup$



                The observed puzzle is resolved by the following two observations:



                From Leonid's answer in the linked q/a:






                • Association is HoldAllComplete. Once it is created, its parts will then normally be held unevaluated.




                From Nothing>> Details





                • Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.




                You can Map ReplaceAll on the association to force removal of Nothings:



                ReplaceAll[d -> Nothing] /@ assoc (* or *)
                Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>



                <|a -> {c, e}|>








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited yesterday

























                answered yesterday









                kglrkglr

                179k9198410




                179k9198410























                    5












                    $begingroup$

                    Use Replace with level spec All rather than ReplaceAll:



                    Replace[<|a -> {c, d, e}|>, d -> Nothing, All]



                    <|a -> {c, e}|>




                    Related: Is there a difference between Replace with parameter "All" and ReplaceAll






                    share|improve this answer









                    $endgroup$


















                      5












                      $begingroup$

                      Use Replace with level spec All rather than ReplaceAll:



                      Replace[<|a -> {c, d, e}|>, d -> Nothing, All]



                      <|a -> {c, e}|>




                      Related: Is there a difference between Replace with parameter "All" and ReplaceAll






                      share|improve this answer









                      $endgroup$
















                        5












                        5








                        5





                        $begingroup$

                        Use Replace with level spec All rather than ReplaceAll:



                        Replace[<|a -> {c, d, e}|>, d -> Nothing, All]



                        <|a -> {c, e}|>




                        Related: Is there a difference between Replace with parameter "All" and ReplaceAll






                        share|improve this answer









                        $endgroup$



                        Use Replace with level spec All rather than ReplaceAll:



                        Replace[<|a -> {c, d, e}|>, d -> Nothing, All]



                        <|a -> {c, e}|>




                        Related: Is there a difference between Replace with parameter "All" and ReplaceAll







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered yesterday









                        CoolwaterCoolwater

                        14.8k32553




                        14.8k32553























                            3












                            $begingroup$

                            Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.



                            This will not do what your input suggests but let me anticipate your needs:



                            <|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //. 
                            {a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
                            d -> Nothing



                            <|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>






                            share|improve this answer









                            $endgroup$


















                              3












                              $begingroup$

                              Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.



                              This will not do what your input suggests but let me anticipate your needs:



                              <|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //. 
                              {a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
                              d -> Nothing



                              <|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>






                              share|improve this answer









                              $endgroup$
















                                3












                                3








                                3





                                $begingroup$

                                Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.



                                This will not do what your input suggests but let me anticipate your needs:



                                <|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //. 
                                {a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
                                d -> Nothing



                                <|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>






                                share|improve this answer









                                $endgroup$



                                Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.



                                This will not do what your input suggests but let me anticipate your needs:



                                <|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //. 
                                {a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
                                d -> Nothing



                                <|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered yesterday









                                KubaKuba

                                104k12201519




                                104k12201519























                                    3












                                    $begingroup$

                                    For completeness sakes (I believe Coolwater's answer is best):



                                    assoc = Association[ a → {c,d,e} ];


                                    Query



                                    assoc // Query[ All, ReplaceAll[ d → Nothing ] ]

                                    (* <|a -> {c, e}|> *)


                                    Note: This will need repeated use of All for nested associations.



                                    DeleteCases



                                    Before there was Nothing there was DeleteCases and the like:



                                    assoc // DeleteCases[ #, d, Infinity ]&

                                    (* <|a -> {c, e}|> *)





                                    share|improve this answer











                                    $endgroup$


















                                      3












                                      $begingroup$

                                      For completeness sakes (I believe Coolwater's answer is best):



                                      assoc = Association[ a → {c,d,e} ];


                                      Query



                                      assoc // Query[ All, ReplaceAll[ d → Nothing ] ]

                                      (* <|a -> {c, e}|> *)


                                      Note: This will need repeated use of All for nested associations.



                                      DeleteCases



                                      Before there was Nothing there was DeleteCases and the like:



                                      assoc // DeleteCases[ #, d, Infinity ]&

                                      (* <|a -> {c, e}|> *)





                                      share|improve this answer











                                      $endgroup$
















                                        3












                                        3








                                        3





                                        $begingroup$

                                        For completeness sakes (I believe Coolwater's answer is best):



                                        assoc = Association[ a → {c,d,e} ];


                                        Query



                                        assoc // Query[ All, ReplaceAll[ d → Nothing ] ]

                                        (* <|a -> {c, e}|> *)


                                        Note: This will need repeated use of All for nested associations.



                                        DeleteCases



                                        Before there was Nothing there was DeleteCases and the like:



                                        assoc // DeleteCases[ #, d, Infinity ]&

                                        (* <|a -> {c, e}|> *)





                                        share|improve this answer











                                        $endgroup$



                                        For completeness sakes (I believe Coolwater's answer is best):



                                        assoc = Association[ a → {c,d,e} ];


                                        Query



                                        assoc // Query[ All, ReplaceAll[ d → Nothing ] ]

                                        (* <|a -> {c, e}|> *)


                                        Note: This will need repeated use of All for nested associations.



                                        DeleteCases



                                        Before there was Nothing there was DeleteCases and the like:



                                        assoc // DeleteCases[ #, d, Infinity ]&

                                        (* <|a -> {c, e}|> *)






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited yesterday

























                                        answered yesterday









                                        gwrgwr

                                        7,70322558




                                        7,70322558























                                            1












                                            $begingroup$

                                            You may use Query to evaluate the result of the replace in an Association. Query gives direct access to the key's value which then evaluated in Query.



                                            Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>



                                            <|a -> {c, e}|>



                                            Hope this helps.






                                            share|improve this answer









                                            $endgroup$


















                                              1












                                              $begingroup$

                                              You may use Query to evaluate the result of the replace in an Association. Query gives direct access to the key's value which then evaluated in Query.



                                              Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>



                                              <|a -> {c, e}|>



                                              Hope this helps.






                                              share|improve this answer









                                              $endgroup$
















                                                1












                                                1








                                                1





                                                $begingroup$

                                                You may use Query to evaluate the result of the replace in an Association. Query gives direct access to the key's value which then evaluated in Query.



                                                Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>



                                                <|a -> {c, e}|>



                                                Hope this helps.






                                                share|improve this answer









                                                $endgroup$



                                                You may use Query to evaluate the result of the replace in an Association. Query gives direct access to the key's value which then evaluated in Query.



                                                Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>



                                                <|a -> {c, e}|>



                                                Hope this helps.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered yesterday









                                                EdmundEdmund

                                                26.1k330100




                                                26.1k330100






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Mathematica Stack Exchange!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    Use MathJax to format equations. MathJax reference.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189530%2funexpected-behaviour-of-nothing-inside-list-inside-association%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Loup dans la culture

                                                    How to solve the problem of ntp “Unable to contact time server” from KDE?

                                                    ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux