This repository has been archived by the owner on Oct 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from purescript/gen
Add generators for `Map` and `StrMap`
- Loading branch information
Showing
5 changed files
with
58 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Data.Map.Gen where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Gen (class MonadGen, chooseInt, resize, sized, unfoldable) | ||
import Control.Monad.Rec.Class (class MonadRec) | ||
import Data.Map (Map, fromFoldable) | ||
import Data.Tuple (Tuple(..)) | ||
import Data.List (List) | ||
|
||
-- | Generates a `Map` using the specified key and value generators. | ||
genMap | ||
:: forall m a b | ||
. MonadRec m | ||
=> MonadGen m | ||
=> Ord a | ||
=> m a | ||
-> m b | ||
-> m (Map a b) | ||
genMap genKey genValue = sized \size -> do | ||
newSize <- chooseInt 0 size | ||
resize (const newSize) $ | ||
(fromFoldable :: List (Tuple a b) -> Map a b) | ||
<$> unfoldable (Tuple <$> genKey <*> genValue) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Data.StrMap.Gen where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Gen (class MonadGen, chooseInt, resize, sized, unfoldable) | ||
import Control.Monad.Rec.Class (class MonadRec) | ||
import Data.StrMap (StrMap, fromFoldable) | ||
import Data.Tuple (Tuple(..)) | ||
import Data.List (List) | ||
|
||
-- | Generates a `StrMap` using the specified key and value generators. | ||
genStrMap | ||
:: forall m a | ||
. MonadRec m | ||
=> MonadGen m | ||
=> m String | ||
-> m a | ||
-> m (StrMap a) | ||
genStrMap genKey genValue = sized \size -> do | ||
newSize <- chooseInt 0 size | ||
resize (const newSize) $ | ||
(fromFoldable :: List (Tuple String a) -> StrMap a) | ||
<$> unfoldable (Tuple <$> genKey <*> genValue) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters