GithubHelp home page GithubHelp logo

adsteele916 / rand-functors Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 97 KB

A zero-cost Rust abstraction for sampling from and enumerating the outcomes of a random process using the same code.

Home Page: https://crates.io/crates/rand_functors

License: Apache License 2.0

Rust 100.00%

rand-functors's Introduction

rand-functors

rand-functors provides an abstraction over different ways of evaluating random processes expressed as functions of both deterministic and stochastic data. This is achieved using a combination of a type-based version of the Strategy pattern and functional programming's Functor pattern.

A motivating problem for this crate is the code duplication present across these two functions modelling the same random process:

use rand::prelude::*;

fn next_state(mut state: u8) -> u8 {
    state = state.wrapping_add(random());
    if random() {
        state %= 3;
    }
    state
}

fn next_states(state: u8) -> Vec<u8> {
    let mut out: Vec<_> = (0..=255).map(|r| state.wrapping_add(r)).collect();
    out.append(&mut out.iter().copied().map(|i| i % 3).collect());
    out
}

While these functions may appear different, the same random process is embedded in both of them. A random u8 is added to state and then, if a random bool is true, the state will be set to itself modulo 3.

This redundant implementation of the random process could pose issues during a refactor. If one decides to change the %= 3 to a %= 5 in next_state, he or she will need to make the corresponding update in next_states.

Using rand-functors, these two functions can be combined as:

use rand::prelude::*;
use rand_functors::{Functor, RandomStrategy};

fn next_state<S: RandomStrategy>(state: u8) -> S::Functor<u8> {
    let mut out = S::fmap_rand(Functor::pure(state), &mut thread_rng(), |s, r| {
        s.wrapping_add(r)
    });
    out = S::fmap_rand(out, &mut thread_rng(), |s, r| if r { s % 3 } else { s });
    out
}

This new implementation makes next_state generic over a RandomStrategy S. Its return type is also changed to the Functor associated with S. Inside, state is converted from u8 to S::Functor<u8>. The remainder of the function is essentially the same as the original next_state, but each operation a random sample is now wrapped in a call to S::fmap_rand. Calling next_state::<Sampler>(s) would be equivalent to calling next_state(s) before. Similarly, one could call next_state::<Enumerator>(s) instead of using next_states(s), which would require maintaining a separate implementation of the same core process.

At present, rand-functors only supports random variables that are either of type bool or of a numeric type occupying no more than 16 bits by default. However, it is possible to implement all the requisite traits for a custom data type.

rand-functors's People

Contributors

adsteele916 avatar

Watchers

 avatar

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.