HORIZON HASKELLDocslts/ghc-9.14.x3e2d5682026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 3e2d568 · 2026-10-11

Modulenonempty-containers-0.3.6.0Haskell2010

Data.IntMap.NonEmpty.Internal

Unsafe internal-use functions used in the implementation of Data.IntMap.NonEmpty. These functions can potentially be used to break the abstraction of NEIntMap and produce unsound maps, so be wary!

  • 2 types
  • 24 values

Non-Empty IntMap type

14 declarations
datadata NEIntMap a
#

A non-empty (by construction) map from integer keys to values a. At least one key-value pair exists in an NEIntMap v at all times.

Functions that take an NEIntMap can safely operate on it with the assumption that it has at least one key-value pair.

Functions that return an NEIntMap provide an assurance that the result has at least one key-value pair.

Data.IntMap.NonEmpty re-exports the API of Data.IntMap, faithfully reproducing asymptotics, typeclass constraints, and semantics. Functions that ensure that input and output maps are both non-empty (like insert) return NEIntMap, but functions that might potentially return an empty map (like delete) return a IntMap instead.

You can directly construct an NEIntMap with the API from Data.IntMap.NonEmpty; it's more or less the same as constructing a normal IntMap, except you don't have access to Data.IntMap.empty. There are also a few ways to construct an NEIntMap from a IntMap:

  1. The nonEmptyMap smart constructor will convert a IntMap k a into a Maybe (NEIntMap k a), returning Nothing if the original IntMap was empty.

  2. You can use the Data.IntMap.NonEmpty.insertIntMap family of functions to insert a value into a IntMap to create a guaranteed NEIntMap.

  3. You can use the IsNonEmpty and IsEmpty patterns to "pattern match" on a IntMap to reveal it as either containing a NEIntMap or an empty map.

  4. withNonEmpty offers a continuation-based interface for deconstructing a IntMap and treating it as if it were an NEIntMap.

You can convert an NEIntMap into a IntMap with toMap or IsNonEmpty, essentially "obscuring" the non-empty property from the type.

Constructors

Instances27Functor, Foldable, Traversable, Foldable1, Eq1, Ord1, …
  • Functor NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Foldable NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal

    Traverses elements in order of ascending keys.

    WARNING: fold and foldMap are different than for the IntMap instance. They traverse elements in order of ascending keys, while IntMap traverses positive keys first, then negative keys.

    foldr1, foldl1, minimum, maximum are all total.

  • Traversable NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal

    Traverses elements in order of ascending keys

    WARNING: Different than for the IntMap instance. They traverse elements in order of ascending keys, while IntMap traverses positive keys first, then negative keys.

  • Foldable1 NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal

    Traverses elements in order of ascending keys

    WARNING: fold1 and foldMap1 are different than fold and foldMap for the IntMap instance of Foldable. They traverse elements in order of ascending keys, while IntMap traverses positive keys first, then negative keys.

  • Eq1 NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Ord1 NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Read1 NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Show1 NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Comonad NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal

    extract gets the value at the minimal key, and duplicate produces a map of maps comprised of all keys from the original map greater than or equal to the current key.

  • Alt NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Traversable1 NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal

    Traverses elements in order of ascending keys

    WARNING: traverse1 and sequence1 are different traverse and sequence for the IntMap instance of Traversable. They traverse elements in order of ascending keys, while IntMap traverses positive keys first, then negative keys.

  • Invariant NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • FoldableWithIndex Int NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • FunctorWithIndex Int NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • TraversableWithIndex Int NEIntMapDefined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • IsList (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Eq a => Eq (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Data a => Data (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Ord a => Ord (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Read e => Read (NEIntMap e)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Show a => Show (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • Semigroup (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal

    Left-biased union

  • NFData a => NFData (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • FromJSON a => FromJSON (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • ToJSON a => ToJSON (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • IsNonEmptyList (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty.List
  • type Item (NEIntMap a) = (Key, a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
valuesingleton :: Key -> a -> NEIntMap a
#

O(1). A map with a single element.

singleton 1 'a'        == fromList ((1, 'a') :| [])
size (singleton 1 'a') == 1
valuenonEmptyMap :: IntMap a -> Maybe (NEIntMap a)
#

O(log n). Smart constructor for an NEIntMap from a IntMap. Returns Nothing if the IntMap was originally actually empty, and Just n with an NEIntMap, if the IntMap was not empty.

nonEmptyMap and maybe Data.IntMap.empty toMap form an isomorphism: they are perfect structure-preserving inverses of eachother.

See IsNonEmpty for a pattern synonym that lets you "match on" the possiblity of a IntMap being an NEIntMap.

nonEmptyMap (Data.IntMap.fromList [(3,"a"), (5,"b")]) == Just (fromList ((3,"a") :| [(5,"b")]))
valuefromList :: NonEmpty (Key, a) -> NEIntMap a
#

O(n*log n). Build a non-empty map from a non-empty list of key/value pairs. See also fromAscList. If the list contains more than one value for the same key, the last value for the key is retained.

fromList ((5,"a") :| [(3,"b"), (5, "c")]) == fromList ((5,"c") :| [(3,"b")])
fromList ((5,"c") :| [(3,"b"), (5, "a")]) == fromList ((5,"a") :| [(3,"b")])
valuetoList :: NEIntMap a -> NonEmpty (Key, a)
#

O(n). Convert the map to a non-empty list of key/value pairs.

toList (fromList ((5,"a") :| [(3,"b")])) == ((3,"b") :| [(5,"a")])
valuemap :: (a -> b) -> NEIntMap a -> NEIntMap b
#

O(n). IntMap a function over all values in the map.

map (++ "x") (fromList ((5,"a") :| [(3,"b")])) == fromList ((3, "bx") :| [(5, "ax")])
valueinsertWith :: (a -> a -> a) -> Key -> a -> NEIntMap a -> NEIntMap a
#

O(log n). Insert with a function, combining new value and old value. insertWith f key value mp will insert the pair (key, value) into mp if key does not exist in the map. If the key does exist, the function will insert the pair (key, f new_value old_value).

See Data.IntMap.NonEmpty.insertIntMapWith for a version where the first argument is a IntMap.

insertWith (++) 5 "xxx" (fromList ((5,"a") :| [(3,"b")])) == fromList ((3, "b") :| [(5, "xxxa")])
insertWith (++) 7 "xxx" (fromList ((5,"a") :| [(3,"b")])) == fromList ((3, "b") :| [(5, "a"), (7, "xxx")])
valueunion :: NEIntMap a -> NEIntMap a -> NEIntMap a
#

O(m*log(n/m + 1)), m <= n. The expression (union t1 t2) takes the left-biased union of t1 and t2. It prefers t1 when duplicate keys are encountered, i.e. (union == unionWith const).

union (fromList ((5, "a") :| [(3, "b")])) (fromList ((5, "A") :| [(7, "C")])) == fromList ((3, "b") :| [(5, "a"), (7, "C")])
valueunions :: Foldable1 f => f (NEIntMap a) -> NEIntMap a
#

The left-biased union of a non-empty list of maps.

unions (fromList ((5, "a") :| [(3, "b")]) :| [fromList ((5, "A") :| [(7, "C")]), fromList ((5, "A3") :| [(3, "B3")])])
    == fromList [(3, "b"), (5, "a"), (7, "C")]
unions (fromList ((5, "A3") :| [(3, "B3")]) :| [fromList ((5, "A") :| [(7, "C")]), fromList ((5, "a") :| [(3, "b")])])
    == fromList ((3, "B3") :| [(5, "A3"), (7, "C")])
valueelems :: NEIntMap a -> NonEmpty a
#

O(n). Return all elements of the map in the ascending order of their keys.

elems (fromList ((5,"a") :| [(3,"b")])) == ("b" :| ["a"])
valuesize :: NEIntMap a -> Int
#

O(1). The number of elements in the map. Guaranteed to be greater than zero.

size (singleton 1 'a')                          == 1
size (fromList ((1,'a') :| [(2,'c'), (3,'b')])) == 3
valuetoMap :: NEIntMap a -> IntMap a
#

O(log n). Convert a non-empty map back into a normal possibly-empty map, for usage with functions that expect IntMap.

Can be thought of as "obscuring" the non-emptiness of the map in its type. See the Data.IntMap.NonEmpty.IsNotEmpty pattern.

nonEmptyMap and maybe Data.IntMap.empty toMap form an isomorphism: they are perfect structure-preserving inverses of eachother.

toMap (fromList ((3,"a") :| [(5,"b")])) == Data.IntMap.fromList [(3,"a"), (5,"b")]

Folds

6 declarations
valuefoldr :: (a -> b -> b) -> b -> NEIntMap a -> b
#

O(n). Fold the values in the map using the given right-associative binary operator, such that foldr f z == foldr f z . elems.

elemsList map = foldr (:) [] map
let f a len = len + (length a)
foldr f 0 (fromList ((5,"a") :| [(3,"bbb")])) == 4
valuefoldr' :: (a -> b -> b) -> b -> NEIntMap a -> b
#

O(n). A strict version of foldr. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldr1 :: (a -> a -> a) -> NEIntMap a -> a
#

O(n). A version of foldr that uses the value at the maximal key in the map as the starting value.

Note that, unlike foldr1 for IntMap, this function is total if the input function is total.

valuefoldl :: (a -> b -> a) -> a -> NEIntMap b -> a
#

O(n). Fold the values in the map using the given left-associative binary operator, such that foldl f z == foldl f z . elems.

elemsList = reverse . foldl (flip (:)) []
let f len a = len + (length a)
foldl f 0 (fromList ((5,"a") :| [(3,"bbb")])) == 4
valuefoldl' :: (a -> b -> a) -> a -> NEIntMap b -> a
#

O(n). A strict version of foldl. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldl1 :: (a -> a -> a) -> NEIntMap a -> a
#

O(n). A version of foldl that uses the value at the minimal key in the map as the starting value.

Note that, unlike foldl1 for IntMap, this function is total if the input function is total.

Traversals

3 declarations
valuetraverseWithKey
  1. :: Applicative t
  2. => Key -> a -> t b
  3. -> NEIntMap a
  4. -> t (NEIntMap b)
#

O(n). traverseWithKey f m == fromList $ traverse ((k, v) -> (,) k $ f k v) (toList m) That is, behaves exactly like a regular traverse except that the traversing function also has access to the key associated with a value.

Use traverseWithKey1 whenever possible (if your Applicative also has Apply instance). This version is provided only for types that do not have Apply instance, since Apply is not at the moment (and might not ever be) an official superclass of Applicative.

WARNING: Differs from Data.IntMap.traverseWithKey, which traverses positive items first, then negative items.

traverseWithKey f = unwrapApplicative . traverseWithKey1 (\k -> WrapApplicative . f k)
valuetraverseWithKey1
  1. :: Apply t
  2. => Key -> a -> t b
  3. -> NEIntMap a
  4. -> t (NEIntMap b)
#

O(n). traverseWithKey1 f m == fromList $ traverse1 ((k, v) -> (,) k $ f k v) (toList m)

That is, behaves exactly like a regular traverse1 except that the traversing function also has access to the key associated with a value.

WARNING: Differs from Data.IntMap.traverseWithKey, which traverses positive items first, then negative items.

Is more general than traverseWithKey, since works with all Apply, and not just Applicative.

Unsafe IntMap Functions

2 declarations
valueinsertMinMap :: Key -> a -> IntMap a -> IntMap a
#

O(log n). Insert new key and value into a map where keys are strictly greater than the new key. That is, the new key must be strictly less than all keys present in the IntMap. /The precondition is not checked./

At the moment this is simply an alias for Data.IntSet.insert, but it's left here as a placeholder in case this eventually gets implemented in a more efficient way.

valueinsertMaxMap :: Key -> a -> IntMap a -> IntMap a
#

O(log n). Insert new key and value into a map where keys are strictly less than the new key. That is, the new key must be strictly greater than all keys present in the IntMap. /The precondition is not checked./

At the moment this is simply an alias for Data.IntSet.insert, but it's left here as a placeholder in case this eventually gets implemented in a more efficient way.

Debug

1 declaration