SortedMap

scalus.prelude.SortedMap
See theSortedMap companion object
case class SortedMap[A, B]

Alternative to scala.collection.immutable.SortedMap in onchain code.

Type parameters

A

the type of keys, must be an instance of Ord

B

the type of values

Attributes

Companion
object
Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def at: B
Extension method from SortedMap

Retrieves the value associated with a key, or fails if the key is not present.

Retrieves the value associated with a key, or fails if the key is not present.

Value parameters

key

the key to retrieve the value for

Attributes

Returns

the value associated with the key

Throws
NoSuchElementException

if the key is not present in the map

Example
 SortedMap.singleton("key", "value").at("key") === "value"
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).at("a") === 1
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).at("c") // throws NoSuchElementException
 SortedMap.empty.at("key") // throws NoSuchElementException
def contains: Boolean
Extension method from SortedMap

Checks if the SortedMap contains a key.

Checks if the SortedMap contains a key.

Value parameters

key

the key to check for existence

Attributes

Returns

true if the map contains the key, false otherwise

Example
 SortedMap.empty.contains("key") === false
 SortedMap.singleton("key", "value").contains("key") === true
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).contains("a") === true
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).contains("c") === false
def delete: SortedMap[A, B]
Extension method from SortedMap

Deletes a key-value pair from the SortedMap by key.

Deletes a key-value pair from the SortedMap by key.

Value parameters

key

the key to delete

Attributes

Returns

a new SortedMap with the key-value pair removed, if it existed

Example
 SortedMap.empty[String, BigInt].delete("key") === SortedMap.empty
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).delete("a").toList === List.Cons(("b", 2), List.Nil)
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).delete("c").toList === List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))
def exists(f: ((A, B)) => Boolean): Boolean
Extension method from SortedMap

Checks if a predicate holds for at least one key-value pair in the SortedMap.

Checks if a predicate holds for at least one key-value pair in the SortedMap.

Value parameters

f

the predicate function to check

Attributes

Returns

true if the predicate holds for at least one pair, false otherwise

Example
 SortedMap.empty.exists(_ => true) === false
 SortedMap.singleton("key", "value").exists(_._1 === "foo") === false
 SortedMap.singleton("key", "value").exists(_._1 === "key") === true
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).exists(_._2 > 1) === true
def filter(predicate: ((A, B)) => Boolean): SortedMap[A, B]
Extension method from SortedMap

Filters the key-value pairs of the SortedMap based on a predicate.

Filters the key-value pairs of the SortedMap based on a predicate.

Value parameters

predicate

the predicate function to apply to each key-value pair

Attributes

Returns

a new SortedMap containing only the key-value pairs that satisfy the predicate

Example
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).filter(_._2 > 1).toList === List.Cons(("b", 2), List.Nil)
def filterKeys(predicate: A => Boolean): SortedMap[A, B]
Extension method from SortedMap

Filters the keys of the SortedMap based on a predicate.

Filters the keys of the SortedMap based on a predicate.

Value parameters

predicate

the predicate function to apply to each key

Attributes

Returns

a new SortedMap containing only the key-value pairs where the key satisfies the predicate

Example
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).filterKeys(_ === "a").toList === List.Cons(("a", 1), List.Nil)
def filterNot(predicate: ((A, B)) => Boolean): SortedMap[A, B]
Extension method from SortedMap

Filters the key-value pairs of the SortedMap based on a negated predicate.

Filters the key-value pairs of the SortedMap based on a negated predicate.

Value parameters

predicate

the predicate function to apply to each key-value pair, negated

Attributes

Returns

a new SortedMap containing only the key-value pairs that do not satisfy the predicate

Example
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).filterNot(_._2 > 1).toList === List.Cons(("a", 1), List.Nil)
def find(predicate: ((A, B)) => Boolean): Option[(A, B)]
Extension method from SortedMap

Optionally returns the first key-value pair that satisfies a predicate.

Optionally returns the first key-value pair that satisfies a predicate.

Value parameters

predicate

the predicate function to apply to each key-value pair

Attributes

Returns

an Option containing the first key-value pair that satisfies the predicate, or None if no such pair exists

Example
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).find(_._1 === "b") === Some(("b", 2))
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).find(_._1 === "c") === None
def findMap[C](predicate: ((A, B)) => Option[C]): Option[C]
Extension method from SortedMap

Finds the first key-value pair that satisfies a predicate and maps it to a new type.

Finds the first key-value pair that satisfies a predicate and maps it to a new type.

Value parameters

predicate

the predicate function to apply to each key-value pair

Attributes

Returns

an Option containing the result of mapping the first key-value pair that satisfies the predicate, or None if no such pair exists

Example
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).findMap {
   case ("b", v) => Some(v + 1)
   case _        => None
 } === Some(3)
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).findMap {
   case ("c", v) => Some(v + 1)
   case _        => None
 } === None
 SortedMap.empty.findMap(_ => Some(1)) === None
def foldLeft[C](init: C)(combiner: (C, (A, B)) => C): C
Extension method from SortedMap

Folds the SortedMap from the left, combining key-value pairs into a single value.

Folds the SortedMap from the left, combining key-value pairs into a single value.

Value parameters

combiner

the function to combine the accumulated value with each key-value pair

init

the initial value to start folding from

Attributes

Returns

the final accumulated value after folding over all key-value pairs

Example
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).foldLeft(0)(_ + _._2) === 3
def foldRight[C](init: C)(combiner: ((A, B), C) => C): C
Extension method from SortedMap

Folds the SortedMap from the right, combining key-value pairs into a single value.

Folds the SortedMap from the right, combining key-value pairs into a single value.

Value parameters

combiner

the function to combine the accumulated value with each key-value pair

init

the initial value to start folding from

Attributes

Returns

the final accumulated value after folding over all key-value pairs

Example
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).foldLeft(0)(_ + _._2) === 3
def forall(f: ((A, B)) => Boolean): Boolean
Extension method from SortedMap

Checks if a predicate holds for all key-value pairs in the SortedMap.

Checks if a predicate holds for all key-value pairs in the SortedMap.

Value parameters

f

the predicate function to check

Attributes

Returns

true if the predicate holds for all pairs, false otherwise

Example
 SortedMap.empty.forall(_ => true) === true
 SortedMap.singleton("key", "value").forall(_._1 === "foo") === false
 SortedMap.singleton("key", "value").forall(_._1 === "key") === true
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).forall(_._2 > 0) === true
def get: Option[B]
Extension method from SortedMap

Optionally returns the value associated with a key.

Optionally returns the value associated with a key.

Value parameters

key

the key value

Attributes

Returns

an option value containing the value associated with key in this map, or None if none exists.

Example
 SortedMap.empty.get("key") === None
 SortedMap.singleton("key", "value").get("key") === Some("value")
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).get("a") === Some(1)
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).get("c") === None
def insert: SortedMap[A, B]
Extension method from SortedMap

Insert a key-value pair into the SortedMap, maintaining the sorted order without duplication. * If the key already exists, it updates the value. * @param key the key to insert

Insert a key-value pair into the SortedMap, maintaining the sorted order without duplication. * If the key already exists, it updates the value. * @param key the key to insert

Value parameters

value

the value associated with the key * @return a new SortedMap with the key-value pair inserted

Attributes

Example
 SortedMap.empty.insert("key", "value") === SortedMap.singleton("key", "value")
 SortedMap.singleton("key", "value").insert("key", "newValue") === SortedMap.singleton("key", "newValue")
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).insert("c", 3).toList === List.Cons(("a", 1), List.Cons(("b", 2), List.Cons(("c", 3), List.Nil)))
inline def isEmpty: Boolean
Extension method from SortedMap

Checks if the SortedMap is empty.

Checks if the SortedMap is empty.

Attributes

Returns

true if the map is empty, false otherwise

Example
 SortedMap.empty.isEmpty === true
 SortedMap.singleton("key", "value").isEmpty === false
def keys: List[A]
Extension method from SortedMap

Returns a list of keys in the SortedMap.

Returns a list of keys in the SortedMap.

Attributes

Returns

a list containing all keys in the map

Example
 SortedMap.empty.keys === List.empty
 SortedMap.singleton("key", "value").keys === List.single("key")
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).keys === List.Cons("a", List.Cons("b", List.Nil))
inline def length: BigInt
Extension method from SortedMap

Returns the number of key-value pairs in the SortedMap.

Returns the number of key-value pairs in the SortedMap.

Attributes

Returns

the number of key-value pairs in the map

Example
 SortedMap.empty.length === 0
 SortedMap.singleton("key", "value").length === 1
def mapValues[C](f: B => C): SortedMap[A, C]
Extension method from SortedMap

Maps the values of the SortedMap using a function.

Maps the values of the SortedMap using a function.

Value parameters

f

the function to apply to each value

Attributes

Returns

a new SortedMap with the same keys and transformed values

Example
 SortedMap.singleton("key", 1).mapValues(_ + 1).toList === List.single(("key", 2))
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).mapValues(_ * 2).toList === List.Cons(("a", 2), List.Cons(("b", 4), List.Nil))
inline def nonEmpty: Boolean
Extension method from SortedMap

Checks if the SortedMap is non-empty.

Checks if the SortedMap is non-empty.

Attributes

Returns

true if the map is non-empty, false otherwise

Example
 SortedMap.empty.nonEmpty === false
 SortedMap.singleton("key", "value").nonEmpty === true
inline def size: BigInt
Extension method from SortedMap

Returns the size of the SortedMap, which is the same as its length.

Returns the size of the SortedMap, which is the same as its length.

Attributes

Returns

the size of the map

Example
 SortedMap.empty.size === 0
 SortedMap.singleton("key", "value").size === 1
def values: List[B]
Extension method from SortedMap

Returns a list of values in the SortedMap.

Returns a list of values in the SortedMap.

Attributes

Returns

a list containing all values in the map

Example
 SortedMap.empty.values === List.empty
 SortedMap.singleton("key", "value").values === List.single("value")
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).values === List.Cons(1, List.Cons(2, List.Nil))

Inherited methods

def productElementNames: Iterator[String]

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

Attributes

Inherited from:
Product