Hoogle Search
Within LTS Haskell 24.3 (ghc-9.10.2)
Note that Stackage only displays results for the latest LTS and Nightly snapshot. Learn more.
fromListMay :: MonadRandom m => [(a, Rational)] -> m (Maybe a)MonadRandom Control.Monad.Random.Class Sample a random value from a weighted list. Return Nothing if the list is empty or the total weight is nonpositive.
fromLists :: Element t => [[t]] -> Matrix thmatrix Numeric.LinearAlgebra.Data Creates a Matrix from a list of lists (considered as rows).
>>> fromLists [[1,2],[3,4],[5,6]] (3><2) [ 1.0, 2.0 , 3.0, 4.0 , 5.0, 6.0 ]
fromListWith :: (Eq k, Hashable k) => (v -> v -> v) -> [(k, v)] -> HashMap k vrio RIO.HashMap Construct a map from a list of elements. Uses the provided function f to merge duplicate entries with (f newVal oldVal).
Examples
Given a list xs, create a map with the number of occurrences of each element in xs:let xs = ['a', 'b', 'a'] in fromListWith (+) [ (x, 1) | x <- xs ] = fromList [('a', 2), ('b', 1)]
Given a list of key-value pairs xs :: [(k, v)], group all values by their keys and return a HashMap k [v].let xs = ('a', 1), ('b', 2), ('a', 3)] in fromListWith (++) [ (k, [v]) | (k, v) <- xs ] = fromList [('a', [3, 1]), ('b', [2])]
Note that the lists in the resulting map contain elements in reverse order from their occurrences in the original list. More generally, duplicate entries are accumulated as follows; this matters when f is not commutative or not associative.fromListWith f [(k, a), (k, b), (k, c), (k, d)] = fromList [(k, f d (f c (f b a)))]
fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k ario RIO.Map 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
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k ario RIO.Map 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.fromListN :: Vector v a => Int -> [a] -> v ario RIO.Vector No documentation available.
fromListN :: Int -> [a] -> Vector ario RIO.Vector.Boxed No documentation available.
fromListN :: Storable a => Int -> [a] -> Vector ario RIO.Vector.Storable No documentation available.
fromListN :: Unbox a => Int -> [a] -> Vector ario RIO.Vector.Unboxed No documentation available.
fromListWithSizeHint :: (HashTable h, Eq k, Hashable k) => Int -> [(k, v)] -> ST s (h s k v)hashtables Data.HashTable.Class Create a hash table from a list of key-value pairs, with a size hint. O(n).