Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

make foldrWithIndex ordered #139

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ instance foldableMap :: Foldable (Map k) where

instance foldableWithIndexMap :: FoldableWithIndex k (Map k) where
foldlWithIndex f z m = foldl (uncurry <<< (flip f)) z $ asList $ toUnfoldable m
foldrWithIndex f z m = foldr (uncurry f) z $ asList $ toUnfoldable m
foldrWithIndex f z m = foldr (uncurry f) z $ asList $ toAscUnfoldable m
foldMapWithIndex f m = foldMap (uncurry f) $ asList $ toUnfoldable m

asList :: forall k v. List (Tuple k v) -> List (Tuple k v)
Expand Down
8 changes: 7 additions & 1 deletion test/Test/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import Control.Monad.Eff.Exception (EXCEPTION)
import Control.Monad.Eff.Random (RANDOM)
import Data.Array as A
import Data.Foldable (foldl, for_, all)
import Data.FoldableWithIndex (foldrWithIndex)
import Data.Function (on)
import Data.List (List(Cons), groupBy, length, nubBy, singleton, sort, sortBy)
import Data.List (List(..), (:), groupBy, length, nubBy, singleton, sort, sortBy)
import Data.List.NonEmpty as NEL
import Data.Map as M
import Data.Map.Gen (genMap)
Expand Down Expand Up @@ -333,3 +334,8 @@ mapTests = do
<> ", mmin: " <> show mmin
<> ", mmax: " <> show mmax
<> ", key: " <> show key

log "foldrWithIndex maintains order"
quickCheck \(TestMap m :: TestMap Int Int) ->
let outList = foldrWithIndex (\i a b -> (Tuple i a) : b) Nil m
in outList == sort outList