Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.
Merged
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
6 changes: 3 additions & 3 deletions src/Data/StrMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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; };
});
11 changes: 6 additions & 5 deletions src/Data/StrMap.purs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Data.StrMap
, freezeST
, runST
, pureST
, toArrayWithKey
) where

import Prelude
Expand Down Expand Up @@ -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.
Expand Down