def using_optional[A <: AutoCloseable, B](opt: Option[A])(f: Option[A] => B): B = { try { f(opt) } finally {
opt match { case Some(a) if a != null => a.close() case _ =>
}
}
}
/* integers */
privateval small_int = 10000 privatelazyval small_int_table = { val array = new Array[String](small_int) for (i <- 0 until small_int) array(i) = i.toString
array
}
def is_small_int(s: String): Boolean = { val len = s.length
1 <= len && len <= 4 &&
s.forall(c => '0' <= c && c <= '9') &&
(len == 1 || s(0) != '0')
}
def signed_string_of_long(i: Long): String = if (0 <= i && i < small_int) small_int_table(i.toInt) else i.toString
def signed_string_of_int(i: Int): String = if (0 <= i && i < small_int) small_int_table(i) else i.toString
/* separated chunks */
def separate[A](s: A, list: List[A]): List[A] = { val result = new mutable.ListBuffer[A] var first = true for (x <- list) { if (first) {
first = false
result += x
} else {
result += s
result += x
}
}
result.toList
}
def length: Int = end - start def charAt(i: Int): Char = text.charAt(end - i - 1)
def subSequence(i: Int, j: Int): CharSequence = if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) elsethrownew IndexOutOfBoundsException
overridedef toString: String =
string_builder(hint = length) { buf => for (i <- 0 until length) buf.append(charAt(i)) }
}
class Line_Termination(text: CharSequence) extends CharSequence { def length: Int = text.length + 1 def charAt(i: Int): Char = if (i == text.length) '\n'else text.charAt(i) def subSequence(i: Int, j: Int): CharSequence = if (j == text.length + 1) new Line_Termination(text.subSequence(i, j - 1)) else text.subSequence(i, j) overridedef toString: String = text.toString + "\n"
}
def distinct[A](xs: List[A], eq: (A, A) => Boolean = (x: A, y: A) => x == y): List[A] = { val result = new mutable.ListBuffer[A]
xs.foreach(x => if (!result.exists(y => eq(x, y))) result += x)
result.toList
}
def duplicates[A](lst: List[A], eq: (A, A) => Boolean = (x: A, y: A) => x == y): List[A] = { val result = new mutable.ListBuffer[A]
@tailrec def dups(rest: List[A]): Unit =
rest match { case Nil => case x :: xs => if (!result.exists(y => eq(x, y)) && xs.exists(y => eq(x, y))) result += x
dups(xs)
}
dups(lst)
result.toList
}
def replicate[A](n: Int, a: A): List[A] = if (n < 0) thrownew IllegalArgumentException elseif (n == 0) Nil else { val res = new mutable.ListBuffer[A]
(1 to n).foreach(_ => res += a)
res.toList
}
def the_single[A](xs: List[A], message: => String = "Single argument expected"): A =
xs match { case List(x) => x case _ => error(message)
}
/* proper values */
def proper_bool(b: Boolean): Option[Boolean] = if (!b) None else Some(b)
def proper_string(s: String): Option[String] = if (s == null || s == "") None else Some(s)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.