Skip to content

Commit adbc107

Browse files
authored
[#29] Add fromFoldableWithIndex (#30)
* Add `fromFoldableWithIndex` * Document behavior on key collision
1 parent 28a6358 commit adbc107

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Add `fromFoldableWithIndex` (#30 by @ptrfrncsmrph)
1011

1112
Bugfixes:
1213

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"devDependencies": {
3737
"purescript-assert": "^6.0.0",
3838
"purescript-minibench": "^4.0.0",
39+
"purescript-ordered-collections": "^3.0.0",
3940
"purescript-quickcheck": "^8.0.1"
4041
}
4142
}

src/Foreign/Object.purs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Foreign.Object
1515
, toAscUnfoldable
1616
, fromFoldable
1717
, fromFoldableWith
18+
, fromFoldableWithIndex
1819
, fromHomogeneous
1920
, delete
2021
, pop
@@ -49,7 +50,7 @@ import Control.Monad.ST as ST
4950
import Data.Array as A
5051
import Data.Eq (class Eq1)
5152
import Data.Foldable (class Foldable, foldl, foldr, for_)
52-
import Data.FoldableWithIndex (class FoldableWithIndex)
53+
import Data.FoldableWithIndex (class FoldableWithIndex, forWithIndex_)
5354
import Data.Function.Uncurried (Fn2, runFn2, Fn4, runFn4)
5455
import Data.FunctorWithIndex (class FunctorWithIndex)
5556
import Data.Maybe (Maybe(..), maybe, fromMaybe)
@@ -221,6 +222,13 @@ fromFoldable l = runST do
221222
ST.foreach (A.fromFoldable l) \(Tuple k v) -> void $ OST.poke k v s
222223
pure s
223224

225+
-- | Create an `Object a` from a `String`-indexed foldable collection
226+
fromFoldableWithIndex :: forall f a. FoldableWithIndex String f => f a -> Object a
227+
fromFoldableWithIndex l = runST do
228+
s <- OST.new
229+
forWithIndex_ l \k v -> OST.poke k v s
230+
pure s
231+
224232
foreign import _lookupST :: forall a r z. Fn4 z (a -> z) String (STObject r a) (ST r z)
225233

226234
-- | Create an `Object a` from a foldable collection of key/value pairs, using the

test/Test/Foreign/Object.purs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Data.FoldableWithIndex (foldlWithIndex, foldrWithIndex, foldMapWithIndex)
1010
import Data.Function (on)
1111
import Data.List as L
1212
import Data.List.NonEmpty as NEL
13+
import Data.Map as M
1314
import Data.Maybe (Maybe(..), fromMaybe)
1415
import Data.Semigroup.First (First(..))
1516
import Data.Semigroup.Last (Last(..))
@@ -150,6 +151,14 @@ objectTests = do
150151
quickCheck (O.lookup "1" nums == Just "one" <?> "invalid lookup - 1")
151152
quickCheck (O.lookup "2" nums == Nothing <?> "invalid lookup - 2")
152153

154+
log "fromFoldableWithIndex"
155+
do
156+
let numsMap = M.fromFoldable [Tuple "0" "zero", Tuple "1" "one", Tuple "2" "two"]
157+
nums = O.fromFoldableWithIndex numsMap
158+
quickCheck (O.lookup "0" nums == Just "zero" <?> "invalid lookup - 0")
159+
quickCheck (O.lookup "1" nums == Just "one" <?> "invalid lookup - 1")
160+
quickCheck (O.lookup "2" nums == Just "two" <?> "invalid lookup - 2")
161+
153162
log "fromFoldableWith const [] = empty"
154163
quickCheck (O.fromFoldableWith const [] == (O.empty :: O.Object Unit)
155164
<?> "was not empty")
@@ -170,6 +179,15 @@ objectTests = do
170179
let f m1 = O.fromFoldable ((O.toUnfoldable m1) :: L.List (Tuple String Int)) in
171180
O.toUnfoldable (f m) == (O.toUnfoldable m :: L.List (Tuple String Int)) <?> show m
172181

182+
log "fromFoldableWithIndex = id"
183+
quickCheck $ \(TestObject m :: _ Int) ->
184+
O.fromFoldableWithIndex m == identity m <?> show m
185+
186+
log "fromFoldableWithIndex = fromFoldable . toUnfoldable"
187+
quickCheck $ \(TestObject m) ->
188+
let f m1 = O.fromFoldable ((O.toUnfoldable m1) :: L.List (Tuple String Int)) in
189+
O.fromFoldableWithIndex m == f m <?> show m
190+
173191
log "fromFoldableWith const = fromFoldable"
174192
quickCheck $ \arr -> O.fromFoldableWith const arr ==
175193
O.fromFoldable (arr :: L.List (Tuple String Int)) <?> show arr

0 commit comments

Comments
 (0)