From d5ec2b36324de3347c2082ded6b92dbea112e239 Mon Sep 17 00:00:00 2001 From: Andreas Schacker Date: Tue, 6 Jun 2017 19:27:11 +0200 Subject: [PATCH] Export `toArrayWithKey` function --- src/Data/StrMap.js | 6 +++--- src/Data/StrMap.purs | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Data/StrMap.js b/src/Data/StrMap.js index 13d81e4e..ab82f990 100644 --- a/src/Data/StrMap.js +++ b/src/Data/StrMap.js @@ -106,7 +106,7 @@ exports._lookupST = function (no, yes, k, m) { }; }; -function _collect(f) { +function toArrayWithKey(f) { return function (m) { var r = []; for (var k in m) { @@ -118,8 +118,8 @@ function _collect(f) { }; } -exports._collect = _collect; +exports.toArrayWithKey = toArrayWithKey; -exports.keys = Object.keys || _collect(function (k) { +exports.keys = Object.keys || toArrayWithKey(function (k) { return function () { return k; }; }); diff --git a/src/Data/StrMap.purs b/src/Data/StrMap.purs index e5c0b798..9105106d 100644 --- a/src/Data/StrMap.purs +++ b/src/Data/StrMap.purs @@ -39,6 +39,7 @@ module Data.StrMap , freezeST , runST , pureST + , toArrayWithKey ) where import Prelude @@ -219,27 +220,27 @@ fromFoldableWith f l = pureST (do for_ l (\(Tuple k v) -> runFn4 _lookupST v (f v) k s >>= SM.poke s k) pure s) -foreign import _collect :: forall a b . (String -> a -> b) -> StrMap a -> Array b +foreign import toArrayWithKey :: forall a b . (String -> a -> b) -> StrMap a -> Array b -- | Unfolds a map into a list of key/value pairs toUnfoldable :: forall f a. Unfoldable f => StrMap a -> f (Tuple String a) -toUnfoldable = A.toUnfoldable <<< _collect Tuple +toUnfoldable = A.toUnfoldable <<< toArrayWithKey Tuple -- | Unfolds a map into a list of key/value pairs which is guaranteed to be -- | sorted by key toAscUnfoldable :: forall f a. Unfoldable f => StrMap a -> f (Tuple String a) -toAscUnfoldable = A.toUnfoldable <<< A.sortWith fst <<< _collect Tuple +toAscUnfoldable = A.toUnfoldable <<< A.sortWith fst <<< toArrayWithKey Tuple -- Internal toArray :: forall a. StrMap a -> Array (Tuple String a) -toArray = _collect Tuple +toArray = toArrayWithKey Tuple -- | Get an array of the keys in a map foreign import keys :: forall a. StrMap a -> Array String -- | Get a list of the values in a map values :: forall a. StrMap a -> Array a -values = _collect (\_ v -> v) +values = toArrayWithKey (\_ v -> v) -- | Compute the union of two maps, preferring the first map in the case of -- | duplicate keys.