kudzu-forest/elm-random-tree/3.0.0

NOTE: the latest version is 3.0.1

Rerouted (regenerated, verified). Same repository and tag, but GitHub now builds the zip with different bytes, so it no longer matches the SHA-1 package.elm-lang.org recorded. The Lamdera package server serves a copy recovered from https://github.com/kudzu-forest/elm-random-tree.

What we checked: Rebuilt with git archive from commit ce921ee5bf92 of https://github.com/kudzu-forest/elm-random-tree (zip prefix kudzu-forest-elm-random-tree-ce921ee/, America/Los_Angeles times): the zip is byte-identical to the one package.elm-lang.org recorded, SHA-1 270e255f44195adf4d02408b8a27294f1c8206ad.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/kudzu-forest/elm-random-tree/zipball/3.0.0/

RandomTree.Uniform

This module provides data structure subjected to random choice of elements in uniform probability.

Note

Tree in this module

  • is not allowed to be empty, so
    • the return value of function that reduce the number of element(take, remove, and filter) all return Maybe (Tree a) just in case all the elements are deleted.
    • You have to pass at least one element at the creation, so function like fromList takes one heading element and tailing list.(similar with Random.uniform or Random.weighted.)
  • is not a search tree(the elements are not ordered), so the time complexity of member and remove is O(N).
  • may be unbalanced when remove or filter is called, so when I say "The time complexity is O(log(N))", N denotes the maximal size in the history of the tree so far.

Types

type Tree a

Type that represents binary tree with elements subjected to random picking up in uniform probabilities.

Creation

singleton : a -> Tree a

Returns a Tree that has only one element.

fromList : a -> List a -> Tree a

Returns a Tree that has all elements in given list.

Insertion

insert : a -> Tree a -> Tree a

Inserts a data to an already-existing Tree.

insertList : List a -> Tree a -> Tree a

Inserts all elements of given list into the tree.

Modification

map : (a -> b) -> Tree a -> Tree b

Maps all the content of the tree.

filter : (a -> Bool) -> Tree a -> Maybe (Tree a)

Removes any number of elements which fails to passes the test function given as the first parameter.

remove : a -> Tree a -> Maybe (Tree a)

Removes any number of elements which is the same as the first parameter from the second parameter.

Random Picking Up

get : Tree a -> Generator a

Random generator that generates one of the elements conteined in Tree given as parameter. The time complexity is O(log(N)).

take : Tree a -> Generator ( a, Maybe (Tree a) )

Random generator that generates one of the elements conteined in Tree, paired with the rest part of the tree. For example,

take
        /\
      /    \
    /\    /\
  /\  c  d    e
 a    b

generates

( a, Just  /\    ),
          /    \
        /\    /\
       b    c  d    e


( b,Just   /\    ),
          /    \
        /\    /\
       a    c  d    e


( c,Just  /\    ),
         /    \
      /\     /\
      a    b  d    e


( d,Just   /\    ),
          /    \
        /\    /\
       a    b  c    e

or

( e,Just   /\    )
          /    \
        /\    /\
       a    b  c    d

in probability of 20%. The time complexity is O(log(N)).

replace : a -> Tree a -> Generator ( a, Tree a )

Random generator that replaces one data from the tree and generates a pair consisting of the removed data and resultant tree. For example,

replace x  /\
         /    \
       /\    /\
     /\  c  d    e
    a    b

generates

( a,  /\    ),
     /    \
   /\    /\
 /\  c  d    e
x    b


( b,  /\    ),
     /    \
   /\    /\
 /\  c  d    e
a    x


( c,  /\    ),
     /    \
   /\    /\
 /\  x  d    e
a    b


( d,  /\    ),
     /    \
   /\    /\
 /\  c  x    e
a    b

or

( e,  /\    ),
     /    \
   /\    /\
 /\  c  d    x
a    b

in probability of 20%. The time complexity is O(log(N)).

Status Checking

count : Tree a -> Int

Returns how many elements the tree has.