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

StrMap.fromRecord #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"purescript-lists": "^4.0.0",
"purescript-st": "^3.0.0",
"purescript-gen": "^1.1.0",
"purescript-foldable-traversable": "^3.6.1"
"purescript-foldable-traversable": "^3.6.1",
"purescript-typelevel-prelude": "^2.6.0"
},
"devDependencies": {
"purescript-quickcheck": "^4.0.0",
Expand Down
4 changes: 4 additions & 0 deletions src/Data/StrMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ exports._lookupST = function (no, yes, k, m) {
};
};

exports.fromRecordImpl = function(r) {
return r;
};

function toArrayWithKey(f) {
return function (m) {
var r = [];
Expand Down
9 changes: 8 additions & 1 deletion src/Data/StrMap.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Data.StrMap
, toAscUnfoldable
, fromFoldable
, fromFoldableWith
, fromRecord
, delete
, pop
, member
Expand Down Expand Up @@ -46,7 +47,6 @@ import Prelude

import Control.Monad.Eff (Eff, runPure, foreachE)
import Control.Monad.ST as ST

import Data.Array as A
import Data.Eq (class Eq1)
import Data.Foldable (class Foldable, foldl, foldr, for_)
Expand All @@ -60,6 +60,7 @@ import Data.Traversable (class Traversable, traverse)
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
import Data.Tuple (Tuple(..), fst, uncurry)
import Data.Unfoldable (class Unfoldable)
import Type.Row.Homogeneous (class Homogeneous)

-- | `StrMap a` represents a map from `String`s to values of type `a`.
foreign import data StrMap :: Type -> Type
Expand Down Expand Up @@ -235,6 +236,12 @@ fromFoldableWith f l = pureST (do
for_ l (\(Tuple k v) -> runFn4 _lookupST v (f v) k s >>= SM.poke s k)
pure s)

-- | Create a map from a homogeneous record (all attributes have the same type).
fromRecord :: forall r t. Homogeneous r t => Record r -> StrMap t
fromRecord = fromRecordImpl

foreign import fromRecordImpl :: forall r t. Record r -> StrMap t

foreign import toArrayWithKey :: forall a b . (String -> a -> b) -> StrMap a -> Array b

-- | Unfolds a map into a list of key/value pairs
Expand Down
4 changes: 4 additions & 0 deletions test/Test/Data/StrMap.purs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ strMapTests = do
quickCheck (M.lookup "1" nums == Just 2 <?> "invalid lookup - 1")
quickCheck (M.lookup "2" nums == Nothing <?> "invalid lookup - 2")

log "fromRecord"
quickCheck (M.fromRecord {a: 1, b: 2, c: 3}
== M.fromFoldable [Tuple "a" 1, Tuple "b" 2, Tuple "c" 3])

log "toUnfoldable . fromFoldable = id"
quickCheck $ \arr -> let f x = M.toUnfoldable (M.fromFoldable x)
in f (f arr) == f (arr :: L.List (Tuple String Int)) <?> show arr
Expand Down