Hoogle Search

Within LTS Haskell 24.4 (ghc-9.10.2)

Note that Stackage only displays results for the latest LTS and Nightly snapshot. Learn more.

  1. fromListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a

    containers Data.IntMap.Strict

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWith.

    fromListWith (++) [(5,"a"), (5,"b"), (3,"x"), (5,"c")] == fromList [(3, "x"), (5, "cba")]
    fromListWith (++) [] == empty
    
    Note the reverse ordering of "cba" in the example. The symmetric combining function f is applied in a left-fold over the list, as f new old.

    Performance

    You should ensure that the given f is fast with this order of arguments. Symmetric functions may be slow in one order, and fast in another. For the common case of collecting values of matching keys in a list, as above: The complexity of (++) a b is <math>, so it is fast when given a short list as its first argument. Thus:
    fromListWith       (++)  (replicate 1000000 (3, "x"))   -- O(n),  fast
    fromListWith (flip (++)) (replicate 1000000 (3, "x"))   -- O(n²), extremely slow
    
    because they evaluate as, respectively:
    fromList [(3, "x" ++ ("x" ++ "xxxxx..xxxxx"))]   -- O(n)
    fromList [(3, ("xxxxx..xxxxx" ++ "x") ++ "x")]   -- O(n²)
    
    Thus, to get good performance with an operation like (++) while also preserving the same order as in the input list, reverse the input:
    fromListWith (++) (reverse [(5,"a"), (5,"b"), (5,"c")]) == fromList [(5, "abc")]
    
    and it is always fast to combine singleton-list values [v] with fromListWith (++), as in:
    fromListWith (++) $ reverse $ map (\(k, v) -> (k, [v])) someListOfTuples
    

  2. fromListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a

    containers Data.IntMap.Strict

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey'.

    let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value
    fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")]
    fromListWithKey f [] == empty
    
    Also see the performance note on fromListWith.

  3. fromListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a

    containers Data.IntMap.Strict.Internal

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWith.

    fromListWith (++) [(5,"a"), (5,"b"), (3,"x"), (5,"c")] == fromList [(3, "x"), (5, "cba")]
    fromListWith (++) [] == empty
    
    Note the reverse ordering of "cba" in the example. The symmetric combining function f is applied in a left-fold over the list, as f new old.

    Performance

    You should ensure that the given f is fast with this order of arguments. Symmetric functions may be slow in one order, and fast in another. For the common case of collecting values of matching keys in a list, as above: The complexity of (++) a b is <math>, so it is fast when given a short list as its first argument. Thus:
    fromListWith       (++)  (replicate 1000000 (3, "x"))   -- O(n),  fast
    fromListWith (flip (++)) (replicate 1000000 (3, "x"))   -- O(n²), extremely slow
    
    because they evaluate as, respectively:
    fromList [(3, "x" ++ ("x" ++ "xxxxx..xxxxx"))]   -- O(n)
    fromList [(3, ("xxxxx..xxxxx" ++ "x") ++ "x")]   -- O(n²)
    
    Thus, to get good performance with an operation like (++) while also preserving the same order as in the input list, reverse the input:
    fromListWith (++) (reverse [(5,"a"), (5,"b"), (5,"c")]) == fromList [(5, "abc")]
    
    and it is always fast to combine singleton-list values [v] with fromListWith (++), as in:
    fromListWith (++) $ reverse $ map (\(k, v) -> (k, [v])) someListOfTuples
    

  4. fromListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a

    containers Data.IntMap.Strict.Internal

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey'.

    let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value
    fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")]
    fromListWithKey f [] == empty
    
    Also see the performance note on fromListWith.

  5. fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a

    containers Data.Map.Internal

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWith.

    fromListWith (++) [(5,"a"), (5,"b"), (3,"x"), (5,"c")] == fromList [(3, "x"), (5, "cba")]
    fromListWith (++) [] == empty
    
    Note the reverse ordering of "cba" in the example. The symmetric combining function f is applied in a left-fold over the list, as f new old.

    Performance

    You should ensure that the given f is fast with this order of arguments. Symmetric functions may be slow in one order, and fast in another. For the common case of collecting values of matching keys in a list, as above: The complexity of (++) a b is <math>, so it is fast when given a short list as its first argument. Thus:
    fromListWith       (++)  (replicate 1000000 (3, "x"))   -- O(n),  fast
    fromListWith (flip (++)) (replicate 1000000 (3, "x"))   -- O(n²), extremely slow
    
    because they evaluate as, respectively:
    fromList [(3, "x" ++ ("x" ++ "xxxxx..xxxxx"))]   -- O(n)
    fromList [(3, ("xxxxx..xxxxx" ++ "x") ++ "x")]   -- O(n²)
    
    Thus, to get good performance with an operation like (++) while also preserving the same order as in the input list, reverse the input:
    fromListWith (++) (reverse [(5,"a"), (5,"b"), (5,"c")]) == fromList [(5, "abc")]
    
    and it is always fast to combine singleton-list values [v] with fromListWith (++), as in:
    fromListWith (++) $ reverse $ map (\(k, v) -> (k, [v])) someListOfTuples
    

  6. fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

    containers Data.Map.Internal

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey.

    let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value
    fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")]
    fromListWithKey f [] == empty
    
    Also see the performance note on fromListWith.

  7. fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a

    containers Data.Map.Lazy

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWith.

    fromListWith (++) [(5,"a"), (5,"b"), (3,"x"), (5,"c")] == fromList [(3, "x"), (5, "cba")]
    fromListWith (++) [] == empty
    
    Note the reverse ordering of "cba" in the example. The symmetric combining function f is applied in a left-fold over the list, as f new old.

    Performance

    You should ensure that the given f is fast with this order of arguments. Symmetric functions may be slow in one order, and fast in another. For the common case of collecting values of matching keys in a list, as above: The complexity of (++) a b is <math>, so it is fast when given a short list as its first argument. Thus:
    fromListWith       (++)  (replicate 1000000 (3, "x"))   -- O(n),  fast
    fromListWith (flip (++)) (replicate 1000000 (3, "x"))   -- O(n²), extremely slow
    
    because they evaluate as, respectively:
    fromList [(3, "x" ++ ("x" ++ "xxxxx..xxxxx"))]   -- O(n)
    fromList [(3, ("xxxxx..xxxxx" ++ "x") ++ "x")]   -- O(n²)
    
    Thus, to get good performance with an operation like (++) while also preserving the same order as in the input list, reverse the input:
    fromListWith (++) (reverse [(5,"a"), (5,"b"), (5,"c")]) == fromList [(5, "abc")]
    
    and it is always fast to combine singleton-list values [v] with fromListWith (++), as in:
    fromListWith (++) $ reverse $ map (\(k, v) -> (k, [v])) someListOfTuples
    

  8. fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

    containers Data.Map.Lazy

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey.

    let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value
    fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")]
    fromListWithKey f [] == empty
    
    Also see the performance note on fromListWith.

  9. fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a

    containers Data.Map.Strict

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWith.

    fromListWith (++) [(5,"a"), (5,"b"), (3,"x"), (5,"c")] == fromList [(3, "x"), (5, "cba")]
    fromListWith (++) [] == empty
    
    Note the reverse ordering of "cba" in the example. The symmetric combining function f is applied in a left-fold over the list, as f new old.

    Performance

    You should ensure that the given f is fast with this order of arguments. Symmetric functions may be slow in one order, and fast in another. For the common case of collecting values of matching keys in a list, as above: The complexity of (++) a b is <math>, so it is fast when given a short list as its first argument. Thus:
    fromListWith       (++)  (replicate 1000000 (3, "x"))   -- O(n),  fast
    fromListWith (flip (++)) (replicate 1000000 (3, "x"))   -- O(n²), extremely slow
    
    because they evaluate as, respectively:
    fromList [(3, "x" ++ ("x" ++ "xxxxx..xxxxx"))]   -- O(n)
    fromList [(3, ("xxxxx..xxxxx" ++ "x") ++ "x")]   -- O(n²)
    
    Thus, to get good performance with an operation like (++) while also preserving the same order as in the input list, reverse the input:
    fromListWith (++) (reverse [(5,"a"), (5,"b"), (5,"c")]) == fromList [(5, "abc")]
    
    and it is always fast to combine singleton-list values [v] with fromListWith (++), as in:
    fromListWith (++) $ reverse $ map (\(k, v) -> (k, [v])) someListOfTuples
    

  10. fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

    containers Data.Map.Strict

    Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey.

    let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value
    fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")]
    fromListWithKey f [] == empty
    
    Also see the performance note on fromListWith.

Page 39 of many | Previous | Next