Home > front end >  Scala no longer supports List.make, error "value make is not a member of object List"
Scala no longer supports List.make, error "value make is not a member of object List"

Time:11-23

When going thru the example http://aperiodic.net/phil/scala/s-99/p15.scala

// P15 (**) Duplicate the elements of a list a given number of times.
//     Example:
//     scala> duplicateN(3, List('a, 'b, 'c, 'c, 'd))
//     res0: List[Symbol] = List('a, 'a, 'a, 'b, 'b, 'b, 'c, 'c, 'c, 'c, 'c, 'c, 'd, 'd, 'd)

object P15 {
  def duplicateN[A](n: Int, ls: List[A]): List[A] =
    ls flatMap { List.make(n, _) }
}

the line

ls flatMap { List.make(n, _) }

produces the compiler error

"value make is not a member of object List"

Is there another way to rewrite this?

CodePudding user response:

make was deprecated in 2.8.0 and removed in 2.11.x.

You can use

ls flatMap { List.fill(n)(_) }
  • Related