Beginner Question: Given a list repeat each element of the list n times

646 views
Skip to first unread message

Harit Himanshu

unread,
Jun 9, 2015, 12:15:25 AM6/9/15
to scala...@googlegroups.com
Hello

The method signature looks like  
def f(num:Int,arr:List[Int]):List[Int] = __________________
so my solution(which works) looks like

def f(num:Int,arr:List[Int]):List[Int] = arr flatMap(e => for(i <- 1 to num) yield e)

I am sure there is better way to write for(i <- 1 to n), just now sure, what that is

Any recommendations?

Thank you

Clint Gilbert

unread,
Jun 9, 2015, 12:38:27 AM6/9/15
to scala...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You could do

for(_ <- 1 to num) yield e

or the equivalent

(1 to num).map(_ => e)

The '_' here means you don't care about the element of the range, and
aren't giving it a name.

Note that if you have a String, you can repeat it with '*':

scala> val s = "a"
s: String = a

scala> s * 5
res3: String = aaaaa

scala> val s = "xyz"
s: String = xyz

scala> s * 5
res4: String = xyzxyzxyzxyzxyz

That's cute, but not useful very often.
> -- You received this message because you are subscribed to the
> Google Groups "scala-user" group. To unsubscribe from this group
> and stop receiving emails from it, send an email to
> scala-user+...@googlegroups.com
> <mailto:scala-user+...@googlegroups.com>. For more options,
> visit https://groups.google.com/d/optout.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)

iEYEARECAAYFAlV2bboACgkQ0GFaTS4nYxtMBACgsraMSGBqotU3AuFLn7W+VTHj
tMgAoLFlqUG+PZAt1RHsluCOqmRDi8cs
=v4kT
-----END PGP SIGNATURE-----

Harit Himanshu

unread,
Jun 9, 2015, 1:24:53 AM6/9/15
to Clint Gilbert, scala...@googlegroups.com
interesting. Thanks Clint. I guess using '_' is a good idea in this problem

Thank you


--
You received this message because you are subscribed to a topic in the Google Groups "scala-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-user/tlqh5mzTSxE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-user+...@googlegroups.com.

Harald Meland

unread,
Jun 9, 2015, 3:18:10 AM6/9/15
to Harit Himanshu, scala...@googlegroups.com
On Tue, Jun 9, 2015 at 6:15 AM, Harit Himanshu <harit.sub...@gmail.com> wrote:
I am sure there is better way to write for(i <- 1 to n), just now sure, what that is

I think you might be looking for List.fill(n)(e)
--
Harald

Harit Himanshu

unread,
Jun 9, 2015, 9:04:54 AM6/9/15
to scala...@googlegroups.com, harit.sub...@gmail.com
I tried that, but it does not keeps the element together, so it breaks the expected behavior

scala> List.fill(n)(l) flatten
warning: there was one feature warning; re-run with -feature for details
res10: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2, 3)

Luis Ángel Vicente Sánchez

unread,
Jun 9, 2015, 9:07:10 AM6/9/15
to Harit Himanshu, scala...@googlegroups.com
You should use it this way

l.flatMap(List.fill(n)(_))
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.

Harit Himanshu

unread,
Jun 9, 2015, 10:50:49 AM6/9/15
to Luis Ángel Vicente Sánchez, scala...@googlegroups.com
Oops! Yes that makes lot of sense. looks much better than the code I had

Reply all
Reply to author
Forward
0 new messages