Code Monkey home page Code Monkey logo

random-extra's Introduction

elm-community/random-extra

This library includes lots of extra helper functions for the Random module. It makes generating pseudo-random values much easier.

If you want to add one or find a bug, please open an issue. If you want to contribute, you can open a PR adding tests.

Changelog

3.2.0

  • Add Random.List.choices
  • Use a uniform shuffle for both Random.List.shuffle and Random.Array.shuffle.

3.1.0

  • Add sequence and traverse functions
  • Improve the performance of Random.List.shuffle and Random.Array.shuffle, especially on long lists/arrays.

3.0.0

  • Update for Elm 0.19, including switching to elm/random.
  • Remove Color module.
  • Rename Random.Date.day to Random.Date.weekday.
  • Remove many trivial functions in Random.Date.
  • Add Random.Extra.bool.
  • Remove Random.Extra.constant; it's present in the official library.
  • Change the signatures of Random.Extra.choices and Random.Extra.frequency to require an element and a list, avoid the issue of an empty list.

2.0.0

  • Update dependencies for Elm 0.18.
  • Remove flatMap as core's andThen is identical.
  • Rename flatMapN to andThenN, for similar reasons.
  • Rename together to combine; see #1.
  • Change signature of Random.Extra.Float.normal; see #2.
  • Add Random.Extra.List module; see #4.

random-extra's People

Contributors

bugq avatar janiczek avatar jvoigtlaender avatar league avatar mgold avatar nicknovitski avatar owanturist avatar rl-king avatar theseamau5 avatar zwilias avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

random-extra's Issues

Generator aware Folding

I think having Generator adapted fold functions might help make cleaner code in some cases. (The idea is not new.)

Signatures like:

foldl : (a -> b -> Generator b) -> Generator b -> List a -> Generator b
foldr : (a -> b -> Generator b) -> Generator b -> List a -> Generator b

and an implementation might look something like (with need for improved naming of variables):

random_foldl f start list =
    let
        g : a -> Generator b -> Generator b
        g value generator = generator |> Random.andThen (\ack -> f value ack)
    in
    List.foldl g start list

Do you think the signatures make sense? I guess they might be suitable in Random.List module.

Release major version

I recently merged to breaking changes, and I want to do a review before releasing a major version. I'm busy with elm-test stuff at the moment so I will need to come back to this in a few days.

trouble trying to import random-extra modules on elm 0.18

i'm using random-extra 2.0.0 compatible version with elm 0.18 but in spite of the package successfully installation and present on the packages folder of elm-stuff the compiler gives an error when trying to import one of the modules, say Random.String for instance, saying it cannot find the module in question. i'm on win10.

the only dependency for random-extra is elm-core, which my project uses version 5.1.1 which fits the spec for random-extra. i have tried to clean exact-dependencies.json with no avail, to reinstall everything from scratch, and also to clone the src locally and add a ref to it on the source-directories section of elm-package but the error persist on either cases. any sugestions are welcome. thx

Non-uniform shuffle in 3.1.0

As of 3.1.0, Random.List.shuffle produces non-uniform shuffle. Shuffling looks uniform(ish) when reverting to 3.0.0. The relevant PRs are #14 and #17 (hi @owanturist!).

@miniBill created an Ellie, which I altered slightly to produce 20 independent rows, each with a List.range 1 50 shuffled. Here are 9 offenders from a single run, with added emphasis for certain regularities:

14 7 6 5 47 29 27 19 4 11 41 17 40 16 38 35 13 3 30 10 26 46 45 12 9 43 2 42 44 1 24 48 34 23 50 22 32 37 28 33 18 25 21 36 49 39 20 31 15 8
11 9 8 5 4 3 1 50 43 26 41 25 22 46 30 49 42 39 15 31 36 40 20 38 6 18 35 37 21 29 44 28 32 14 13 45 12 23 24 34 48 19 7 17 33 16 47 27 10 2
28 44 43 41 40 23 27 25 22 21 19 2 17 26 16 15 14 12 18 39 1 31 10 46 29 5 37 11 33 45 20 13 4 9 3 42 30 7 47 50 48 32 49 36 24 34 35 8 6 38
48 45 44 43 42 41 40 29 9 37 36 7 1 25 35 24 23 20 18 50 17 6 39 4 49 3 28 19 12 34 27 15 5 26 14 2 38 13 22 33 46 16 21 32 31 8 11 10 30 47
16 15 21 14 9 8 5 4 2 40 18 39 38 36 34 33 7 37 47 31 13 19 1 41 11 30 6 3 49 46 43 29 12 28 45 42 22 20 26 32 10 50 24 23 27 48 17 35 25 44
44 43 42 39 35 34 33 32 28 26 29 6 5 25 22 4 38 10 7 2 18 31 46 13 21 20 40 11 41 50 36 9 37 15 14 3 17 8 23 16 12 45 49 1 47 27 19 48 30 24
49 48 47 45 44 39 38 37 31 28 34 27 24 43 23 22 33 21 32 2 20 9 26 14 1 5 41 42 8 13 7 12 4 29 40 19 3 50 36 18 25 15 16 30 11 35 46 6 10 17
36 35 34 33 32 31 29 28 26 25 14 50 24 43 13 21 27 45 3 12 20 11 19 49 10 18 5 47 42 9 48 37 39 8 15 16 17 41 22 7 44 4 30 1 23 38 6 40 46 2
20 18 17 16 30 29 12 21 24 9 34 28 15 14 8 47 32 41 7 23 31 22 6 27 5 3 44 43 2 40 45 35 26 11 46 50 49 10 42 39 37 36 48 1 13 38 33 4 19 25

Provide code sample for `person` for Random.Extra.andMap

First, thank you for the hard work and time y'all put in to the this package and thank you for taking the time to consider & respond to this issue.

In the code sample for Random.Extra.andMap, the code sample references person but doesn't explicitly define person. I know that it's probably a type alias somewhere but I don't know if it's an already existing person record or if it's supposed to be a Person constructor.

sequence and combine seem to be two different implementations of the same function

{-| Turn a list of generators into a generator of lists.
-}
combine : List (Generator a) -> Generator (List a)
combine generators =
    case generators of
        [] ->
            constant []

        g :: gs ->
            Random.map2 (::) g (combine gs)
{-| Start with a list of generators, and turn them into a generator that returns a list.
-}
sequence : List (Generator a) -> Generator (List a)
sequence =
    List.foldr (Random.map2 (::)) (Random.constant [])

The slightly different documentation can also be confusing.

Update to Elm 0.19?

Are there any plans to update this package to Elm 0.19? I'm not actually using it currently, but I will soon need a random list shuffling function. I was pleased to see there was one in this package, but the implementation looked somewhat inefficient so I took a stab at writing my own. The updated implementation is much faster (255% faster on a list of length 10, 438% on a list of length 20, 1196% for 50 and 2654% for 100). I'd be happy to contribute the new implementation to this package, but to make it fully robust it would really need independentSeed in the 0.19 Random package, so I wasn't sure if it was worth making a pull request against a 0.18 version.

Shuffle function for List wanted?

Hi there,

I noticed that while there's a shuffle function for Array, I didn't see one for List. I've been sharing a gist I wrote that provides a list shuffling implemention with users on Slack, but I could clean it up and add it to Random.Extra. Is this something you'd be interested in?

Update changelog in readme

The readme makes it sound like the package hasn't been updated since 0.18. You should add something about 0.19 support. :)

Debug does not work

This is my elm.json file.

{
    "type": "application",
    "source-directories": [
        "src"
    ],
    "elm-version": "0.19.0",
    "dependencies": {
        "direct": {
            "elm/browser": "1.0.1",
            "elm/core": "1.0.0",
            "elm/html": "1.0.0",
            "elm/random": "1.0.0",
            "elm/svg": "1.0.1",
            "elm-community/random-extra": "3.0.0"
        },
        "indirect": {
            "elm/json": "1.0.0",
            "elm/time": "1.0.0",
            "elm/url": "1.0.0",
            "elm/virtual-dom": "1.0.2"
        }
    },
    "test-dependencies": {
        "direct": {},
        "indirect": {}
    }
}

I try to generate random Array with

faces : Random.Generator (Array.Array Int)
faces =
    Random.Array.array 2 (Random.int 1 6)

This array is accessed with this code only

let
    newFaceOne = Maybe.withDefault 0 (Array.get 0 newFaces)
    newFaceTwo = Maybe.withDefault 0 (Array.get 1 newFaces)
in

When I run make elm allways throws error.

elm make src/Main.elm  --debug
Dependencies loaded from local cache.
Dependencies ready!                
Success! Compiled 1 module.                                          
elm: Map.!: given key is not an element in the map
CallStack (from HasCallStack):
  error, called at ./Data/Map/Internal.hs:610:17 in containers-0.5.11.0-K2TDqgYtGUcKxAY1UqVZ3R:Data.Map.Internal

This error does not happen when I don't use debug. But when I use it happens. Also this happens only when this code is present whit it's alternative Random.list it works just fine. I'm just not sure if this is a good place for this bug report and it is not an error in debugger itself.

Adding chain and repeat (Markov Chains)

Wrote two new combinators that let you create Markov chains with lists

chain : a -> List (a -> Generator a) -> Generator (List a)
repeat : Int -> a -> (a -> Generator a) -> Generator (List a)

Repeat is defined by chain.

If you are interested I could submit a PR.

upgrade to elm 0.18

I ran elm-upgrade on my program and saw this:

WARNING! 1 of your dependencies have not yet been upgraded to 
support Elm 0.18. You can create an issue to request the packages be 
upgraded here:
  - https://github.com/elm-community/random-extra/issues

I'll try to create a PR myself

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.