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

Commit

Permalink
restore old foldrWithIndex
Browse files Browse the repository at this point in the history
any attempt at directly folding is not stack safe
  • Loading branch information
matthewleon committed Jan 17, 2018
1 parent ab72859 commit 5fd2b86
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ module Data.Map
import Prelude

import Data.Eq (class Eq1)
import Data.Foldable (foldl, foldMapDefaultL, class Foldable)
import Data.FoldableWithIndex (class FoldableWithIndex, foldlWithIndex, foldrWithIndex, foldMapWithIndexDefaultL)
import Data.Foldable (foldl, foldr, foldMapDefaultL, class Foldable)
import Data.FoldableWithIndex (class FoldableWithIndex, foldlWithIndex, foldrWithIndex, foldrWithIndexDefault, foldMapWithIndexDefaultL)
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
import Data.List (List(..), (:), length, nub)
import Data.List.Lazy as LL
Expand Down Expand Up @@ -117,19 +117,7 @@ instance foldableWithIndexMap :: FoldableWithIndex k (Map k) where
go acc (left : singleton k v : right : tl)
Three left k1 v1 mid k2 v2 right ->
go acc (left : singleton k1 v1 : mid : singleton k2 v2 : right : tl)
foldrWithIndex f z m = go (m : Nil)
where
go Nil = z
go (hd : tl) = case hd of
Leaf -> go tl
Two Leaf k v Leaf ->
f k v $ go tl
Two Leaf k v right ->
f k v $ go $ right : tl
Two left k v right ->
go $ left : singleton k v : right : tl
Three left k1 v1 mid k2 v2 right ->
go $ left : singleton k1 v1 : mid : singleton k2 v2 : right : tl
foldrWithIndex f z m = foldr (uncurry f) z $ asList $ toUnfoldable m
foldMapWithIndex = foldMapWithIndexDefaultL

asList :: forall k v. List (Tuple k v) -> List (Tuple k v)
Expand Down

0 comments on commit 5fd2b86

Please sign in to comment.