Skip to content

Commit 0e6ade6

Browse files
committed
Add fromFoldableWithIndex
Fixes #29
1 parent 28a6358 commit 0e6ade6

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Foreign/Object.purs

Lines changed: 10 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,14 @@ 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 an indexed foldable collection, using the
226+
-- | specified function for converting the index to `String`
227+
fromFoldableWithIndex :: forall f k v. FoldableWithIndex k f => (k -> String) -> f v -> Object v
228+
fromFoldableWithIndex f l = runST do
229+
s <- OST.new
230+
forWithIndex_ l \k v -> OST.poke (f k) v s
231+
pure s
232+
224233
foreign import _lookupST :: forall a r z. Fn4 z (a -> z) String (STObject r a) (ST r z)
225234

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

0 commit comments

Comments
 (0)