Skip to content

fix frequency #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2017
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ install:
- bower install
script:
- npm run -s build
- npm run -s test
after_success:
- >-
test $TRAVIS_TAG &&
Expand Down
7 changes: 7 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,12 @@
"purescript-tuples": "^4.0.0",
"purescript-unfoldable": "^3.0.0",
"purescript-integers": "^3.0.0"
},
"devDependencies": {
"purescript-console": "^3.0.0",
"purescript-assert": "^3.0.0",
"purescript-arrays": "^4.2.1",
"purescript-transformers": "^3.4.0",
"purescript-math": "^2.1.0"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"test": "pulp test",
"build": "pulp build -- --censor-lib --strict"
},
"devDependencies": {
Expand Down
24 changes: 17 additions & 7 deletions src/Control/Monad/Gen.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Control.Monad.Gen.Class (class MonadGen, Size, chooseBool, chooseFloat, c
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM)

import Data.Foldable (class Foldable, length, foldl, foldMap)
import Data.Int as Int
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Monoid.Additive (Additive(..))
import Data.Newtype (alaF)
Expand Down Expand Up @@ -46,15 +45,26 @@ frequency
=> Foldable f
=> NonEmpty f (Tuple Number (m a))
-> m a
frequency (x :| xs) =
frequency (x :| xs) =
let
first = fst x
total = first + alaF Additive foldMap fst xs
in do
pos <- chooseFloat 0.0 total
let n = Int.round (pos / total * length xs)
snd $ if n == 0 then x else fromIndex (n - 1) x xs

in
chooseFloat 0.0 total >>= pick
where
pick pos =
let
initial = go (Tuple 0.0 $ snd x) x
in
snd $ foldl go initial xs
where
go (Tuple weight val) (Tuple currWeight currVal) =
let
nextWeight = weight + currWeight
in
if weight <= pos && pos <= nextWeight
then Tuple nextWeight currVal
else Tuple nextWeight val
-- | Creates a generator that outputs a value chosen from a selection with
-- | uniform probability.
elements :: forall m f a. MonadGen m => Foldable f => NonEmpty f a -> m a
Expand Down
65 changes: 65 additions & 0 deletions test/Test/Frequency.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Test.Frequency where

import Prelude

import Test.Assert (ASSERT, assert)
import Partial.Unsafe (unsafeCrashWith)
import Control.Monad.Eff (Eff)
import Control.Monad.Gen (class MonadGen, frequency)
import Data.NonEmpty ((:|), NonEmpty(..))
import Data.Traversable (sequence)
import Data.Array (replicate, group', length)
import Data.Tuple (Tuple(..))
import Data.Newtype (unwrap)
import Math (remainder)
import Control.Monad.State (State, class MonadState, get, put, evalStateT)


newtype TestGenFrequency a = TestGenFrequency (State Number a)
derive newtype instance testGenFunctor :: Functor TestGenFrequency
derive newtype instance testGenApply :: Apply TestGenFrequency
derive newtype instance testGenBind :: Bind TestGenFrequency
derive newtype instance testGenApplicative :: Applicative TestGenFrequency
derive newtype instance testGenMonad :: Monad TestGenFrequency
derive newtype instance testGenMonadState :: MonadState Number TestGenFrequency

instance testGenMonadGen :: MonadGen TestGenFrequency where
sized _ = unsafeCrashWith "sized should not be called"
resize _ _ = unsafeCrashWith "resize should not be called"
chooseBool = pure unit >>= \_ -> unsafeCrashWith "chooseBool should not be called"
chooseFloat s e = do
c <- get
put (c + 1.0)
pure ((s + c) `remainder` e)
chooseInt _ _ = unsafeCrashWith "chooseFloat should not be called"

runTestGenFrequency :: TestGenFrequency ~> State Number
runTestGenFrequency (TestGenFrequency x) = x

check :: forall r. Eff (assert :: ASSERT | r) Unit
check =
let
abcGen :: TestGenFrequency String
abcGen =
frequency $
( Tuple 10.0 $ pure "A" ) :|
[ Tuple 20.0 $ pure "B"
, Tuple 0.0 $ pure "Z"
, Tuple 30.0 $ pure "C"
, Tuple 40.0 $ pure "D"
, Tuple 50.0 $ pure "E"
, Tuple 50.0 $ pure "F"
]
abcArrGen = sequence $ replicate 200 abcGen
abcArr = runTestGenFrequency abcArrGen `evalStateT` 0.0 # unwrap
actual = group' abcArr <#> \(NonEmpty x xs) -> Tuple (length xs + 1) x
expected =
[ (Tuple 10 "A")
, (Tuple 20 "B")
, (Tuple 30 "C")
, (Tuple 40 "D")
, (Tuple 50 "E")
, (Tuple 50 "F")
]
in
assert (expected == actual)
16 changes: 16 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Test.Main where

import Prelude

import Test.Frequency as Frequency
import Test.Assert (ASSERT)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

type Tests = Eff (console :: CONSOLE, assert :: ASSERT) Unit

main :: Tests
main = do
log "check frequency"
Frequency.check