How to create a two dimensional list using for loop in Scala?
up vote
0
down vote
favorite
If I write the following code in scala I get one dimensional list, as such:
scala> for (a <- (1 to 2).toList; b <- (1 to 3).toList) yield (a, b)
res1 = List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))
But I'm expecting :-
List(List((1,1), (1,2), (1,3)), List((2,1), (2,2), (2,3)))
Is it possible to do this using a for loop in scala or is some kind of other construct needed?
scala
New contributor
add a comment |
up vote
0
down vote
favorite
If I write the following code in scala I get one dimensional list, as such:
scala> for (a <- (1 to 2).toList; b <- (1 to 3).toList) yield (a, b)
res1 = List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))
But I'm expecting :-
List(List((1,1), (1,2), (1,3)), List((2,1), (2,2), (2,3)))
Is it possible to do this using a for loop in scala or is some kind of other construct needed?
scala
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
If I write the following code in scala I get one dimensional list, as such:
scala> for (a <- (1 to 2).toList; b <- (1 to 3).toList) yield (a, b)
res1 = List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))
But I'm expecting :-
List(List((1,1), (1,2), (1,3)), List((2,1), (2,2), (2,3)))
Is it possible to do this using a for loop in scala or is some kind of other construct needed?
scala
New contributor
If I write the following code in scala I get one dimensional list, as such:
scala> for (a <- (1 to 2).toList; b <- (1 to 3).toList) yield (a, b)
res1 = List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))
But I'm expecting :-
List(List((1,1), (1,2), (1,3)), List((2,1), (2,2), (2,3)))
Is it possible to do this using a for loop in scala or is some kind of other construct needed?
scala
scala
New contributor
New contributor
edited Nov 10 at 17:31
New contributor
asked Nov 10 at 16:04
Joe Johnes
33
33
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
You could do it in 2 for comprehensions:
for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))
1
While this is the correct answer, I would like to point out that since in scalafor/yield
is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls tomap
,flatMap
&filter
, @Tom's answer is also correct.
– Luis Miguel Mejía Suárez
Nov 10 at 17:48
add a comment |
up vote
1
down vote
You could also use map
(1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))
add a comment |
up vote
0
down vote
{for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList
You get a List
of Vector
, but that's fine, Vector
is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList
until the end. If you are OK with Vector
Tom's answer is really good and you can just refactor down to:
(1 to 4).map(n => ('a' to 'c').map(m => (n, m)))
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You could do it in 2 for comprehensions:
for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))
1
While this is the correct answer, I would like to point out that since in scalafor/yield
is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls tomap
,flatMap
&filter
, @Tom's answer is also correct.
– Luis Miguel Mejía Suárez
Nov 10 at 17:48
add a comment |
up vote
1
down vote
accepted
You could do it in 2 for comprehensions:
for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))
1
While this is the correct answer, I would like to point out that since in scalafor/yield
is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls tomap
,flatMap
&filter
, @Tom's answer is also correct.
– Luis Miguel Mejía Suárez
Nov 10 at 17:48
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You could do it in 2 for comprehensions:
for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))
You could do it in 2 for comprehensions:
for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))
answered Nov 10 at 16:25
Harald
3,71221727
3,71221727
1
While this is the correct answer, I would like to point out that since in scalafor/yield
is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls tomap
,flatMap
&filter
, @Tom's answer is also correct.
– Luis Miguel Mejía Suárez
Nov 10 at 17:48
add a comment |
1
While this is the correct answer, I would like to point out that since in scalafor/yield
is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls tomap
,flatMap
&filter
, @Tom's answer is also correct.
– Luis Miguel Mejía Suárez
Nov 10 at 17:48
1
1
While this is the correct answer, I would like to point out that since in scala
for/yield
is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls to map
, flatMap
& filter
, @Tom's answer is also correct.– Luis Miguel Mejía Suárez
Nov 10 at 17:48
While this is the correct answer, I would like to point out that since in scala
for/yield
is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls to map
, flatMap
& filter
, @Tom's answer is also correct.– Luis Miguel Mejía Suárez
Nov 10 at 17:48
add a comment |
up vote
1
down vote
You could also use map
(1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))
add a comment |
up vote
1
down vote
You could also use map
(1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))
add a comment |
up vote
1
down vote
up vote
1
down vote
You could also use map
(1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))
You could also use map
(1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))
answered Nov 10 at 17:31
Tom
1,6042924
1,6042924
add a comment |
add a comment |
up vote
0
down vote
{for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList
You get a List
of Vector
, but that's fine, Vector
is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList
until the end. If you are OK with Vector
Tom's answer is really good and you can just refactor down to:
(1 to 4).map(n => ('a' to 'c').map(m => (n, m)))
add a comment |
up vote
0
down vote
{for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList
You get a List
of Vector
, but that's fine, Vector
is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList
until the end. If you are OK with Vector
Tom's answer is really good and you can just refactor down to:
(1 to 4).map(n => ('a' to 'c').map(m => (n, m)))
add a comment |
up vote
0
down vote
up vote
0
down vote
{for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList
You get a List
of Vector
, but that's fine, Vector
is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList
until the end. If you are OK with Vector
Tom's answer is really good and you can just refactor down to:
(1 to 4).map(n => ('a' to 'c').map(m => (n, m)))
{for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList
You get a List
of Vector
, but that's fine, Vector
is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList
until the end. If you are OK with Vector
Tom's answer is really good and you can just refactor down to:
(1 to 4).map(n => ('a' to 'c').map(m => (n, m)))
answered Nov 10 at 18:07
Daniel Hinojosa
78237
78237
add a comment |
add a comment |
Joe Johnes is a new contributor. Be nice, and check out our Code of Conduct.
Joe Johnes is a new contributor. Be nice, and check out our Code of Conduct.
Joe Johnes is a new contributor. Be nice, and check out our Code of Conduct.
Joe Johnes is a new contributor. Be nice, and check out our Code of Conduct.
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%2f53240777%2fhow-to-create-a-two-dimensional-list-using-for-loop-in-scala%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