Skip to content

Commit

Permalink
Add a replicate provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Nov 30, 2018
1 parent 0488f77 commit c6ed233
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ a field within the JSON object.
- array(expr [, expr ... ])
- oneOf(arrayExpr)
- oneOf(expr, expr [, expr ... ])
- replicate(number, expr)


## Installation
Expand Down
8 changes: 7 additions & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Main where


import Control.Monad (forever)
import Control.Monad (forever, replicateM)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.State.Strict (StateT)
import qualified Control.Monad.Trans.State.Strict as State
Expand Down Expand Up @@ -112,6 +112,9 @@ asArray o = error $ "Expected an array, but received: " <> show o
--
-- >>> exec "oneOf(37, 42, 21)"
-- Number 21.0
--
-- >>> exec "replicate(randomInt(2, 4), oneOf(37, 42, 21))"
-- Array [Number 42.0,Number 42.0,Number 21.0,Number 42.0]
eval :: Expr -> State Value
eval (IntLiteral x) = pure $ Number $ fromInteger x
eval (StringLiteral x) = pure $ String x
Expand All @@ -133,6 +136,9 @@ eval (FunctionCall "oneOf" [arg]) = do
eval (FunctionCall "oneOf" args) = do
idx <- State.state $ randomR (0, length args - 1)
eval (args !! idx)
eval (FunctionCall "replicate" [num, expr]) = do
num' <- asInt <$> eval num
Array . V.fromList <$> replicateM num' (eval expr)
eval (FunctionCall name _) = pure $ String $ "No random generator for " <> name


Expand Down

0 comments on commit c6ed233

Please sign in to comment.