SortedMap

scalus.prelude.SortedMap
See theSortedMap companion class
object SortedMap

Attributes

Companion
class
Graph
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
SortedMap.type

Members list

Type members

Inherited and Abstract types

type MirroredElemLabels <: Tuple

The names of the product elements

The names of the product elements

Attributes

Inherited from:
Mirror
type MirroredLabel <: String

The name of the type

The name of the type

Attributes

Inherited from:
Mirror

Value members

Concrete methods

def empty[A, B]: SortedMap[A, B]

Constructs an empty SortedMap.

Constructs an empty SortedMap.

Attributes

Example
 SortedMap.empty.toList === List.empty
def fromList[A : Ord, B](lst: List[(A, B)]): SortedMap[A, B]

Constructs a SortedMap from a list of key-value pairs, ordering it in strictly ascending order, in case when a key is presented multiple times, the first occurrence prevails.

Constructs a SortedMap from a list of key-value pairs, ordering it in strictly ascending order, in case when a key is presented multiple times, the first occurrence prevails.

Value parameters

lst

the list of key-value pairs

Attributes

Returns

a SortedMap containing the key-value pairs from the list, with unique keys in sorted order

See also

unsafeFromList for an unfiltered unsafe version or fromStrictlyAscendingList for a faster stricter version

Example
 SortedMap.fromList(List.Cons(("b", 2), List.Cons(("a", 1), List.Nil))).toList === List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))
 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("a", 2), List.Nil))).toList === List.Cons(("a", 1), List.Nil)
def fromStrictlyAscendingList[A : Ord, B](lst: List[(A, B)]): SortedMap[A, B]

Constructs a SortedMap from a list of key-value pairs, or fails if the list is not in strictly ascending order.

Constructs a SortedMap from a list of key-value pairs, or fails if the list is not in strictly ascending order.

Value parameters

lst

the list of key-value pairs

Attributes

Returns

a SortedMap containing the key-value pairs from the list, or fails if the list is not in strictly ascending order

Throws
scalus.cardano.onchain.RequirementError

if the list is not in strictly ascending order

See also

unsafeFromList for an unsafe fast or fromList for a safe slow versions that don't require strictly ascending order

Example
 SortedMap.fromStrictlyAscendingList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).toList === List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))
 SortedMap.fromStrictlyAscendingList(List.Cons(("a", 1), List.Cons(("a", 2), List.Nil))) // throws scalus.cardano.onchain.RequirementError
def singleton[A, B](key: A, value: B): SortedMap[A, B]

Constructs a SortedMap with a single key-value pair.

Constructs a SortedMap with a single key-value pair.

Value parameters

key

the key to insert

value

the value associated with the key

Attributes

Returns

a SortedMap containing the single key-value pair

Example
 SortedMap.singleton("key", "value").toList === List.single(("key", "value"))

Provides a validating FromData instance for SortedMap[A, B] where both key and value types are instances of FromData. This method ensures that the keys are in strictly ascending order using Ord[A] instance for keys.

Provides a validating FromData instance for SortedMap[A, B] where both key and value types are instances of FromData. This method ensures that the keys are in strictly ascending order using Ord[A] instance for keys.

Attributes

Throws
scalus.cardano.onchain.RequirementError

if the keys are not in strictly ascending order

def union[A : Ord, B, C](lhs: SortedMap[A, B], rhs: SortedMap[A, C]): SortedMap[A, These[B, C]]

Merges two SortedMaps into a new SortedMap containing keys from both maps. if a key is only present in left map, it is wrapped in These.This, if only in right map, it is wrapped in These.That, if a key is present in both maps, its values are wrapped in These.These. The resulting map is sorted by keys in strictly ascending order.

Merges two SortedMaps into a new SortedMap containing keys from both maps. if a key is only present in left map, it is wrapped in These.This, if only in right map, it is wrapped in These.That, if a key is present in both maps, its values are wrapped in These.These. The resulting map is sorted by keys in strictly ascending order.

This method is useful for combining two maps where you want to keep track of which keys are unique to each map and which keys are shared between

Value parameters

lhs

the left-hand side SortedMap

rhs

the right-hand side SortedMap

Attributes

Returns

a new SortedMap containing keys from both maps, with values combined into These

Example
 val map1 = SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil)))
 val map2 = SortedMap.fromList(List.Cons(("b", 3), List.Cons(("c", 4), List.Nil)))
 SortedMap.union(map1, map2).toList === List.Cons(("a", These.This(1)), List.Cons(("b", These.These(2, 3)), List.Cons(("c", These.That(4)), List.Nil)))
def unsafeFromList[A, B](lst: List[(A, B)]): SortedMap[A, B]

Constructs a SortedMap in unsafe way from a list of key-value pairs assuming that it's in strictly ascending order without any validation.

Constructs a SortedMap in unsafe way from a list of key-value pairs assuming that it's in strictly ascending order without any validation.

Value parameters

lst

the list of key-value pairs

Attributes

Returns

a SortedMap containing the key-value pairs from the list

See also

fromList or fromStrictlyAscendingList for safe versions

Example
 SortedMap.unsafeFromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).toList === List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))

Givens

Givens

given sortedMapEq[A : Eq, B : Eq]: () => SortedMap[A, B]

Provides an Eq instance for SortedMap[A, B] where both key and value types are instances of Eq.

Provides an Eq instance for SortedMap[A, B] where both key and value types are instances of Eq.

Attributes

Provides a FromData instance for SortedMap[A, B] where both key and value types are instances of FromData. There is no validation that the keys are in strictly ascending order

Provides a FromData instance for SortedMap[A, B] where both key and value types are instances of FromData. There is no validation that the keys are in strictly ascending order

Attributes

given sortedMapOrd[A : Ord, B : Ord]: Ord[SortedMap[A, B]]

Provides an Ord instance for SortedMap[A, B] where both key and value types are instances of Ord.

Provides an Ord instance for SortedMap[A, B] where both key and value types are instances of Ord.

Attributes

given sortedMapToData[A : ToData, B : ToData]: ToData[SortedMap[A, B]]

Provides a ToData instance for SortedMap[A, B] where both key and value types are instances of ToData.

Provides a ToData instance for SortedMap[A, B] where both key and value types are instances of ToData.

Attributes

Extensions

Extensions

extension [A, B](self: SortedMap[A, B])
def exists(f: ((A, B)) => Boolean): Boolean

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]

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]

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]

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)]

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]

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

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

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

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
inline def isEmpty: Boolean

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]

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

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]

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

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

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]

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))
extension [A, B](self: SortedMap[A, B])(implicit evidence$1: Ord[A])
def at: B

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

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]

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 get: Option[B]

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]

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)))