Shuffle lists in structs nested in another list









up vote
1
down vote

favorite












With the following code:



[
%Quizz
question: "L'unità d' Italia",
answers: [
%answer: 1900, result: false,
%answer: 1861, result: true,
%answer: 1848, result: false,
]
,
%Quizz
question: "La Rivoluzione Francese",
answers: [
%answer: 1789, result: true,
%answer: 1818, result: false,
%answer: 1766, result: false,
]
,
%Quizz
question: "La scoperta dell'America",
answers: [
%answer: 1280, result: false,
%answer: 1500, result: false,
%answer: 1492, result: true,
]

]


I would like to shuffle the 3 quizz structs using Enum.shuffle and shuffle the list of answers for each struct. I am able to shuffle the list but I'm struggling to updated the answers list for each struct. How can I do that?










share|improve this question



























    up vote
    1
    down vote

    favorite












    With the following code:



    [
    %Quizz
    question: "L'unità d' Italia",
    answers: [
    %answer: 1900, result: false,
    %answer: 1861, result: true,
    %answer: 1848, result: false,
    ]
    ,
    %Quizz
    question: "La Rivoluzione Francese",
    answers: [
    %answer: 1789, result: true,
    %answer: 1818, result: false,
    %answer: 1766, result: false,
    ]
    ,
    %Quizz
    question: "La scoperta dell'America",
    answers: [
    %answer: 1280, result: false,
    %answer: 1500, result: false,
    %answer: 1492, result: true,
    ]

    ]


    I would like to shuffle the 3 quizz structs using Enum.shuffle and shuffle the list of answers for each struct. I am able to shuffle the list but I'm struggling to updated the answers list for each struct. How can I do that?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      With the following code:



      [
      %Quizz
      question: "L'unità d' Italia",
      answers: [
      %answer: 1900, result: false,
      %answer: 1861, result: true,
      %answer: 1848, result: false,
      ]
      ,
      %Quizz
      question: "La Rivoluzione Francese",
      answers: [
      %answer: 1789, result: true,
      %answer: 1818, result: false,
      %answer: 1766, result: false,
      ]
      ,
      %Quizz
      question: "La scoperta dell'America",
      answers: [
      %answer: 1280, result: false,
      %answer: 1500, result: false,
      %answer: 1492, result: true,
      ]

      ]


      I would like to shuffle the 3 quizz structs using Enum.shuffle and shuffle the list of answers for each struct. I am able to shuffle the list but I'm struggling to updated the answers list for each struct. How can I do that?










      share|improve this question















      With the following code:



      [
      %Quizz
      question: "L'unità d' Italia",
      answers: [
      %answer: 1900, result: false,
      %answer: 1861, result: true,
      %answer: 1848, result: false,
      ]
      ,
      %Quizz
      question: "La Rivoluzione Francese",
      answers: [
      %answer: 1789, result: true,
      %answer: 1818, result: false,
      %answer: 1766, result: false,
      ]
      ,
      %Quizz
      question: "La scoperta dell'America",
      answers: [
      %answer: 1280, result: false,
      %answer: 1500, result: false,
      %answer: 1492, result: true,
      ]

      ]


      I would like to shuffle the 3 quizz structs using Enum.shuffle and shuffle the list of answers for each struct. I am able to shuffle the list but I'm struggling to updated the answers list for each struct. How can I do that?







      elixir






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 8:08









      Sheharyar

      43.4k10106158




      43.4k10106158










      asked Nov 11 at 15:19









      Sandro Palmieri

      3112518




      3112518






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You can shuffle the list and then map over it, replacing each answers field with a shuffled version of it



          Enum.shuffle(list_of_quizzes)
          |> Enum.map(fn(%answers: answers = quizz) ->
          %quizz
          end)





          share|improve this answer



























            up vote
            2
            down vote













            I would go with Kernel.SpecialForms.for/1 comprehension:



            for quizz <- Enum.shuffle(quizzes),
            do: %Quizzquizz


            or, other way round:



            for quizz <- Enum.shuffle(quizzes),
            answers = Enum.shuffle(quizz.answers),
            do: %Quizzquizz





            share|improve this answer





























              up vote
              1
              down vote













              You can also use Map.update!/3 or Kernel.update_in/3:





              quiz_list
              |> Enum.shuffle
              |> Enum.map(fn q -> Map.update!(q, :answers, &Enum.shuffle/1) end)





              share|improve this answer




















                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                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',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                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%2fstackoverflow.com%2fquestions%2f53250145%2fshuffle-lists-in-structs-nested-in-another-list%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                4
                down vote



                accepted










                You can shuffle the list and then map over it, replacing each answers field with a shuffled version of it



                Enum.shuffle(list_of_quizzes)
                |> Enum.map(fn(%answers: answers = quizz) ->
                %quizz
                end)





                share|improve this answer
























                  up vote
                  4
                  down vote



                  accepted










                  You can shuffle the list and then map over it, replacing each answers field with a shuffled version of it



                  Enum.shuffle(list_of_quizzes)
                  |> Enum.map(fn(%answers: answers = quizz) ->
                  %quizz
                  end)





                  share|improve this answer






















                    up vote
                    4
                    down vote



                    accepted







                    up vote
                    4
                    down vote



                    accepted






                    You can shuffle the list and then map over it, replacing each answers field with a shuffled version of it



                    Enum.shuffle(list_of_quizzes)
                    |> Enum.map(fn(%answers: answers = quizz) ->
                    %quizz
                    end)





                    share|improve this answer












                    You can shuffle the list and then map over it, replacing each answers field with a shuffled version of it



                    Enum.shuffle(list_of_quizzes)
                    |> Enum.map(fn(%answers: answers = quizz) ->
                    %quizz
                    end)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 11 at 17:09









                    m3characters

                    1,5142815




                    1,5142815






















                        up vote
                        2
                        down vote













                        I would go with Kernel.SpecialForms.for/1 comprehension:



                        for quizz <- Enum.shuffle(quizzes),
                        do: %Quizzquizz


                        or, other way round:



                        for quizz <- Enum.shuffle(quizzes),
                        answers = Enum.shuffle(quizz.answers),
                        do: %Quizzquizz





                        share|improve this answer


























                          up vote
                          2
                          down vote













                          I would go with Kernel.SpecialForms.for/1 comprehension:



                          for quizz <- Enum.shuffle(quizzes),
                          do: %Quizzquizz


                          or, other way round:



                          for quizz <- Enum.shuffle(quizzes),
                          answers = Enum.shuffle(quizz.answers),
                          do: %Quizzquizz





                          share|improve this answer
























                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            I would go with Kernel.SpecialForms.for/1 comprehension:



                            for quizz <- Enum.shuffle(quizzes),
                            do: %Quizzquizz


                            or, other way round:



                            for quizz <- Enum.shuffle(quizzes),
                            answers = Enum.shuffle(quizz.answers),
                            do: %Quizzquizz





                            share|improve this answer














                            I would go with Kernel.SpecialForms.for/1 comprehension:



                            for quizz <- Enum.shuffle(quizzes),
                            do: %Quizzquizz


                            or, other way round:



                            for quizz <- Enum.shuffle(quizzes),
                            answers = Enum.shuffle(quizz.answers),
                            do: %Quizzquizz






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 12 at 15:22

























                            answered Nov 12 at 15:15









                            Aleksei Matiushkin

                            77.5k95088




                            77.5k95088




















                                up vote
                                1
                                down vote













                                You can also use Map.update!/3 or Kernel.update_in/3:





                                quiz_list
                                |> Enum.shuffle
                                |> Enum.map(fn q -> Map.update!(q, :answers, &Enum.shuffle/1) end)





                                share|improve this answer
























                                  up vote
                                  1
                                  down vote













                                  You can also use Map.update!/3 or Kernel.update_in/3:





                                  quiz_list
                                  |> Enum.shuffle
                                  |> Enum.map(fn q -> Map.update!(q, :answers, &Enum.shuffle/1) end)





                                  share|improve this answer






















                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    You can also use Map.update!/3 or Kernel.update_in/3:





                                    quiz_list
                                    |> Enum.shuffle
                                    |> Enum.map(fn q -> Map.update!(q, :answers, &Enum.shuffle/1) end)





                                    share|improve this answer












                                    You can also use Map.update!/3 or Kernel.update_in/3:





                                    quiz_list
                                    |> Enum.shuffle
                                    |> Enum.map(fn q -> Map.update!(q, :answers, &Enum.shuffle/1) end)






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 12 at 8:06









                                    Sheharyar

                                    43.4k10106158




                                    43.4k10106158



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • 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.

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





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • 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.

                                        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%2fstackoverflow.com%2fquestions%2f53250145%2fshuffle-lists-in-structs-nested-in-another-list%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







                                        這個網誌中的熱門文章

                                        How to read a connectionString WITH PROVIDER in .NET Core?

                                        Node.js Script on GitHub Pages or Amazon S3

                                        Museum of Modern and Contemporary Art of Trento and Rovereto