FP

com.phasmid.majabigwaduce.core.FP
object FP

This module contains functional programming methods which can also be found in the FP module of LaScala

Attributes

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

Members list

Value members

Concrete methods

def asFuture[X](xy: => Try[X]): Future[X]

Converts a lazy Try[X] into a Future[X].

Converts a lazy Try[X] into a Future[X].

If the Try is a Success, the resulting Future will be successfully completed with the same value. If the Try is a Failure, the resulting Future will be failed with the same exception.

Type parameters

X

the type of the value contained within the Try.

Value parameters

xy

a lazy computation producing a Try[X] instance, which may either be Success containing a value or Failure containing an exception.

Attributes

Returns

a Future[X] that is completed based on the outcome of the provided Try.

def checkCompatible[A, B](as: Seq[A], bs: Seq[B]): Try[(Seq[A], Seq[B])]

Method to make a compatibility check on two vectors (not currently used). The result is successful if the vectors are of the same (non-zero) size.

Method to make a compatibility check on two vectors (not currently used). The result is successful if the vectors are of the same (non-zero) size.

Type parameters

A

the underlying type of as.

B

the underlying type of bs.

Value parameters

as

a vector of As.

bs

a vector of Bs.

Attributes

Returns

a tuple of the two vectors, all wrapped in Try.

def checkCompatibleX[A, B](as: Seq[A], bss: Seq[Seq[B]]): Try[(Seq[A], Seq[Seq[B]])]

Method to make a compatibility check on a vector and a 2-matrix (not currently used). The result is successful if the vectors are of the same (non-zero) size.

Method to make a compatibility check on a vector and a 2-matrix (not currently used). The result is successful if the vectors are of the same (non-zero) size.

Type parameters

A

the underlying type of as.

B

the underlying type of bss.

Value parameters

as

a vector of As, represented as a Seq[A].

bss

a 2-matrix of Bs, represented as a Seq[Seq[B]].

Attributes

Returns

a tuple of the vector and the transpose of the 2-matrix, all wrapped in Try.

def flatten[X](xyf: Future[Try[X]])(implicit executor: ExecutionContext): Future[X]

Method flatten which takes a Future[Try[X]] and returns a Future[X].

Method flatten which takes a Future[Try[X]] and returns a Future[X].

Type parameters

X

the underlying type.

Value parameters

executor

(implicit) the ExecutionContext

xyf

a Future of Try of X.

Attributes

Returns

a Future[X], which will be successful if the Try was a success, otherwise a failure.

def flatten[X](xfy: Try[Future[X]])(implicit executor: ExecutionContext): Future[X]

Method to flatten a Try[Future[X]] (not a common occurrence).

Method to flatten a Try[Future[X]] (not a common occurrence).

Type parameters

X

the underlying type.

Value parameters

executor

(implicit) the ExecutionContext

xfy

a Try of Future of X.

Attributes

Returns

a Future[X], which will be successful if the Try was a success, otherwise a failure.

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 invokeTupled[T1, T2, R](t: (T1, T2))(f: (T1, T2) => R): R

Method to invoke a function (T1,T2)=>R on a tuple (T1, T2).

Method to invoke a function (T1,T2)=>R on a tuple (T1, T2).

Type parameters

R

the type of the result.

T1

the type of the first attribute of t.

T2

the type of the second attribute of t.

Value parameters

f

the function.

t

the tuple.

Attributes

Returns

the result of invoking f on t.

def lift[T, R](f: T => R): T => Try[R]

Lifts a function f of type T => R into a function of type T => Try[R]. This allows the application of f to a value of type T while capturing any exceptions that may be thrown during its execution as a Failure. If the function executes successfully, the result is wrapped in a Success.

Lifts a function f of type T => R into a function of type T => Try[R]. This allows the application of f to a value of type T while capturing any exceptions that may be thrown during its execution as a Failure. If the function executes successfully, the result is wrapped in a Success.

Type parameters

R

the output type of the function.

T

the input type of the function.

Value parameters

f

the function to be lifted, of type T => R.

Attributes

Returns

a new function of type T => Try[R] that applies the original function and wraps its result in a Try.

def lift[T1, T2, R](f: (T1, T2) => R): (T1, T2) => Try[R]

Lifts a binary function (T1, T2) => R into a function that returns a Try[R], capturing any exceptions that might occur during its execution.

Lifts a binary function (T1, T2) => R into a function that returns a Try[R], capturing any exceptions that might occur during its execution.

Type parameters

R

the return type of the function.

T1

the type of the first input parameter of the function.

T2

the type of the second input parameter of the function.

Value parameters

f

the binary function to be lifted, of type (T1, T2) => R.

Attributes

Returns

a new function of type (T1, T2) => Try[R] that applies the original function and wraps its result in a Try.

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 partition[K, V, X](vXeKm: Map[K, Either[X, V]]): (Seq[(K, Either[X, V])], Seq[(K, Either[X, V])])

Method to take a Map[K,Either[X,V]] and generated a tuple of two sequenced-maps, each of the same form as the input but containing only the left-values or right-values as appropriate. TESTME

Method to take a Map[K,Either[X,V]] and generated a tuple of two sequenced-maps, each of the same form as the input but containing only the left-values or right-values as appropriate. TESTME

Type parameters

K

the key type

V

the value type

X

the partition type

Value parameters

vXeKm

the input map

Attributes

Returns

a tuple of Map[K,Either[X,V]] maps in sequenced form.

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 recoverAsTry[T](to: Option[T])(x: => Throwable): Try[T]

Converts an Option into a Try, providing a failure cause if the Option is None.

Converts an Option into a Try, providing a failure cause if the Option is None.

Type parameters

T

the underlying type of to.

Value parameters

to

the optional value to convert into a Try.

x

the throwable to use as the failure cause if the Option is None.

Attributes

Returns

a Success containing the value from the Option if it is Some, or a Failure if it is None.

def recoverWithTry[T](to: Option[T])(x: => Try[T]): Try[T]

Converts an Option into a Try, falling back to the given Try if the Option is None.

Converts an Option into a Try, falling back to the given Try if the Option is None.

Type parameters

T

the underlying type of to.

Value parameters

to

the optional value to convert into a Try.

x

the Try to fall back to if the Option is None.

Attributes

Returns

a Success containing the value from the Option if it is Some, or x if it is None.

def sequence[X](xts: Seq[Try[X]]): Try[Seq[X]]

Method sequence which, applied to a Seq[Try[X]], returns a Try[Seq[X]]

Method sequence which, applied to a Seq[Try[X]], returns a Try[Seq[X]]

Type parameters

X

the underlying type

Value parameters

xts

: Seq[Try[X]]

Attributes

Returns

: Try[Seq[X]]

def sequence[A, B](t: (A, Try[B])): Try[(A, B)]

Method sequence to convert a tuple of A, Try[B] to a Try[(A, B)]

Method sequence to convert a tuple of A, Try[B] to a Try[(A, B)]

Type parameters

A

the underlying type of the _1 element of t.

B

the underlying type of the _2 element of t.

Value parameters

t

the tuple.

Attributes

Returns

a Try of (A, B).

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 sequence[X](xos: Iterator[Option[X]]): Option[Iterator[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 Iterator of Option[X].

Attributes

Returns

an Option of Iterator[X].

def sequenceLeft[K, X, V](xVeKs: Seq[(K, Either[X, V])]): Seq[(K, X)]

Method sequenceLeft which, given a Map[K,Either[X,V]] (in sequential form), returns a Map[K,X] (also in sequential form) for those elements of the input map which are a (left) X (as opposed to a (right) V).

Method sequenceLeft which, given a Map[K,Either[X,V]] (in sequential form), returns a Map[K,X] (also in sequential form) for those elements of the input map which are a (left) X (as opposed to a (right) V).

Type parameters

K

the key type

V

the value type

X

the partition type

Value parameters

xVeKs

a Map[K,Either[X,V]] (in sequential form)

Attributes

Returns

a Map[K,X] (in sequential form)

def sequenceLeftRight[K, V, X](vXeKm: Map[K, Either[X, V]]): (Seq[(K, X)], Seq[(K, V)])

Method sequenceLeftRight which, given a Map[K,Either[X,V]], returns a tuple of sequenced maps (each with the same key type), with the X values on the left and the V values on the right.

Method sequenceLeftRight which, given a Map[K,Either[X,V]], returns a tuple of sequenced maps (each with the same key type), with the X values on the left and the V values on the right.

Type parameters

K

the key type

V

the value type

X

the partition type

Value parameters

vXeKm

the map

Attributes

Returns

the separated maps as a tuple of sequenced maps

def sequenceRight[K, X, V](xVeKs: Seq[(K, Either[X, V])]): Seq[(K, V)]

Method sequenceRight which, given a Map[K,Either[X,V]] (in sequential form), returns a Map[K,V] (also in sequential form) for those elements of the input map which are a (right) V (as opposed to a (left) X).

Method sequenceRight which, given a Map[K,Either[X,V]] (in sequential form), returns a Map[K,V] (also in sequential form) for those elements of the input map which are a (right) V (as opposed to a (left) X).

Type parameters

K

the key type

V

the value type

X

the partition type

Value parameters

xVeKs

a Map[K,Either[X,V]] (in sequential form)

Attributes

Returns

a Map[K,V] (in sequential form)

def toEither[X](xt: Try[X]): Either[Throwable, X]

Method sequence which applied to a Try[X] returns an Either[Throwable,X].

Method sequence which applied to a Try[X] returns an Either[Throwable,X].

Type parameters

X

the underlying type

Value parameters

xt

: Try[X]

Attributes

Returns

: Either[Throwable,X]

def toMap[K, V, X](t: (Seq[(K, X)], Seq[(K, V)])): (Map[K, X], Map[K, V])

Method toMap which takes a tuple of sequenced maps and returns a tuple of actual maps (each map has the same key type but different value types)

Method toMap which takes a tuple of sequenced maps and returns a tuple of actual maps (each map has the same key type but different value types)

Type parameters

K

the key type

V

the value type

X

the partition type

Value parameters

t

the input tuple

Attributes

Returns

the output tuple

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.

Type parameters

X

the underlying type.

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 toOptionWithLog[X](log: Throwable => Unit)(xy: => Try[X]): Option[X]

Converts a Try instance into an Option. Any exception in the input is logged according to the log function.

Converts a Try instance into an Option. Any exception in the input is logged according to the log function.

Type parameters

X

the underlying type.

Value parameters

log

a function to log the exception, if any.

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, given a default Try to use if the Option is None.

Method to convert an Option into a Try, given a default Try to use if the Option is None.

Type parameters

X

the underlying type of both input and output.

Value parameters

default

a Try[X] to use if xo is None.

xo

an Option[X].

Attributes

Returns

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

def tupleMap[L1, L2, R1, R2](fl: L1 => L2, fr: R1 => R2)(t: (L1, R1)): (L2, R2)

Transforms each element of a tuple by applying the provided functions to its components.

Transforms each element of a tuple by applying the provided functions to its components.

Type parameters

L1

the type of the first element in the input tuple.

L2

the type of the first element in the output tuple.

R1

the type of the second element in the input tuple.

R2

the type of the second element in the output tuple.

Value parameters

fl

a function to transform the first element of the tuple.

fr

a function to transform the second element of the tuple.

t

a tuple containing two elements to be transformed.

Attributes

Returns

a new tuple where the first element is transformed by fl, and the second by fr.

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.

Type parameters

X

the underlying type.

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 an 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.

Type parameters

X

the underlying type.

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. Unlike whenTry, the computation itself yields a Try[X], so the result is flattened.

Executes the provided computation if the given condition is true; otherwise, returns a Failure. Unlike whenTry, the computation itself yields a Try[X], so the result is flattened.

Type parameters

X

the underlying type.

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.