aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/core/Decorators.scala
blob: d0ce3efe029013e23efa3f9fd048ce02c9380265 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package dotty.tools.dotc
package core

import annotation.tailrec
import Symbols._

import Contexts._, Names._

object Decorators {

  implicit class toTypeNameDecorator(val s: String) extends AnyVal {
    def toTypeName(implicit context: Context): TypeName =
      context.names.newTypeName(s)
  }

  implicit class toTermNameDecorator(val s: String) extends AnyVal {
    def toTermName(implicit context: Context): TermName =
      context.names.newTermName(s)
  }

  implicit class SymbolIteratorDecorator(val it: Iterator[Symbol]) extends AnyVal {
    final def findSymbol(p: Symbol => Boolean): Symbol = {
      while (it.hasNext) {
        val sym = it.next
        if (p(sym)) return sym
      }
      NoSymbol
    }
  }

  final val MaxRecursions = 1000

  implicit class ListDecorator[T](val xs: List[T]) extends AnyVal {
    def filterConserve(p: T => Boolean): List[T] = {
      def loop(xs: List[T], nrec: Int): List[T] = xs match {
        case Nil => xs
        case x :: xs1 =>
          if (nrec < MaxRecursions) {
            val ys1 = loop(xs1, nrec + 1)
            if (p(x))
              if (ys1 eq xs1) xs else x :: ys1
            else
              ys1
          } else xs filter p
      }
      loop(xs, 0)
    }
  }
}