Relude.Extra.Map

relude-1.2.2.0: Safe, performant, user-friendly and lightweight Haskell Standard Library

Copyright | (c) 2018-2023 Kowainik
License | MIT
Maintainer | Kowainik <xrom.xkov@gmail.com>
Stability | Stable
Portability | Portable
Safe Haskell | Trustworthy
Language | Haskell2010

Relude.Extra.Map

Contents

- To pairs

Description

Contains implementation of polymorphic type classes for data types `Set` and `Map`.

Synopsis

- class StaticMap t where
  - type Key t
  - type Val t
  - size :: t -> Int
  - lookup :: Key t -> t -> Maybe (Val t)
  - member :: Key t -> t -> Bool
- class StaticMap t => DynamicMap t where
  - insert :: Key t -> Val t -> t -> t
  - insertWith :: (Val t -> Val t -> Val t) -> Key t -> Val t -> t -> t
  - delete :: Key t -> t -> t
  - alter :: (Maybe (Val t) -> Maybe (Val t)) -> Key t -> t -> t
- (!?) :: StaticMap t => t -> Key t -> Maybe (Val t)
- notMember :: StaticMap t => Key t -> t -> Bool
- lookupDefault :: StaticMap t => Val t -> Key t -> t -> Val t
- toPairs :: (IsList t, Item t ~ (a, b)) => t -> [(a, b)]
- keys :: (IsList t, Item t ~ (a, b)) => t -> [a]
- elems :: (IsList t, Item t ~ (a, b)) => t -> [b]

# Documentation

class StaticMap t where #

Read-only map or set. Contains polymorphic functions which work for both sets and maps.

Since: 0.1.0

Associated Types

type Key t #

type Val t #

Methods

size :: t -> Int #

lookup :: Key t -> t -> Maybe (Val t) #

member :: Key t -> t -> Bool #

#### Instances

Instances details

StaticMap IntSet # | Since: 0.1.0
Instance details Defined in Relude.Extra.Map Associated Types
type Key IntSet |
Instance details Defined in Relude.Extra.Map type Key IntSet = Int
type Val IntSet |
Instance details Defined in Relude.Extra.Map type Val IntSet = Int
Methods size :: IntSet -> Int # lookup :: Key IntSet -> IntSet -> Maybe (Val IntSet) # member :: Key IntSet -> IntSet -> Bool #
StaticMap (IntMap v) # | Since: 0.1.0
Instance details Defined in Relude.Extra.Map Associated Types
type Key (IntMap v) |
Instance details Defined in Relude.Extra.Map type Key (IntMap v) = Int
type Val (IntMap v) |
Instance details Defined in Relude.Extra.Map type Val (IntMap v) = v
Methods size :: IntMap v -> Int # lookup :: Key (IntMap v) -> IntMap v -> Maybe (Val (IntMap v)) # member :: Key (IntMap v) -> IntMap v -> Bool #
Ord a => StaticMap (Set a) # | Since: 0.1.0
Instance details Defined in Relude.Extra.Map Associated Types
type Key (Set a) |
Instance details Defined in Relude.Extra.Map type Key (Set a) = a
type Val (Set a) |
Instance details Defined in Relude.Extra.Map type Val (Set a) = a
Methods size :: Set a -> Int # lookup :: Key (Set a) -> Set a -> Maybe (Val (Set a)) # member :: Key (Set a) -> Set a -> Bool #
Hashable a => StaticMap (HashSet a) # | Since: 0.1.0
Instance details Defined in Relude.Extra.Map Associated Types
type Key (HashSet a) |
Instance details Defined in Relude.Extra.Map type Key (HashSet a) = a
type Val (HashSet a) |
Instance details Defined in Relude.Extra.Map type Val (HashSet a) = a
Methods size :: HashSet a -> Int # lookup :: Key (HashSet a) -> HashSet a -> Maybe (Val (HashSet a)) # member :: Key (HashSet a) -> HashSet a -> Bool #
Ord k => StaticMap (Map k v) # | Since: 0.1.0
Instance details Defined in Relude.Extra.Map Associated Types
type Key (Map k v) |
Instance details Defined in Relude.Extra.Map type Key (Map k v) = k
type Val (Map k v) |
Instance details Defined in Relude.Extra.Map type Val (Map k v) = v
Methods size :: Map k v -> Int # lookup :: Key (Map k v) -> Map k v -> Maybe (Val (Map k v)) # member :: Key (Map k v) -> Map k v -> Bool #
Hashable k => StaticMap (HashMap k v) # | Since: 0.1.0
Instance details Defined in Relude.Extra.Map Associated Types
type Key (HashMap k v) |
Instance details Defined in Relude.Extra.Map type Key (HashMap k v) = k
type Val (HashMap k v) |
Instance details Defined in Relude.Extra.Map type Val (HashMap k v) = v
Methods size :: HashMap k v -> Int # lookup :: Key (HashMap k v) -> HashMap k v -> Maybe (Val (HashMap k v)) # member :: Key (HashMap k v) -> HashMap k v -> Bool #

class StaticMap t => DynamicMap t where #

Modifiable Map.

Since: 0.1.0

Methods

insert :: Key t -> Val t -> t -> t #

insertWith :: (Val t -> Val t -> Val t) -> Key t -> Val t -> t -> t #

delete :: Key t -> t -> t #

alter :: (Maybe (Val t) -> Maybe (Val t)) -> Key t -> t -> t #

#### Instances

Instances details

DynamicMap (IntMap v) # | Since: 0.1.0
Instance details Defined in Relude.Extra.Map Methods insert :: Key (IntMap v) -> Val (IntMap v) -> IntMap v -> IntMap v # insertWith :: (Val (IntMap v) -> Val (IntMap v) -> Val (IntMap v)) -> Key (IntMap v) -> Val (IntMap v) -> IntMap v -> IntMap v # delete :: Key (IntMap v) -> IntMap v -> IntMap v # alter :: (Maybe (Val (IntMap v)) -> Maybe (Val (IntMap v))) -> Key (IntMap v) -> IntMap v -> IntMap v #
Ord k => DynamicMap (Map k v) # | Since: 0.1.0
Instance details Defined in Relude.Extra.Map Methods insert :: Key (Map k v) -> Val (Map k v) -> Map k v -> Map k v # insertWith :: (Val (Map k v) -> Val (Map k v) -> Val (Map k v)) -> Key (Map k v) -> Val (Map k v) -> Map k v -> Map k v # delete :: Key (Map k v) -> Map k v -> Map k v # alter :: (Maybe (Val (Map k v)) -> Maybe (Val (Map k v))) -> Key (Map k v) -> Map k v -> Map k v #
Hashable k => DynamicMap (HashMap k v) # | Since: 0.1.0
Instance details Defined in Relude.Extra.Map Methods insert :: Key (HashMap k v) -> Val (HashMap k v) -> HashMap k v -> HashMap k v # insertWith :: (Val (HashMap k v) -> Val (HashMap k v) -> Val (HashMap k v)) -> Key (HashMap k v) -> Val (HashMap k v) -> HashMap k v -> HashMap k v # delete :: Key (HashMap k v) -> HashMap k v -> HashMap k v # alter :: (Maybe (Val (HashMap k v)) -> Maybe (Val (HashMap k v))) -> Key (HashMap k v) -> HashMap k v -> HashMap k v #

(!?) :: StaticMap t => t -> Key t -> Maybe (Val t) infixl 9 #

Operator version of `lookup` function.

    >>> let myHashMap = HashMap.fromList [('a', "xxx"), ('b', "yyy")]
    >>> myHashMap !? 'b'
    Just "yyy"

    >>> myHashMap !? 'd'
    Nothing

Since: 0.1.0

notMember :: StaticMap t => Key t -> t -> Bool #

Inverse of `member` function.

    >>> let myHashMap = HashMap.fromList [('a', "xxx"), ('b', "yyy")]
    >>> notMember 'b' myHashMap
    False

    >>> notMember 'c' myHashMap
    True

Since: 0.1.0

lookupDefault #

Arguments

:: StaticMap t |
=> Val t | Default value to return.
-> Key t | Key to search
-> t | Container to search
-> Val t |

Return the value to which the specified key is mapped, or the default value if this map contains no mapping for the key.

    >>> let myHashMap = HashMap.fromList [('a', "xxx"), ('b', "yyy")]
    >>> lookupDefault "zzz" 'b' myHashMap
    "yyy"

    >>> lookupDefault "zzz" 'c' myHashMap
    "zzz"

Since: 0.1.0

# To pairs

toPairs :: (IsList t, Item t ~ (a, b)) => t -> [(a, b)] #

Converts the structure to the list of the key-value pairs.

    >>> toPairs (HashMap.fromList [('a', "xxx"), ('b', "yyy")])
    [('a',"xxx"),('b',"yyy")]

Since: 0.1.0

keys :: (IsList t, Item t ~ (a, b)) => t -> [a] #

Converts the structure to the list of the keys.

    >>> keys (HashMap.fromList [('a', "xxx"), ('b', "yyy")])
    "ab"

Since: 0.1.0

elems :: (IsList t, Item t ~ (a, b)) => t -> [b] #

Converts the structure to the list of the values.

    >>> elems (HashMap.fromList [('a', "xxx"), ('b', "yyy")])
    ["xxx","yyy"]

Since: 0.1.0