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?
elixir
add a comment |
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?
elixir
add a comment |
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?
elixir
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
elixir
edited Nov 12 at 8:08
Sheharyar
43.3k10106158
43.3k10106158
asked Nov 11 at 15:19
Sandro Palmieri
3112518
3112518
add a comment |
add a comment |
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 | answers: Enum.shuffle(answers)}
end)
add a comment |
up vote
2
down vote
I would go with Kernel.SpecialForms.for/1
comprehension:
for quizz <- Enum.shuffle(quizzes),
do: %Quizz{quizz | answers: Enum.shuffle(quizz.answers)}
or, other way round:
for quizz <- Enum.shuffle(quizzes),
answers = Enum.shuffle(quizz.answers),
do: %Quizz{quizz | answers: answers}
add a comment |
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)
add a comment |
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 | answers: Enum.shuffle(answers)}
end)
add a comment |
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 | answers: Enum.shuffle(answers)}
end)
add a comment |
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 | answers: Enum.shuffle(answers)}
end)
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 | answers: Enum.shuffle(answers)}
end)
answered Nov 11 at 17:09
m3characters
1,4942815
1,4942815
add a comment |
add a comment |
up vote
2
down vote
I would go with Kernel.SpecialForms.for/1
comprehension:
for quizz <- Enum.shuffle(quizzes),
do: %Quizz{quizz | answers: Enum.shuffle(quizz.answers)}
or, other way round:
for quizz <- Enum.shuffle(quizzes),
answers = Enum.shuffle(quizz.answers),
do: %Quizz{quizz | answers: answers}
add a comment |
up vote
2
down vote
I would go with Kernel.SpecialForms.for/1
comprehension:
for quizz <- Enum.shuffle(quizzes),
do: %Quizz{quizz | answers: Enum.shuffle(quizz.answers)}
or, other way round:
for quizz <- Enum.shuffle(quizzes),
answers = Enum.shuffle(quizz.answers),
do: %Quizz{quizz | answers: answers}
add a comment |
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: %Quizz{quizz | answers: Enum.shuffle(quizz.answers)}
or, other way round:
for quizz <- Enum.shuffle(quizzes),
answers = Enum.shuffle(quizz.answers),
do: %Quizz{quizz | answers: answers}
I would go with Kernel.SpecialForms.for/1
comprehension:
for quizz <- Enum.shuffle(quizzes),
do: %Quizz{quizz | answers: Enum.shuffle(quizz.answers)}
or, other way round:
for quizz <- Enum.shuffle(quizzes),
answers = Enum.shuffle(quizz.answers),
do: %Quizz{quizz | answers: answers}
edited Nov 12 at 15:22
answered Nov 12 at 15:15
Aleksei Matiushkin
77.3k95088
77.3k95088
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 12 at 8:06
Sheharyar
43.3k10106158
43.3k10106158
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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