unisonweb / base

Unison base libraries
https://share.unison-lang.org/@unison/base
18 stars 6 forks source link

Add some Random generators #155

Closed ceedubs closed 1 year ago

ceedubs commented 1 year ago

You can see these here on Share.

It's not straightforward to test random generators, but this watch expression suggests that Random.weighted is producing a distribution that at least resembles the supplied weights:

> splitmix 42 do
  withInitialValue Map.empty do
    g = Random.weighted [
      (1, '?a),
      (2, '?b),
      (1, '?c)
    ]
    Each.run do
      repeat 1000
      Store.modify (Map.putWith (Nat.+) !g 1)
    Store.get |> Map.toList
          ⧩
          [(?a, 239), (?b, 502), (?c, 259)]

Code review

  ⍟ I've added these definitions:

    abilities.Random.char.ascii.control       : '{Random} Char
    abilities.Random.char.ascii.control.doc   : Doc
    abilities.Random.char.ascii.lower         : '{Random} Char
    abilities.Random.char.ascii.lower.doc     : Doc
    abilities.Random.char.ascii.printable     : '{Random} Char
    abilities.Random.char.ascii.printable.doc : Doc
    abilities.Random.char.ascii.upper         : '{Random} Char
    abilities.Random.char.ascii.upper.doc     : Doc
    abilities.Random.char.unicode     : '{Random} Char
    abilities.Random.char.unicode.doc : Doc
    abilities.Random.weighted                 : [(Nat, '{g, Random} a)] ->{g} '{g, Random} a
    abilities.Random.weighted.doc             : Doc
    abilities.Random.weighted.fromStream      : '{g, Stream (Nat, '{g, Random} a)} r
                                                ->{g} '{g, Random} a
    abilities.Random.weighted.fromStream.doc  : Doc
runarorama commented 1 year ago

Fixed up the docs a bit and merged. 🌈⭐