Skip to content

Commit a332a39

Browse files
committed
Restored some methods to List.
Deprecated since 2.8.0 but causing far too much inconvenience by not being present.
1 parent 5c599a3 commit a332a39

File tree

1 file changed

+247
-0
lines changed

1 file changed

+247
-0
lines changed

src/library/scala/collection/immutable/List.scala

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ sealed abstract class List[+A] extends AbstractSeq[A]
277277
override def toStream : Stream[A] =
278278
if (isEmpty) Stream.Empty
279279
else new Stream.Cons(head, tail.toStream)
280+
281+
@deprecated("use `distinct` instead", "2.8.0")
282+
def removeDuplicates: List[A] = distinct
280283
}
281284

282285
/** The empty list.
@@ -343,6 +346,8 @@ final case class ::[B](private var hd: B, private[scala] var tl: List[B]) extend
343346
*/
344347
object List extends SeqFactory[List] {
345348

349+
import scala.collection.{Iterable, Seq, IndexedSeq}
350+
346351
/** $genericCanBuildFromInfo */
347352
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, List[A]] =
348353
ReusableCBF.asInstanceOf[GenericCanBuildFrom[A]]
@@ -352,6 +357,248 @@ object List extends SeqFactory[List] {
352357
override def empty[A]: List[A] = Nil
353358

354359
override def apply[A](xs: A*): List[A] = xs.toList
360+
361+
/** Create a sorted list with element values `v,,>n+1,, = step(v,,n,,)`
362+
* where `v,,0,, = start` and elements are in the range between `start`
363+
* (inclusive) and `end` (exclusive).
364+
*
365+
* @param start the start value of the list
366+
* @param end the end value of the list
367+
* @param step the increment function of the list, which given `v,,n,,`,
368+
* computes `v,,n+1,,`. Must be monotonically increasing
369+
* or decreasing.
370+
* @return the sorted list of all integers in range `[start;end)`.
371+
*/
372+
@deprecated("use `iterate` instead", "2.8.0")
373+
def range(start: Int, end: Int, step: Int => Int): List[Int] = {
374+
val up = step(start) > start
375+
val down = step(start) < start
376+
val b = new ListBuffer[Int]
377+
var i = start
378+
while ((!up || i < end) && (!down || i > end)) {
379+
b += i
380+
val next = step(i)
381+
if (i == next)
382+
throw new IllegalArgumentException("the step function did not make any progress on "+ i)
383+
i = next
384+
}
385+
b.toList
386+
}
387+
388+
/** Create a list containing several copies of an element.
389+
*
390+
* @param n the length of the resulting list
391+
* @param elem the element composing the resulting list
392+
* @return a list composed of `n` elements all equal to `elem`
393+
*/
394+
@deprecated("use `fill` instead", "2.8.0")
395+
def make[A](n: Int, elem: A): List[A] = {
396+
val b = new ListBuffer[A]
397+
var i = 0
398+
while (i < n) {
399+
b += elem
400+
i += 1
401+
}
402+
b.toList
403+
}
404+
405+
/** Concatenate all the elements of a given list of lists.
406+
*
407+
* @param xss the list of lists that are to be concatenated
408+
* @return the concatenation of all the lists
409+
*/
410+
@deprecated("use `xss.flatten` instead of `List.flatten(xss)`", "2.8.0")
411+
def flatten[A](xss: List[List[A]]): List[A] = {
412+
val b = new ListBuffer[A]
413+
for (xs <- xss) {
414+
var xc = xs
415+
while (!xc.isEmpty) {
416+
b += xc.head
417+
xc = xc.tail
418+
}
419+
}
420+
b.toList
421+
}
422+
423+
/** Transforms a list of pairs into a pair of lists.
424+
*
425+
* @param xs the list of pairs to unzip
426+
* @return a pair of lists.
427+
*/
428+
@deprecated("use `xs.unzip` instead of `List.unzip(xs)`", "2.8.0")
429+
def unzip[A,B](xs: List[(A,B)]): (List[A], List[B]) = {
430+
val b1 = new ListBuffer[A]
431+
val b2 = new ListBuffer[B]
432+
var xc = xs
433+
while (!xc.isEmpty) {
434+
b1 += xc.head._1
435+
b2 += xc.head._2
436+
xc = xc.tail
437+
}
438+
(b1.toList, b2.toList)
439+
}
440+
441+
/** Transforms an iterable of pairs into a pair of lists.
442+
*
443+
* @param xs the iterable of pairs to unzip
444+
* @return a pair of lists.
445+
*/
446+
@deprecated("use `xs.unzip` instead of `List.unzip(xs)`", "2.8.0")
447+
def unzip[A,B](xs: Iterable[(A,B)]): (List[A], List[B]) =
448+
xs.foldRight[(List[A], List[B])]((Nil, Nil)) {
449+
case ((x, y), (xs, ys)) => (x :: xs, y :: ys)
450+
}
451+
452+
/**
453+
* Returns the `Left` values in the given `Iterable` of `Either`s.
454+
*/
455+
@deprecated("use `xs collect { case Left(x: A) => x }` instead of `List.lefts(xs)`", "2.8.0")
456+
def lefts[A, B](es: Iterable[Either[A, B]]) =
457+
es.foldRight[List[A]](Nil)((e, as) => e match {
458+
case Left(a) => a :: as
459+
case Right(_) => as
460+
})
461+
462+
/**
463+
* Returns the `Right` values in the given `Iterable` of `Either`s.
464+
*/
465+
@deprecated("use `xs collect { case Right(x: B) => x }` instead of `List.rights(xs)`", "2.8.0")
466+
def rights[A, B](es: Iterable[Either[A, B]]) =
467+
es.foldRight[List[B]](Nil)((e, bs) => e match {
468+
case Left(_) => bs
469+
case Right(b) => b :: bs
470+
})
471+
472+
/** Transforms an Iterable of Eithers into a pair of lists.
473+
*
474+
* @param xs the iterable of Eithers to separate
475+
* @return a pair of lists.
476+
*/
477+
@deprecated("use `(for (Left(x) <- es) yield x, for (Right(x) <- es) yield x)` instead", "2.8.0")
478+
def separate[A,B](es: Iterable[Either[A, B]]): (List[A], List[B]) =
479+
es.foldRight[(List[A], List[B])]((Nil, Nil)) {
480+
case (Left(a), (lefts, rights)) => (a :: lefts, rights)
481+
case (Right(b), (lefts, rights)) => (lefts, b :: rights)
482+
}
483+
484+
/** Converts an iterator to a list.
485+
*
486+
* @param it the iterator to convert
487+
* @return a list that contains the elements returned by successive
488+
* calls to `it.next`
489+
*/
490+
@deprecated("use `it.toList` instead of `List.toList(it)`", "2.8.0")
491+
def fromIterator[A](it: Iterator[A]): List[A] = it.toList
492+
493+
/** Converts an array into a list.
494+
*
495+
* @param arr the array to convert
496+
* @return a list that contains the same elements than `arr`
497+
* in the same order
498+
*/
499+
@deprecated("use `array.toList` instead of `List.fromArray(array)`", "2.8.0")
500+
def fromArray[A](arr: Array[A]): List[A] = fromArray(arr, 0, arr.length)
501+
502+
/** Converts a range of an array into a list.
503+
*
504+
* @param arr the array to convert
505+
* @param start the first index to consider
506+
* @param len the length of the range to convert
507+
* @return a list that contains the same elements than `arr`
508+
* in the same order
509+
*/
510+
@deprecated("use `array.view(start, end).toList` instead of `List.fromArray(array, start, end)`", "2.8.0")
511+
def fromArray[A](arr: Array[A], start: Int, len: Int): List[A] = {
512+
var res: List[A] = Nil
513+
var i = start + len
514+
while (i > start) {
515+
i -= 1
516+
res = arr(i) :: res
517+
}
518+
res
519+
}
520+
521+
/** Returns the list resulting from applying the given function `f`
522+
* to corresponding elements of the argument lists.
523+
*
524+
* @param f function to apply to each pair of elements.
525+
* @return `[f(a,,0,,,b,,0,,), ..., f(a,,n,,,b,,n,,)]` if the lists are
526+
* `[a,,0,,, ..., a,,k,,]`, `[b,,0,,, ..., b,,l,,]` and
527+
* `n = min(k,l)`
528+
*/
529+
@deprecated("use `(xs, ys).zipped.map(f)` instead of `List.map2(xs, ys)(f)`", "2.8.0")
530+
def map2[A,B,C](xs: List[A], ys: List[B])(f: (A, B) => C): List[C] = {
531+
val b = new ListBuffer[C]
532+
var xc = xs
533+
var yc = ys
534+
while (!xc.isEmpty && !yc.isEmpty) {
535+
b += f(xc.head, yc.head)
536+
xc = xc.tail
537+
yc = yc.tail
538+
}
539+
b.toList
540+
}
541+
542+
/** Tests whether the given predicate `p` holds
543+
* for all corresponding elements of the argument lists.
544+
*
545+
* @param p function to apply to each pair of elements.
546+
* @return `(p(a<sub>0</sub>,b<sub>0</sub>) &amp;&amp;
547+
* ... &amp;&amp; p(a<sub>n</sub>,b<sub>n</sub>))]`
548+
* if the lists are `[a<sub>0</sub>, ..., a<sub>k</sub>]`;
549+
* `[b<sub>0</sub>, ..., b<sub>l</sub>]`
550+
* and `n = min(k,l)`
551+
*/
552+
@deprecated("use `(xs, ys).zipped.forall(f)` instead of `List.forall2(xs, ys)(f)`", "2.8.0")
553+
def forall2[A,B](xs: List[A], ys: List[B])(f: (A, B) => Boolean): Boolean = {
554+
var xc = xs
555+
var yc = ys
556+
while (!xc.isEmpty && !yc.isEmpty) {
557+
if (!f(xc.head, yc.head)) return false
558+
xc = xc.tail
559+
yc = yc.tail
560+
}
561+
true
562+
}
563+
564+
/** Tests whether the given predicate `p` holds
565+
* for some corresponding elements of the argument lists.
566+
*
567+
* @param p function to apply to each pair of elements.
568+
* @return `n != 0 &amp;&amp; (p(a<sub>0</sub>,b<sub>0</sub>) ||
569+
* ... || p(a<sub>n</sub>,b<sub>n</sub>))]` if the lists are
570+
* `[a<sub>0</sub>, ..., a<sub>k</sub>]`,
571+
* `[b<sub>0</sub>, ..., b<sub>l</sub>]` and
572+
* `n = min(k,l)`
573+
*/
574+
@deprecated("use `(xs, ys).zipped.exists(f)` instead of `List.exists2(xs, ys)(f)`", "2.8.0")
575+
def exists2[A,B](xs: List[A], ys: List[B])(f: (A, B) => Boolean): Boolean = {
576+
var xc = xs
577+
var yc = ys
578+
while (!xc.isEmpty && !yc.isEmpty) {
579+
if (f(xc.head, yc.head)) return true
580+
xc = xc.tail
581+
yc = yc.tail
582+
}
583+
false
584+
}
585+
586+
/** Transposes a list of lists.
587+
* pre: All element lists have the same length.
588+
*
589+
* @param xss the list of lists
590+
* @return the transposed list of lists
591+
*/
592+
@deprecated("use `xss.transpose` instead of `List.transpose(xss)`", "2.8.0")
593+
def transpose[A](xss: List[List[A]]): List[List[A]] = {
594+
val buf = new ListBuffer[List[A]]
595+
var yss = xss
596+
while (!yss.head.isEmpty) {
597+
buf += (yss map (_.head))
598+
yss = (yss map (_.tail))
599+
}
600+
buf.toList
601+
}
355602
}
356603

357604
/** Only used for list serialization */

0 commit comments

Comments
 (0)