npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

foresight

v0.0.1

Published

Predict the next output of a process which can only output binary states.

Downloads

6

Readme

foresight Build Status

Predict the next output of a process which can only output binary states.

If a process outputs only two states (random or predictable), it's sometimes useful to predict the next state the process would output.

Why?

Here is a usage application:

function isAwesome(input) {
    
    if (expensiveComputation(input))
        return true;

    if (expensiveOperation(input))
        return true;

    return false;
}

// can potentially reduce potential computations by predicting which algorithm would hit first
function betterIsAwesome(input) {
    
    var order = foresight.guess();
    var algorithms = [expensiveComputation, expensiveOperation];
    var n = 2;

    if (order === 'algo1') {
        while(n-- > 0) {
            if(algorithms[1-n](input)) {
                (n == 1) && foresight.move('algo1') || foresight.move('algo2');
                return true;
            }
        }
    } else {
        while(n-- > 0) {
            if(algorithms[n](input)) {
                (n == 1) && foresight.move('algo2') || foresight.move('algo1');
                return true;
            }
        }
    }

    return false;
}

Usage

$ npm install --save foresight
var foresightMaker = require('foresight');

var foresight = foresightMaker();

foresight.move('head');
var guess = foresight.guess();

API

foresightMaker(options)

A factory function that returns an instance of foresight object.

options

Type: object

options.moves

By default foresight uses coin-flipping semantics to capture actual moves and to predict the next move.

You may remap these values to something else. Once they're remapped, you must use the new values for foresight.move(actual). And as well as expect them as output of foresight.guess().

Example:

options.move = {
    head: 'left',
    tail: 'right'
}

Default:

options.move = {
    head: 'head',
    tail: 'tail',
    pass: 'pass'
}

foresight.move(actual)

Input the actual state output of the tracking process.

actual

Value: head, tail, or values mapped to head and tail.

NOTE: actual may not equal to pass (in default move semantics) or move mapped to pass. Only foresight may return pass; which indicates that it is uncertain of its guess.

foresight.guess()

Returns a guess of the next state that a process would output.

If foresight is uncertain of the next move, it would output pass (or remapped value of pass).

Otherwise, if foresight is certain of the next move, it would output head or tail (or remapped values).

Credit

Code is ported to a usable npm module from: http://www.loper-os.org/bad-at-entropy/manmach.html

I did not come up with the underlying algorithm. I just rewrapped it into nicer API.

Weaknesses

As demonstrated by someone, the algorithm is deterministic, and one can beat it. You should expect that the process you're tracking won't likely output the precise sequences of moves that foresight would guess incorrectly most of the time.

The algorithm is light enough to be used in favour of a PRNG in some applications. Especially when heavyweight prediction tools isn't a likely solution.

License

MIT