o1.util

package o1.util

Type members

Classlikes

object nice

The contained objects extend various standard API classes with nice convenience methods.

The contained objects extend various standard API classes with nice convenience methods.

Types

A structural supertype for everything that has a close method. This is an alias for java.lang.AutoCloseable.

A structural supertype for everything that has a close method. This is an alias for java.lang.AutoCloseable.

See also:
type Failure[T] = Failure[T]

An alias for convenient use of scala.util.Try via import o1.util.*.

An alias for convenient use of scala.util.Try via import o1.util.*.

type Path = Path

An alias for convenient use of java.nio.file.Path via import o1.util.*.

An alias for convenient use of java.nio.file.Path via import o1.util.*.

type Random = Random

An alias for convenient use of scala.util.Random via import o1.util.*.

An alias for convenient use of scala.util.Random via import o1.util.*.

type Source = Source

An alias for convenient use of scala.io.Source via import o1.util.*.

An alias for convenient use of scala.io.Source via import o1.util.*.

type Success[T] = Success[T]

An alias for convenient use of scala.util.Try via import o1.util.*.

An alias for convenient use of scala.util.Try via import o1.util.*.

type Try[T] = Try[T]

An alias for convenient use of scala.util.Try via import o1.util.*.

An alias for convenient use of scala.util.Try via import o1.util.*.

type URL = URL

An alias for convenient use of java.net.URL via import o1.util.*.

An alias for convenient use of java.net.URL via import o1.util.*.

Value members

Concrete methods

def Path(name: String): Path

Constructs a new Path object from a given string.

Constructs a new Path object from a given string.

def Path(url: URL): Path

Constructs a new Path object from a given string URL.

Constructs a new Path object from a given string URL.

inline def ast(inline expr: Any): String

A macro that returns a string describing the structure (abstract syntax tree) of the given Scala expression.

A macro that returns a string describing the structure (abstract syntax tree) of the given Scala expression.

Searches the working directory and, failing that, the classpath for a readable file or folder with the given relative path. Returns the Path in an Option wrapper.

Searches the working directory and, failing that, the classpath for a readable file or folder with the given relative path. Returns the Path in an Option wrapper.

def forEachLine(source: Source)(effect: String => Unit): Unit

Calls getLines on the given Source and performs the specified effect on each of the lines. Calls close() on the source even if an exception occurs, but doesn’t catch any exceptions.

Calls getLines on the given Source and performs the specified effect on each of the lines. Calls close() on the source even if an exception occurs, but doesn’t catch any exceptions.

def ifImplemented[Result](computation: => Result): Option[Result]

Performs a given computation and checks to see if it crashes with a NotImplementedError. Returns the result of the computation in an Option; returns None only in the computation wasn’t implemented. Any other exception is thrown.

Performs a given computation and checks to see if it crashes with a NotImplementedError. Returns the result of the computation in an Option; returns None only in the computation wasn’t implemented. Any other exception is thrown.

Type parameters:
Result

the type of the given computation

Value parameters:
computation

a computation that may or may not be implemented

def isImplemented[Result](computation: => Result): Boolean

Performs a given computation and determines whether it crashes with a NotImplementedError. Returns true if it did and false otherwise.

Performs a given computation and determines whether it crashes with a NotImplementedError. Returns true if it did and false otherwise.

Type parameters:
Result

the type of the given computation

Value parameters:
computation

a computation that may or may not be implemented

def localFile(resourcePath: String): Option[Path]

Searches the classpath for a file with the given relative path and returns the corresponding Path. The result comes in an Option wrapper in case no such file is accessible.

Searches the classpath for a file with the given relative path and returns the corresponding Path. The result comes in an Option wrapper in case no such file is accessible.

def localSource(resourcePath: String): Option[Source]

Searches the classpath for a file with the given relative path and returns a corresponding Source. The result comes in an Option wrapper in case no such file is accessible. Assumes UTF-8 encoding.

Searches the classpath for a file with the given relative path and returns a corresponding Source. The result comes in an Option wrapper in case no such file is accessible. Assumes UTF-8 encoding.

def localURL(resourcePath: String): Option[URL]

Searches the classpath for a file with the given relative path. Delegates the task to the class loader. Returns the URL of any found file in an Option wrapper.

Searches the classpath for a file with the given relative path. Delegates the task to the class loader. Returns the URL of any found file in an Option wrapper.

def readFileLines(relativePath: String, trimEach: Boolean, excludeIfEmpty: Boolean): Option[Vector[String]]

Reads and returns the lines from a classpath-relative text file. The result comes in an Option wrapper in case no such file is accessible. Calls close() on the file even if an exception occurs, but doesn’t catch any exceptions. Assumes UTF-8 encoding.

Reads and returns the lines from a classpath-relative text file. The result comes in an Option wrapper in case no such file is accessible. Calls close() on the file even if an exception occurs, but doesn’t catch any exceptions. Assumes UTF-8 encoding.

Value parameters:
excludeIfEmpty

whether to exclude (possibly trimmed) empty lines from the return value; if unspecified, defaults to true

relativePath

the path to a resource; relative to a classpath entry

trimEach

whether to remove whitespace around each of the lines; if unspecified, defaults to true

def readLinesFromSource(source: Source, trimEach: Boolean, excludeIfEmpty: Boolean): Vector[String]

Calls getLines on the given Source and returns the lines in a vector. Calls close() on the source even if an exception occurs, but doesn’t catch any exceptions.

Calls getLines on the given Source and returns the lines in a vector. Calls close() on the source even if an exception occurs, but doesn’t catch any exceptions.

Value parameters:
excludeIfEmpty

whether to exclude (possibly trimmed) empty lines from the return value; if unspecified, defaults to true

source

the source to read the lines from

trimEach

whether to remove whitespace around each of the lines; if unspecified, defaults to true

def readTextFile(relativePath: String): Option[String]

Returns, as a single String, the entire contents of a classpath-relative text file. The result comes in an Option wrapper in case no such file is accessible. Calls close() on the file even if an exception occurs, but doesn’t catch any exceptions. Assumes UTF-8 encoding.

Returns, as a single String, the entire contents of a classpath-relative text file. The result comes in an Option wrapper in case no such file is accessible. Calls close() on the file even if an exception occurs, but doesn’t catch any exceptions. Assumes UTF-8 encoding.

Value parameters:
relativePath

the path to a resource; relative to a classpath entry

inline def report(inline exprs: Any*): Unit

A macro that prints out the given expressions. Literals are printed as they are, other expressions as "expr=value". Potentially useful for debugging.

A macro that prints out the given expressions. Literals are printed as they are, other expressions as "expr=value". Potentially useful for debugging.

def tryForSource(pathOrURL: String): Try[Source]

Takes in a path or a URL as a string and tries to construct a corresponding Source. Tries to interpret the given string as one of the following, in order:

Takes in a path or a URL as a string and tries to construct a corresponding Source. Tries to interpret the given string as one of the following, in order:

  1. A resource relative to the classpath (as per localURL).
  2. A complete URL string, with protocol and all (e.g., "http://example.com/").
  3. A URL string with "http://" missing (e.g., "example.com").
  4. A URL string with "https://" missing. Calls scala.io.Source.fromURL on each of those candidates until a source is successfully constructed or all attempts have failed. Assumes UTF-8 encoding.
Value parameters:
pathOrURL

the possible path or URL to a resource

Returns:

the first Success, or a Failure if none of the candidates can be read as a Source

def tryForURL(pathOrURL: String): Try[URL]

Takes in a path or a URL as a string and tries to locate the corresponding resource. Tries to interpret the given string as one of the following (in order until a match is found):

Takes in a path or a URL as a string and tries to locate the corresponding resource. Tries to interpret the given string as one of the following (in order until a match is found):

  1. A resource relative to the classpath (as per localURL).
  2. A complete URL string, with protocol and all (e.g., "http://example.com/").
  3. A URL string with "http://" missing (e.g., "example.com").
Value parameters:
pathOrURL

the possible path or URL to a resource

Returns:

the first Success, or a Failure if the string isn’t even valid for forming a URL

def useAndClose[Resource <: Closeable, Result](resource: Resource)(operation: Resource => Result): Result

Attempts to apply the given operation to the given resource and returns the result, making sure to close the resource. Calls close() on the resource even if the attempt fails with an exception. Doesn’t catch any exceptions. (This in an implementation of the so-called “loan pattern”.) Since Scala 2.13, the functionality is available in the standard API, and this is now just an alias for scala.util.Using.resource.)

Attempts to apply the given operation to the given resource and returns the result, making sure to close the resource. Calls close() on the resource even if the attempt fails with an exception. Doesn’t catch any exceptions. (This in an implementation of the so-called “loan pattern”.) Since Scala 2.13, the functionality is available in the standard API, and this is now just an alias for scala.util.Using.resource.)

Value parameters:
operation

an operation that can be applied to resource

resource

a resource, such as a file, that has a close() method

An alias for convenient access to java.lang.System.getProperty("user.dir") via import o1.util.*.

An alias for convenient access to java.lang.System.getProperty("user.dir") via import o1.util.*.

def writeTextFile(filePath: String, text: String): Unit

Writes the given text in a new text file at the given filePath. Overwrites any existing file there. Encodes the output as UTF-8. Calls close() on the output stream even if an exception occurs, but doesn’t catch any exceptions.

Writes the given text in a new text file at the given filePath. Overwrites any existing file there. Encodes the output as UTF-8. Calls close() on the output stream even if an exception occurs, but doesn’t catch any exceptions.

Value parameters:
filePath

an absolute path to a local file or a path relative to the working directory

Concrete fields

A label that can be used as an empty method body or other “no-op” block.

A label that can be used as an empty method body or other “no-op” block.

An alias for convenient use of scala.math.Ordering.Double.TotalOrdering via import o1.util.*.

An alias for convenient use of scala.math.Ordering.Double.TotalOrdering via import o1.util.*.

val Failure: Failure.type

An alias for convenient use of scala.util.Try via import o1.util.*.

An alias for convenient use of scala.util.Try via import o1.util.*.

val Random: Random.type

An alias for convenient use of scala.util.Random via import o1.util.*.

An alias for convenient use of scala.util.Random via import o1.util.*.

val Source: Source.type

An alias for convenient use of scala.io.Source via import o1.util.*.

An alias for convenient use of scala.io.Source via import o1.util.*.

val Success: Success.type

An alias for convenient use of scala.util.Try via import o1.util.*.

An alias for convenient use of scala.util.Try via import o1.util.*.

val Try: Try.type

An alias for convenient use of scala.util.Try via import o1.util.*.

An alias for convenient use of scala.util.Try via import o1.util.*.

Extensions

Extensions

extension (interpolated: StringContext)
final def r: Regex
extension [Key, Value](self: Map[Key, Value])
final def mapValuesOnly[Result](transform: Value => Result): Map[Key, Result]
extension [T1, T2, Pair <: (T1, T2), Collection <: (Seq)](self: Seq[(T1, T2)])
final inline def firsts: Seq[T1]
final inline def seconds: Seq[T2]
extension [Element](self: Seq[Element])
final def randomElement(): Element
extension [Content](self: Option[Content])
final def tap(effect: Content => Unit): Option[Content]
extension (self: Path)
final def fileName: String
final def isReadable: Boolean
final def listFiles(includeIf: Path => Boolean): Option[Vector[Path]]
final def readLines(trimEach: Boolean, excludeIfEmpty: Boolean): Option[Vector[String]]
extension [T1, T2](self: (T1, T2))
final inline def first: T1
final inline def second: T2
extension [Element, Collection <: (Iterable)](self: Iterable[Element])
final def frequencies: Map[Element, Int]
final inline def ifNonEmpty[Result](compute: Iterable[Element] => Result): Option[Result]
final def log(format: Iterable[Element] => String): Iterable[Element]
final def mapFromID[ID](formID: Element => ID): Map[ID, Element]
final def mapGroups[Key, Value](formKey: Element => Key)(transformGroup: Iterable[Element] => Value): Map[Key, Value]
final def mapTo[Value](formValue: Element => Value): Map[Element, Value]
final def mapify[Key, Value](formKey: Element => Key)(formValue: Element => Value): Map[Key, Value]
final def tap(effect: Element => Unit): Iterable[Element]
final def toLazy: LazyList[Element]
extension (value: Int)
final inline def atLeast(minimum: Int): Int
final inline def atMost(maximum: Int): Int
final inline def clamp(low: Int, high: Int): Int
final inline def isBetween(low: Int, high: Int): Boolean
final inline def isEven: Boolean
final inline def isOdd: Boolean
extension (value: Double)
final inline def atLeast(minimum: Double): Double
final inline def atMost(maximum: Double): Double
final inline def clamp(low: Double, high: Double): Double
final inline def isBetween(low: Double, high: Double): Boolean
extension (value: Float)
final inline def atLeast(minimum: Float): Float
final inline def atMost(maximum: Float): Float
final inline def clamp(low: Float, high: Float): Float
final inline def isBetween(low: Float, high: Float): Boolean
extension (value: Long)
final inline def atLeast(minimum: Long): Long
final inline def atMost(maximum: Long): Long
final inline def clamp(low: Long, high: Long): Long
final inline def isBetween(low: Long, high: Long): Boolean
final inline def isEven: Boolean
final inline def isOdd: Boolean