FP

com.phasmidsoftware.number.core.misc.FP
object FP

This module is concerned with the generic operations for operating on Numbers.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
FP.type

Members list

Value members

Concrete methods

def doMap[L, R, Z](lRe: Either[L, R])(r2Z: R => Z, l2Zy: L => Try[Z]): Try[Z]

This method and optionMap are (almost) twins (working for Try and Option respectively). To yield the (tried) result, we map the right-hand member of the input (lRe) with a function rToZ. The result of this is then pattern-matched: In the Some(z) case, we return the Success(z). In the None case, we return the result of tryMapLeft applied to the lRe with the function l2Zy.

This method and optionMap are (almost) twins (working for Try and Option respectively). To yield the (tried) result, we map the right-hand member of the input (lRe) with a function rToZ. The result of this is then pattern-matched: In the Some(z) case, we return the Success(z). In the None case, we return the result of tryMapLeft applied to the lRe with the function l2Zy.

NOTE: in practice, this does not seem to be used, but it should remain here.

Type parameters

L

the type of the left-side of the Either.

R

the type of the right-side of the Either.

Z

the underlying type of the result.

Value parameters

l2Zy

a function L => Try[Z]

lRe

the input, an Either[L,R].

r2Z

a function R => Z.

Attributes

Returns

a Try[Z]

def fail[X, Z](s: String): X => Try[Z]

Method to yield a Failure.

Method to yield a Failure.

Type parameters

Z

the underlying type of the result.

Value parameters

s

a String

Attributes

Returns

a function of Z => Failure[Z] based on CoreException(s)

def fail[X, Z](e: Throwable): X => Try[Z]

Method to yield a Failure.

Method to yield a Failure.

Type parameters

Z

the underlying type of the result.

Value parameters

e

a Throwable

Attributes

Returns

a function of Z => Failure[Z] based on e

def getOrThrow[X](xo: => Option[X], t: => Throwable): X

Method to get the value of an Option[X] but throwing a given exception rather than the usual NoSuchElement.

Method to get the value of an Option[X] but throwing a given exception rather than the usual NoSuchElement.

Type parameters

X

the underlying type of xo and the type of the result.

Value parameters

t

a throwable.

xo

an optional value of X (called by name).

Attributes

Returns

the value of xo or throws t.

Note

Throws java.lang.Throwable t

def identityTry[X](x: X): Try[X]

This method is a substitute for Try.apply in the case that we want it as a function (otherwise, we run into a type inference problem).

This method is a substitute for Try.apply in the case that we want it as a function (otherwise, we run into a type inference problem).

Type parameters

X

the type of x.

Value parameters

x

an X.

Attributes

Returns

Success(x)

def optionMap[L, R, Z](lRe: Either[L, R])(r2Z: R => Z, l2Zo: L => Option[Z]): Option[Z]

This method and tryMap are (almost) twins (working for Option and Try respectively -- although there are a few differences other than the monad type). To yield the (optional) result, we map the right-hand member of the input (lRe) with a function rToZ. We return either the result or, if empty, then the result of flatMap applied to the left member with a function lToZo.

This method and tryMap are (almost) twins (working for Option and Try respectively -- although there are a few differences other than the monad type). To yield the (optional) result, we map the right-hand member of the input (lRe) with a function rToZ. We return either the result or, if empty, then the result of flatMap applied to the left member with a function lToZo.

Type parameters

L

the type of the left-side of the Either.

R

the type of the right-side of the Either.

Z

the underlying type of the result.

Value parameters

l2Zo

a function L => Option[Z].

lRe

the input, an Either[L,R].

r2Z

a function R => Z.

Attributes

Returns

an Option[Z]

def optional[T](p: T => Boolean)(t: T): Option[T]

Method to yield an Option of T according to whether the predicate p yields true.

Method to yield an Option of T according to whether the predicate p yields true.

Type parameters

T

the type of t (and the underlying type of the result).

Value parameters

p

a predicate on T.

t

an actual value of T.

Attributes

Returns

Some(t) if p(t) is true, otherwise None.

def power[X : Numeric](x: X, n: Int): X

Computes the power of a given number by raising it to the specified exponent.

Computes the power of a given number by raising it to the specified exponent.

CONSIDER moving this somewhere else more aligned to Numeric ops

Value parameters

n

the exponent to which the base number is raised

x

the base number

Attributes

Returns

the result of raising the base number to the specified exponent

def readFromResource(filename: String, function: Array[String] => Option[String]): Try[Seq[BigInt]]

Reads data from a specified resource file, applies a transformation function to each line, and converts the resulting values into a sequence of BigInt objects.

Reads data from a specified resource file, applies a transformation function to each line, and converts the resulting values into a sequence of BigInt objects.

Value parameters

filename

the name of the resource file to read from

function

a function that takes an array of strings (split from a line) and returns an optional string result after processing

Attributes

Returns

a Try containing a sequence of BigInt values if successful, or a failure if an error occurs during processing or if the file contains invalid input

def recover[T](to: Option[T], x: => Throwable): T

Method to convert a None value to a given exception (rather than the NoSuchElement exception).

Method to convert a None value to a given exception (rather than the NoSuchElement exception).

Type parameters

T

the underlying type of to.

Value parameters

to

an Option[T].

x

a Throwable to be thrown if to is None.

Attributes

Returns

t if to is Some(t); otherwise x will be thrown.

def recover[T](ty: Try[T]): T

Method to extract a value from a Try[T] or throw an exception. It's essentially the same as just calling get, but without Codacy complaining.

Method to extract a value from a Try[T] or throw an exception. It's essentially the same as just calling get, but without Codacy complaining.

Type parameters

T

the underlying type of ty.

Value parameters

ty

an Try[T].

Attributes

Returns

ty.get

def resource[C : ClassTag](resourceName: String): Try[URL]

Method to yield a URL for a given resourceForClass in the classpath for C.

Method to yield a URL for a given resourceForClass in the classpath for C.

Type parameters

C

a class of the package containing the resourceForClass.

Value parameters

resourceName

the name of the resourceForClass.

Attributes

Returns

a Try[URL].

def resourceForClass(resourceName: String, clazz: Class[_] = ...): Try[URL]

Method to yield a Try[URL] for a resource name and a given class.

Method to yield a Try[URL] for a resource name and a given class.

Value parameters

clazz

the class, relative to which, the resource can be found (defaults to the caller's class).

resourceName

the name of the resource.

Attributes

Returns

a Try[URL]

def sequence[X](xys: Iterator[Try[X]]): Try[Iterator[X]]

Sequence method to combine elements of Try.

Sequence method to combine elements of Try.

Type parameters

X

the underlying type

Value parameters

xys

an Iterator of Try[X]

Attributes

Returns

a Try of Iterator[X]

def sequence[X](xys: Iterable[Try[X]]): Try[Seq[X]]

Sequence method to combine elements of Try.

Sequence method to combine elements of Try.

Type parameters

X

the underlying type

Value parameters

xys

an Iterable of Try[X]

Attributes

Returns

a Try of Seq[X] NOTE: that the output collection type will be Seq, regardless of the input type

def sequence[X](xos: Iterator[Option[X]]): Option[Iterator[X]]

Sequence method to combine elements of Try.

Sequence method to combine elements of Try.

Type parameters

X

the underlying type

Value parameters

xos

an Iterator of Try[X]

Attributes

Returns

a Try of Iterator[X]

def sequence[X](xyo: Option[Try[X]]): Try[Option[X]]

Sequence method to invert the order of types Option/Try.

Sequence method to invert the order of types Option/Try.

Type parameters

X

the underlying type.

Value parameters

xyo

an Option of Try[X].

Attributes

Returns

a Try of Option[X].

def sequence[X](xos: Iterable[Option[X]]): Option[Seq[X]]

Sequence method to combine elements of type Option[X].

Sequence method to combine elements of type Option[X].

Type parameters

X

the underlying type.

Value parameters

xos

an Iterable of Option[X].

Attributes

Returns

if xos contains any Nones, the result will be None, otherwise Some(...). NOTE: that the output collection type will be Seq, regardless of the input type

def toOption[X](xy: Try[X]): Option[X]

Converts a Try instance into an Option. Any Exception in the input is of course lost.

Converts a Try instance into an Option. Any Exception in the input is of course lost.

Value parameters

xy

The Try instance to be converted.

Attributes

Returns

An Option containing the value if the Try is a Success, or None if the Try is a Failure.

def toTry[X](xo: Option[X], default: => Try[X]): Try[X]

Method to convert an Option into a Try.

Method to convert an Option into a Try.

Type parameters

X

the underlying type of both input and output.

Value parameters

default

a Try[X].

xo

an Option[X].

Attributes

Returns

if xo is Some(x) then Success(x) else default.

def toTryWithRationalException[X](xo: Option[X], default: => String): Try[X]

Method to convert an Option into a Try where the default is a String.

Method to convert an Option into a Try where the default is a String.

Type parameters

X

the underlying type of both input and output.

Value parameters

default

a Throwable.

xo

an Option[X].

Attributes

Returns

if xo is Some(x) then Success(x) else Failure(RationalException(default)).

def toTryWithThrowable[X](xo: Option[X], default: => Throwable): Try[X]

Method to convert an Option into a Try where the default is an exception, i.e., a Throwable. TODO split parameter set into two.

Method to convert an Option into a Try where the default is an exception, i.e., a Throwable. TODO split parameter set into two.

Type parameters

X

the underlying type of both input and output.

Value parameters

default

a Throwable.

xo

an Option[X].

Attributes

Returns

if xo is Some(x) then Success(x) else Failure(default).

def transpose[L, R](lRe: Either[L, R])(implicit rToL: R => L): Either[L, R]

If lRe is a Right(R), then we transpose it into a Left(L).

If lRe is a Right(R), then we transpose it into a Left(L).

Type parameters

L

the left type.

R

the right type.

Value parameters

lRe

an Either[L, R].

Attributes

Returns

a Left(L) as an Either[L, R].

def tryF[X, Z](f: X => Z): X => Try[Z]

Method to yield a partially lifted version of a Function1 as a function.

Method to yield a partially lifted version of a Function1 as a function.

Type parameters

X

the type of the input to f and the input to the resulting function.

Z

the type of the output from f and the underlying type of the resulting function.

Value parameters

f

an X => Z.

Attributes

Returns

a function X => Try[Z]

def tryF[X, Y, Z](f: (X, Y) => Z): (X, Y) => Try[Z]

Method to yield a partially lifted version of a Function2 as a function.

Method to yield a partially lifted version of a Function2 as a function.

Type parameters

X

the type of the first input to f and of the resulting function.

Y

the type of the second input to f and of the resulting function.

Z

the type of the output from f and the underlying type of the resulting function.

Value parameters

f

an (X,Y) => Z.

Attributes

Returns

a function X => Try[Z]

def tryMap[L, R, Z](lRe: Either[L, R])(r2Zy: R => Try[Z], l2Zy: L => Try[Z])(implicit r2L: R => L): Try[Z]

This method is similar to doMap but with some important differences. To yield the (tried) result, we map the right-hand member of the input (lRe) with a function rToZy. The result of this is then pattern-matched: In the case of Some(Success(z)), we return the Success(z). In the case of Some(Failure), we invoke tryMapLeft with the transpose of lRe and the function l2Zy. In the case of None, we return the result of tryMapLeft applied to the lRe with the function l2Zy.

This method is similar to doMap but with some important differences. To yield the (tried) result, we map the right-hand member of the input (lRe) with a function rToZy. The result of this is then pattern-matched: In the case of Some(Success(z)), we return the Success(z). In the case of Some(Failure), we invoke tryMapLeft with the transpose of lRe and the function l2Zy. In the case of None, we return the result of tryMapLeft applied to the lRe with the function l2Zy.

Type parameters

L

the type of the left-side of the Either.

R

the type of the right-side of the Either.

Z

the underlying type of the result.

Value parameters

l2Zy

a function L => Try[Z]

lRe

the input, an Either[L,R].

r2L

an (implicit) converter from R to L.

r2Zy

a function R => Try[Z].

Attributes

Returns

a Try[Z]

def whenTry[X](p: Boolean)(x: => X): Try[X]

Executes the provided computation if the given condition is true; otherwise, returns a Failure.

Executes the provided computation if the given condition is true; otherwise, returns a Failure.

Value parameters

p

A Boolean condition that determines whether the computation should be executed.

x

A computation represented as a call-by-name parameter, which returns a Try[X] when evaluated.

Attributes

Returns

A Try[X] representing the result of the computation if the condition is true, or a Failure if the condition is false.

def whenever[X](p: Boolean)(x: => Option[X]): Option[X]

Executes the provided block of code conditionally based on the given boolean predicate. If the predicate is true, the block of code is executed and its result is returned. If the predicate is false, None is returned.

Executes the provided block of code conditionally based on the given boolean predicate. If the predicate is true, the block of code is executed and its result is returned. If the predicate is false, None is returned.

Value parameters

p

the boolean predicate that determines whether the block of code should be executed

x

a by-name parameter representing the block of code returning an Option of type X

Attributes

Returns

an Option containing the result of the block of code if p is true, or None if p is false

def wheneverTry[X](p: Boolean)(x: => Try[X]): Try[X]

Executes the provided computation if the given condition is true; otherwise, returns a Failure.

Executes the provided computation if the given condition is true; otherwise, returns a Failure.

Value parameters

p

A Boolean condition that determines whether the computation should be executed.

x

A computation represented as a call-by-name parameter, which returns a Try[X] when evaluated.

Attributes

Returns

A Try[X] representing the result of the computation if the condition is true, or a Failure if the condition is false.