Git Product home page Git Product logo

wrap-around's Introduction

wrap-around

table of values

Restricts a number n to the interval 0 <= n < m by "wrapping it around" within said range.

const wrap = require('wrap-around')
var result = {}
for (var m = 3, n = -3; n < 9; n++) {
  result[n] = wrap(m, n)
}

Some possible uses of this function include:

  • restricting an index to valid array indices
index = wrap(array.length, index)
  • selecting items from the end of a list (Python-style!)
> var array = ['foo', 'bar', 'baz']
> array[wrap(array.length, -1)]
'baz'
  • wrapping the player around the screen in a game of Pac-Man, Snake, etc.
hero.position = [wrap(width, x), wrap(height, y)]

usage

NPM

Why not just use n % m?

While the modulo operator wraps positive values with ease (it's actually used internally by the wrap function), it takes a bit more setup to handle negative values correctly. Consider the following example, in which % fails to provide the desired result:

> -1 % 3
-1

> wrap(3, -1)
2

What about loops?

Using loops for this kind of thing is a handy way of demonstrating what exactly this function does - wrap(m, n) produces the same result as two loops forcing a number between the desired range.

while (n < 0) n += m
while (n >= m) n -= m

Unfortunately, they're also 300x slower. ๐Ÿ˜ฌ

# wrap 100000 times
ok ~4.7 ms (0 s + 4696167 ns)

# loop 100000 times
ok ~1.36 s (1 s + 359910028 ns)

So at the end of the day, you're better off avoid loops for "wrapping" numbers in production code. Use the modulo % operator for positive numbers or wrap-around if you plan on handling negative numbers.

license

MIT ยฉ Brandon Semilla

wrap-around's People

Contributors

semibran avatar

Stargazers

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

Watchers

 avatar  avatar

wrap-around's Issues

Add unit tests

Probably just assert that all n between [-2*m..2*m] give expected results. Could panic if m <= 0 as well but thats some minor conditional branching overhead.

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.