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 Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Members list
Value members
Concrete methods
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
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
-
trueif the map contains the key,falseotherwise - 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
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
SortedMapwith 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))
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
-
trueif the predicate holds for at least one pair,falseotherwise - 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
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
SortedMapcontaining 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)
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
SortedMapcontaining 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)
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
SortedMapcontaining 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)
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
Optioncontaining the first key-value pair that satisfies the predicate, orNoneif 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
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
Optioncontaining the result of mapping the first key-value pair that satisfies the predicate, orNoneif 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
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
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
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
-
trueif the predicate holds for all pairs,falseotherwise - 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
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
keyin this map, orNoneif 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
Retrieves the value associated with a key, or fails with a custom message if the key is not presented.
Retrieves the value associated with a key, or fails with a custom message if the key is not presented.
Value parameters
- key
-
the key to retrieve the value for
- message
-
the custom error message to use if the key is not found
Attributes
- Returns
-
the value associated with the key
- Throws
-
NoSuchElementException
if the key is not present in the map
- Example
-
SortedMap.singleton("key", "value").getOrFail("key") === "value" SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).getOrFail("a") === 1 SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).getOrFail("c") // throws NoSuchElementException SortedMap.empty.getOrFail("key") // throws NoSuchElementException
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
SortedMapwith 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)))
Checks if the SortedMap is empty.
Checks if the SortedMap is empty.
Attributes
- Returns
-
trueif the map is empty,falseotherwise - Example
-
SortedMap.empty.isEmpty === true SortedMap.singleton("key", "value").isEmpty === false
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.singleton("key") SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).keys === List.Cons("a", List.Cons("b", List.Nil))
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
Maps the values of the SortedMap using a function.
Maps the values of the SortedMap using a function.
On-chain this uses PairList.mapValues which is ~3x cheaper than List.map on tuples.
Value parameters
- f
-
the function to apply to each value
Attributes
- Returns
-
a new
SortedMapwith the same keys and transformed values - Example
-
SortedMap.singleton("key", 1).mapValues(_ + 1).toList === List.singleton(("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))
Checks if the SortedMap is non-empty.
Checks if the SortedMap is non-empty.
Attributes
- Returns
-
trueif the map is non-empty,falseotherwise - Example
-
SortedMap.empty.nonEmpty === false SortedMap.singleton("key", "value").nonEmpty === true
Returns the only entry of this map, or fails with message when the map is empty or has more than one entry.
Returns the only entry of this map, or fails with message when the map is empty or has more than one entry.
Value parameters
- message
-
The failure message when the map is not of size one.
Attributes
- Returns
-
The only key-value pair.
- Example
-
SortedMap.singleton("key", BigInt(1)).singleOrFail("expected one") === ("key", BigInt(1)) SortedMap.empty[String, BigInt].singleOrFail("expected one") // fails
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
Converts the SortedMap to a PairList.
Converts the SortedMap to a PairList.
On-chain this is efficient: toList does unMapData and toPairList is a noop.
Attributes
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.singleton("value") SortedMap.fromList(List.Cons(("a", 1), List.Cons(("b", 2), List.Nil))).values === List.Cons(1, List.Cons(2, List.Nil))
Inherited methods
Attributes
- Inherited from:
- Product
Attributes
- Inherited from:
- Product