ndmitchell / hlint

Haskell source code suggestions
Other
1.45k stars 194 forks source link

avoid redundant toList-calls in `containers` package #1570

Open jvoigtlaender opened 5 months ago

jvoigtlaender commented 5 months ago

In the spirit of #1569, various toList calls could be optimized away. But I'm not sure containers is "in scope" since no other rules currently seem to refer to functions from it.

Anyway, here are the proposed rules:

    - warn: {lhs: foldMap f (Data.IntMap.toList x), rhs: Data.IntMap.foldMapWithKey (curry f) x}
    - warn: {lhs: foldr f z (Data.IntMap.toList x), rhs: Data.IntMap.foldrWithKey (curry f) z x}
    - warn: {lhs: foldr' f z (Data.IntMap.toList x), rhs: Data.IntMap.foldrWithKey' (curry f) z x}
    - warn: {lhs: foldl f z (Data.IntMap.toList x), rhs: Data.IntMap.foldlWithKey (curry . f) z x}
    - warn: {lhs: foldl' f z (Data.IntMap.toList x), rhs: Data.IntMap.foldlWithKey' (curry . f) z x}
    - warn: {lhs: null (Data.IntMap.toList x), rhs: Data.IntMap.null x}
    - warn: {lhs: length (Data.IntMap.toList x), rhs: Data.IntMap.size x}

    - warn: {lhs: foldr f z (Data.IntSet.toList x), rhs: Data.IntSet.foldr f z x}
    - warn: {lhs: foldr' f z (Data.IntSet.toList x), rhs: Data.IntSet.foldr' f z x}
    - warn: {lhs: foldl f z (Data.IntSet.toList x), rhs: Data.IntSet.foldl f z x}
    - warn: {lhs: foldl' f z (Data.IntSet.toList x), rhs: Data.IntSet.foldl' f z x}
    - warn: {lhs: null (Data.IntSet.toList x), rhs: Data.IntSet.null x}
    - warn: {lhs: length (Data.IntSet.toList x), rhs: Data.IntSet.size x}
    - warn: {lhs: elem y (Data.IntSet.toList x), rhs: Data.IntSet.member y x}
    - warn: {lhs: notElem y (Data.IntSet.toList x), rhs: Data.IntSet.notMember y x}

    - warn: {lhs: foldMap f (Data.Map.toList x), rhs: Data.Map.foldMapWithKey (curry f) x}
    - warn: {lhs: foldr f z (Data.Map.toList x), rhs: Data.Map.foldrWithKey (curry f) z x}
    - warn: {lhs: foldr' f z (Data.Map.toList x), rhs: Data.Map.foldrWithKey' (curry f) z x}
    - warn: {lhs: foldl f z (Data.Map.toList x), rhs: Data.Map.foldlWithKey (curry . f) z x}
    - warn: {lhs: foldl' f z (Data.Map.toList x), rhs: Data.Map.foldlWithKey' (curry . f) z x}
    - warn: {lhs: null (Data.Map.toList x), rhs: Data.Map.null x}
    - warn: {lhs: length (Data.Map.toList x), rhs: Data.Map.size x}

Similar optimizations would likely also be possible regarding toList from the aeson, vector and unordered-containers packages.